Google Forms Quiz Timer

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 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);
})();