Greasy Fork is available in English.

Leetcode problem pass rate

Add the pass rate of Leetcode problems

Verze ze dne 19. 05. 2020. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name Leetcode problem pass rate
  3. // @namespace https://github.com/rustberry
  4. // @version 0.1
  5. // @description Add the pass rate of Leetcode problems
  6. // @author Rust
  7. // @match https://leetcode.com/problems/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. function addRate() {
  14. let arr = document.querySelectorAll(".css-oqu510")
  15. console.log(arr)
  16. var ac = arr[0].lastElementChild.textContent
  17. var sub = arr[1].lastElementChild.textContent
  18. console.log(ac, sub)
  19.  
  20. ac = parseInt(ac.replace(/,/g, ''))
  21. sub = parseInt(sub.replace(/,/g, ''))
  22. console.log(ac, sub)
  23. var res = ac/sub * 100
  24. res = res.toPrecision(4) + "%"
  25.  
  26. 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")
  27. var html =
  28. '<div class="css-oqu510"><div class="css-y3si18">Rate</div><div class="css-jkjiwi">'
  29. + res + '</div></div>'
  30.  
  31. parent.insertAdjacentHTML('beforeend', html)
  32. }
  33. function waitForElement(selector) {
  34. var timeout = 60000 // wait for at most 1 minute
  35. var start = performance.now();
  36. var now = 0;
  37.  
  38. return new Promise(function (resolve, reject) {
  39. var interval = setInterval(function () {
  40. var element = document.querySelectorAll(selector);
  41.  
  42. if (element !== null && element.length >= 2) {
  43. clearInterval(interval);
  44. console.log("Exists!");
  45. addRate()
  46. resolve();
  47. }
  48.  
  49. now = performance.now();
  50.  
  51. if (now - start >= timeout) {
  52. reject("Could not find the element " + selector + " within " + timeout + " ms");
  53. }
  54. }, 100); // check every 100ms
  55. });
  56. }
  57. waitForElement(".css-oqu510")
  58. })();