Greasy Fork is available in English.

Leetcode problem pass rate

为 Leetcode(英文版)增加题目通过率,使用网站**原本样式**。

  1. // ==UserScript==
  2. // @name Leetcode problem pass rate
  3. // @namespace https://github.com/rustberry
  4. // @version 0.1
  5. // @description 为 Leetcode(英文版)增加题目通过率,使用网站**原本样式**。
  6. // @description:en Add the pass rate of Leetcode problems.
  7. // @author Rust
  8. // @match https://leetcode.com/problems/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. function addRate() {
  15. let arr = document.querySelectorAll(".css-oqu510")
  16. console.log(arr)
  17. var ac = arr[0].lastElementChild.textContent
  18. var sub = arr[1].lastElementChild.textContent
  19. console.log(ac, sub)
  20.  
  21. ac = parseInt(ac.replace(/,/g, ''))
  22. sub = parseInt(sub.replace(/,/g, ''))
  23. console.log(ac, sub)
  24. var res = ac/sub * 100
  25. res = res.toPrecision(4) + "%"
  26.  
  27. var parent = document.querySelector("#app > div > div.main__2_tD > div.content__3fR6 > div > div.side-tools-wrapper__1TS9 > div > div.css-9z7f7i-Container.e5i1odf0 > div.css-jtoecv > div > div.tab-pane__ncJk.css-xailxq-TabContent.e5i1odf5 > div > div:nth-child(3) > div.css-12aggky")
  28. var html =
  29. '<div class="css-oqu510"><div class="css-y3si18">Rate</div><div class="css-jkjiwi">'
  30. + res + '</div></div>'
  31.  
  32. parent.insertAdjacentHTML('beforeend', html)
  33. }
  34. function waitForElement(selector) {
  35. var timeout = 60000 // wait for at most 1 minute
  36. var start = performance.now();
  37. var now = 0;
  38.  
  39. return new Promise(function (resolve, reject) {
  40. var interval = setInterval(function () {
  41. var element = document.querySelectorAll(selector);
  42.  
  43. if (element !== null && element.length >= 2) {
  44. clearInterval(interval);
  45. console.log("Exists!");
  46. addRate()
  47. resolve();
  48. }
  49.  
  50. now = performance.now();
  51.  
  52. if (now - start >= timeout) {
  53. reject("Could not find the element " + selector + " within " + timeout + " ms");
  54. }
  55. }, 100); // check every 100ms
  56. });
  57. }
  58. waitForElement(".css-oqu510")
  59. })();