Greasy Fork is available in English.

LeetCode Reset Stopwatch

Leetcode reset the stopwatch with one click

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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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.

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

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