Leetcode Question Difficulty Hider

Hide the difficulty tag on a leetcode question page with a reveal option.

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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 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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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 Question Difficulty Hider
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Hide the difficulty tag on a leetcode question page with a reveal option.
// @author       DoubleX
// @match        https://leetcode.com/problems/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function waitForElm(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    observer.disconnect();
                    resolve(document.querySelector(selector));
                }
            });

            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        });
    }

    function revealDiff(selector, diffcolor, difftext) {
        waitForElm(selector).then((ele) => {
            ele.style.backgroundColor = "black";
            ele.style.color = "white";
            ele.innerText = "Reveal";
            ele.style.cursor = "pointer";
            ele.toggled = false;
            ele.onclick = (e) => {
                if (ele.toggled) {
                    ele.style.backgroundColor = "black";
                    ele.style.color = "white";
                    ele.innerText = "Reveal";
                    ele.toggled = false;
                }
                else {
                    ele.style.backgroundColor = "var(--fill-secondary)";
                    ele.style.color = diffcolor;
                    ele.innerText = difftext;
                    ele.toggled = true;
                }
            }
        });
    }

    function setup(){
        revealDiff(".text-difficulty-hard", "var(--difficulty-hard)", "Hard");
        revealDiff(".text-difficulty-medium", "var(--difficulty-medium)", "Medium");
        revealDiff(".text-difficulty-easy", "var(--difficulty-easy)", "Easy");
    };

    if (document.readyState !== 'loading') {
        setup();
    } else {
        document.addEventListener('DOMContentLoaded', function () {
            setup();
        });
    }
})();