Greasy Fork is available in English.

Google Forms Quiz Timer

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();