Better Stats Bar (LeetCode)

Adds total questions per difficulty to the stats bar.

As of 2019-01-02. See the latest version.

  1. // ==UserScript==
  2. // @name Better Stats Bar (LeetCode)
  3. // @description Adds total questions per difficulty to the stats bar.
  4. // @namespace https://greatest.deepsurf.us/en/users/128831-marvinyan
  5. // @match https://leetcode.com/problemset/algorithms/
  6. // @grant none
  7. // @require https://greatest.deepsurf.us/scripts/374849-library-onelementready-es6/code/Library%20%7C%20onElementReady%20ES6.js?version=649483
  8. // @version 0.0.1.20190102075610
  9. // ==/UserScript==
  10. (() => {
  11. const LC_ALGO_API = 'https://leetcode.com/api/problems/algorithms/';
  12.  
  13. const getData = url =>
  14. new Promise((resolve, reject) => {
  15. const xhr = new XMLHttpRequest();
  16.  
  17. xhr.open('GET', url, true);
  18. xhr.onload = () => {
  19. if (xhr.status >= 200 && xhr.status < 300) {
  20. resolve(xhr.response);
  21. } else {
  22. reject({
  23. status: xhr.status,
  24. statusText: xhr.statusText
  25. });
  26. }
  27. };
  28. xhr.onerror = () => {
  29. reject({
  30. status: xhr.status,
  31. statusText: xhr.statusText
  32. });
  33. };
  34. xhr.send();
  35. });
  36.  
  37. const parseData = response => {
  38. const counts = [0, 0, 0];
  39. const questions = JSON.parse(response).stat_status_pairs;
  40.  
  41. questions.forEach(q => {
  42. counts[q.difficulty.level - 1]++;
  43. });
  44.  
  45. return counts;
  46. };
  47.  
  48. const updateStatsBar = counts => {
  49. const statsBar = $('#welcome > span > span');
  50.  
  51. let $totalSolvedSpan = $(statsBar[0]).closest('span');
  52. const newText = $totalSolvedSpan.text().replace('/', ' / ');
  53. $totalSolvedSpan.text(newText);
  54.  
  55. for (let i = 1; i < statsBar.length; i++) {
  56. statsBar[i].append(` / ${counts[i - 1]}`);
  57. }
  58. };
  59.  
  60. const run = async () => {
  61. const data = await getData(LC_ALGO_API);
  62. const counts = parseData(data);
  63. updateStatsBar(counts);
  64. };
  65.  
  66. waitForKeyElements('#welcome', run);
  67. })();