LeetCode Reset Stopwatch

Leetcode reset the stopwatch with one click

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         LeetCode Reset Stopwatch
// @description  Leetcode reset the stopwatch with one click
// @namespace    http://tampermonkey.net/
// @version      1.0
// @match        https://leetcode.com/*
// @author       Jatin Sharma
// @icon         https://leetcode.com/favicon.ico
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const sleep = (ms) => new Promise(r => setTimeout(r, ms));

    async function restartStopwatch() {
        for (let i = 0; i < 5; i++) {
            const buttons = document.querySelectorAll('div[role="popover"] button');

            let clickedStart = false;

            for (const btn of buttons) {
                const text = btn.textContent?.trim();

                btn.click();

                if (text?.includes('Start Stopwatch')) {
                    clickedStart = true;
                    break;
                }

                await sleep(10);
            }

            if (clickedStart) break;

            await sleep(80);
        }
    }

    function init() {
        document.addEventListener('click', (e) => {
            if (e.ctrlKey) return;
            const reset = e.target.closest('div[aria-label="Reset"]');
            if (!reset) return;

            restartStopwatch();
        }, true);
    }

    init();
})();