Leetcode Question Difficulty Hider

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

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

You will need to install a user script manager extension to install this script.

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

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

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