Leetcode Question Difficulty Hider

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

  1. // ==UserScript==
  2. // @name Leetcode Question Difficulty Hider
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Hide the difficulty tag on a leetcode question page with a reveal option.
  6. // @author DoubleX
  7. // @match https://leetcode.com/problems/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function waitForElm(selector) {
  17. return new Promise(resolve => {
  18. if (document.querySelector(selector)) {
  19. return resolve(document.querySelector(selector));
  20. }
  21.  
  22. const observer = new MutationObserver(mutations => {
  23. if (document.querySelector(selector)) {
  24. observer.disconnect();
  25. resolve(document.querySelector(selector));
  26. }
  27. });
  28.  
  29. observer.observe(document.body, {
  30. childList: true,
  31. subtree: true
  32. });
  33. });
  34. }
  35.  
  36. function revealDiff(selector, diffcolor, difftext) {
  37. waitForElm(selector).then((ele) => {
  38. ele.style.backgroundColor = "black";
  39. ele.style.color = "white";
  40. ele.innerText = "Reveal";
  41. ele.style.cursor = "pointer";
  42. ele.toggled = false;
  43. ele.onclick = (e) => {
  44. if (ele.toggled) {
  45. ele.style.backgroundColor = "black";
  46. ele.style.color = "white";
  47. ele.innerText = "Reveal";
  48. ele.toggled = false;
  49. }
  50. else {
  51. ele.style.backgroundColor = "var(--fill-secondary)";
  52. ele.style.color = diffcolor;
  53. ele.innerText = difftext;
  54. ele.toggled = true;
  55. }
  56. }
  57. });
  58. }
  59.  
  60. function setup(){
  61. revealDiff(".text-difficulty-hard", "var(--difficulty-hard)", "Hard");
  62. revealDiff(".text-difficulty-medium", "var(--difficulty-medium)", "Medium");
  63. revealDiff(".text-difficulty-easy", "var(--difficulty-easy)", "Easy");
  64. };
  65.  
  66. if (document.readyState !== 'loading') {
  67. setup();
  68. } else {
  69. document.addEventListener('DOMContentLoaded', function () {
  70. setup();
  71. });
  72. }
  73. })();