leetcode-cage

Removes editorial, solutions, discussion, topics, hints section on leetCode problem pages

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         leetcode-cage
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Removes editorial, solutions, discussion, topics, hints section on leetCode problem pages
// @author       Lovelin Dhoni J B
// @match        https://leetcode.com/problems/*
// @match        https://cn.leetcode.com/problems/*
// @match        https://leetcode.cn/problems/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  function containsTextRecursively(element, texts) {
    if (!element) return false;
    const walker = document.createTreeWalker(
      element,
      NodeFilter.SHOW_TEXT,
      null,
    );
    while (walker.nextNode()) {
      const nodeText = walker.currentNode.nodeValue.trim();
      if (texts.some((text) => nodeText.includes(text))) {
        return true;
      }
    }
    return false;
  }

  function cleanPage() {
    document
      .querySelectorAll(
        "div.flexlayout__tab_button.flexlayout__tab_button_top.flexlayout__tab_button--unselected",
        "div.flexlayout__tab_button.flexlayout__tab_button_top.flexlayout__tab_button--selected"
      )
      .forEach((div) => {
        const text = div.textContent.trim();
        if (text.includes("Solutions") || text.includes("Editorial")) {
          div.remove();
        }
      });

    const statsDivs = document.querySelectorAll("div.mt-6.flex.flex-col.gap-3");
    statsDivs.forEach((div) => {
      const keywords = [
        "Accepted",
        "Acceptance Rate",
        "Topics",
        "Companies",
        "Discussion",
      ];
      if (containsTextRecursively(div, keywords)) {
        div.remove();
      }
    });

    const tagDivs = document.querySelectorAll("div.flex.gap-1");
    tagDivs.forEach((div) => {
      const keywords = ["Topics", "Companies"];
      if (containsTextRecursively(div, keywords)) {
        div.remove();
      }
    });

    const problemListDivs = document.querySelectorAll(
      ".lc-md\\:flex.group.flex.max-w-\\[300px\\].items-center.overflow-hidden.rounded.hover\\:bg-fill-tertiary.dark\\:hover\\:bg-fill-tertiary"
    );
    problemListDivs.forEach((div) => {
      if (containsTextRecursively(div, ["Problem List"])) {
        div.remove();
      }
    });
  }

  cleanPage();

  const observer = new MutationObserver(cleanPage);
  observer.observe(document.body, { childList: true, subtree: true });
})();