LeetCode Reset Stopwatch

Leetcode reset the stopwatch with one click

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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