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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);
})();