Google Forms Quiz Timer

Додає таймер для Google Forms

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

Advertisement:

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

Advertisement:

// ==UserScript==
// @name         Google Forms Quiz Timer
// @namespace    https://docs.google.com/forms/
// @version      2026-05-21
// @description  Додає таймер для Google Forms
// @author       
// @match        https://docs.google.com/forms/*
// @icon         https://www.gstatic.com/images/branding/product/1x/forms_2020q4_48dp.png
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let seconds = 0;
    let timerBox = null;
    let timerInterval = null;
    let observer = null;

    function formatTime(totalSeconds) {
        const hrs = String(Math.floor(totalSeconds / 3600)).padStart(2, '0');
        const mins = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0');
        const secs = String(totalSeconds % 60).padStart(2, '0');
        return `${hrs}:${mins}:${secs}`;
    }

    function createTimer() {
        if (document.getElementById('gf-quiz-timer')) return;

        timerBox = document.createElement('div');
        timerBox.id = 'gf-quiz-timer';
        timerBox.textContent = `⏱ ${formatTime(seconds)}`;

        Object.assign(timerBox.style, {
            position: 'fixed',
            top: '20px',
            right: '20px',
            background: '#111',
            color: '#00ff7f',
            padding: '10px 14px',
            borderRadius: '10px',
            fontSize: '16px',
            fontFamily: 'monospace',
            fontWeight: 'bold',
            zIndex: '999999',
            boxShadow: '0 4px 12px rgba(0,0,0,0.35)',
            userSelect: 'none',
            pointerEvents: 'none'
        });

        document.body.appendChild(timerBox);

        timerInterval = setInterval(() => {
            seconds++;
            if (timerBox) {
                timerBox.textContent = `⏱ ${formatTime(seconds)}`;
            }
        }, 1000);
    }

    function removeTimer() {
        if (timerInterval) {
            clearInterval(timerInterval);
            timerInterval = null;
        }

        if (timerBox) {
            timerBox.remove();
            timerBox = null;
        }
    }

    function isFormPage() {
        // Запускаємо таймер тільки на сторінці перегляду/заповнення форми
        return location.href.includes('/viewform');
    }

    function isFinishedPage() {
        // Google Forms зазвичай перенаправляє на /formResponse після відправки
        if (location.href.includes('/formResponse')) return true;

        // Перевірка тексту на випадок односторінкового оновлення
        const text = document.body.innerText || '';
        return (
            text.includes('Відповідь записано') ||
            text.includes('Ваша відповідь була записана') ||
            text.includes('Your response has been recorded') ||
            text.includes('Дякуємо') ||
            text.includes('Thank you')
        );
    }

    function initObserver() {
        if (observer) observer.disconnect();

        observer = new MutationObserver(() => {
            if (isFinishedPage()) {
                removeTimer();
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    function init() {
        if (isFinishedPage()) return; // Якщо це сторінка результатів - нічого не робимо
        if (!isFormPage()) return; // Якщо це режим редагування форми - теж ігноруємо

        createTimer();
        initObserver();
    }

    window.addEventListener('load', init);
})();