Google Forms Quiz Timer

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

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