Greasy Fork is available in English.

Leetcode ProblemSet Stat Better Display

To better display Leetcode ProblemSet Stat; to hide and dismiss the locked problems.

Fra og med 04.02.2019. Se den nyeste version.

  1. // ==UserScript==
  2. // @name Leetcode ProblemSet Stat Better Display
  3. // @namespace
  4. // @description To better display Leetcode ProblemSet Stat; to hide and dismiss the locked problems.
  5. // @version 0.1
  6. // @author pcm
  7. // @match https://leetcode.com/problemset/*
  8. // @match https://leetcode.com/tag/*
  9. // @run-at document-start
  10. // @grant GM_xmlhttpRequest
  11. // @connect leetcode.com
  12. // ==/UserScript==
  13.  
  14. // some global configuration
  15. CONF = {
  16. 'SVG_HEIGHT': 480,
  17. 'SVG_WIDTH': 300,
  18. 'COLOR_AC': '#7bc96f',
  19. 'COLOR_TODO': '#ebedf0',
  20. 'CELL_HEIGHT': 10,
  21. 'CELL_WIDTH': 10,
  22. 'CELL_MARGIN': 2,
  23. 'CELL_W_COUNT': 25,
  24. }
  25.  
  26.  
  27. // if switchHideLockedProblems == true, hide all locked problems in problemset
  28. function statDifficulty(problemTable, switchHideLockedProblems, switchDrawProgressBar) {
  29. var res = {
  30. 'num_total_easy': 0,
  31. 'num_total_normal': 0,
  32. 'num_total_hard': 0,
  33. 'num_total': 0,
  34. 'num_ac_easy': 0,
  35. 'num_ac_normal': 0,
  36. 'num_ac_hard': 0,
  37. 'num_ac': 0,
  38. }
  39.  
  40. // find all related indices
  41. var index = {}
  42. var thead = problemTable.getElementsByTagName('thead')[0];
  43. var ths = thead.getElementsByTagName("th");
  44. for (var i = 0; i < ths.length; i++) {
  45. var th = ths[i];
  46. if (th.textContent == 'Difficulty') index['difficulty'] = i;
  47. if (th.textContent == '\xa0') index['status'] = i;
  48. if (th.textContent == 'Title') index['title'] = i;
  49. if (th.textContent == '#') index['#'] = i;
  50. }
  51. if (keys(index).indexOf('status') == -1) index['status'] = 0;
  52. if (keys(index).indexOf('#') == -1) index['#'] = 1;
  53.  
  54. var tbody = problemTable.getElementsByTagName('tbody')[0];
  55. var trs = tbody.getElementsByTagName("tr");
  56. for ( var i = 0; i < trs.length; i++) {
  57. var tds = trs[i].getElementsByTagName("td");
  58. var td_status = tds[index['status']];
  59. var td_title = tds[index['title']];
  60. var td_difficulty = tds[index['difficulty']];
  61. var td_number = tds[index['#']];
  62. // dismiss locked problems
  63. var locked = td_title.getElementsByClassName('fa-lock');
  64. if (locked[0]) {
  65. if (switchHideLockedProblems) {
  66. trs[i].innerHTML = "";
  67. }
  68. continue;
  69. }
  70.  
  71. // stat difficulty
  72. var hard = td_difficulty.getElementsByClassName('label-danger');
  73. var normal = td_difficulty.getElementsByClassName('label-warning');
  74. var easy = td_difficulty.getElementsByClassName('label-success');
  75. var ac = td_status.getElementsByClassName('fa-check');
  76. if (switchDrawProgressBar) {
  77. var title = td_number.textContent + ' ' + td_title.firstChild.firstChild.textContent;
  78. drawOneCell(res['num_total'], ac[0] ? true : false, title);
  79. }
  80. if (hard[0]) {
  81. res['num_total_hard']++;
  82. if (ac[0]) res['num_ac_hard']++;
  83. }
  84. else if (normal[0]) {
  85. res['num_total_normal']++;
  86. if (ac[0]) res['num_ac_normal']++;
  87. }
  88. else if (easy[0]) {
  89. res['num_total_easy']++;
  90. if (ac[0]) res['num_ac_easy']++;
  91. }
  92. res['num_total']++;
  93. if (ac[0]) res['num_ac']++;
  94. }
  95. return res;
  96. }
  97.  
  98. function updateStatBar(welcomes) {
  99. var statBar = document.getElementById('welcome');
  100. if (!statBar) {
  101. var div = document.getElementsByClassName('header__2ZIe')[0];
  102. var p = div.getElementsByTagName('p')[1];
  103. p.innerHTML = '<div id="welcome"><span><span class="label label-primary round">Solved</span>&nbsp;-&nbsp;<span class="label label-success round"></span>&nbsp;<span class="label label-warning round"></span>&nbsp;<span class="label label-danger round"></span></span></div>';
  104. statBar = document.getElementById('welcome');
  105. }
  106. var total = statBar.getElementsByClassName('label-primary')[0];
  107. var hard = statBar.getElementsByClassName('label-danger')[0];
  108. var normal = statBar.getElementsByClassName('label-warning')[0];
  109. var easy = statBar.getElementsByClassName('label-success')[0];
  110.  
  111. total.textContent = welcomes['num_ac'].toString() + '/' + welcomes['num_total'].toString() + ' Solved';
  112. hard.textContent = 'Hard ' + welcomes['num_ac_hard'].toString() + '/' + welcomes['num_total_hard'].toString();
  113. normal.textContent = 'Medium ' + welcomes['num_ac_normal'].toString() + '/' + welcomes['num_total_normal'].toString();
  114. easy.textContent = 'Easy ' + welcomes['num_ac_easy'].toString() + '/' + welcomes['num_total_easy'].toString();
  115. }
  116.  
  117. function drawOneCell(i, ac, title) {
  118. var svg = document.getElementById('gm_progress_graph');
  119. var x = (i % CONF['CELL_W_COUNT']) * (CONF['CELL_MARGIN'] + CONF['CELL_WIDTH']);
  120. var y = Math.floor(i / CONF['CELL_W_COUNT']) * (CONF['CELL_MARGIN'] + CONF['CELL_HEIGHT']);
  121. var rectHTML =
  122. '<rect ' +
  123. 'width="' + CONF['CELL_WIDTH'] + '" ' +
  124. 'height="' + CONF['CELL_HEIGHT'] + '" ' +
  125. 'x="' + x.toString() + '" ' +
  126. 'y="' + y.toString() + '" ' +
  127. 'fill="' + (ac ? CONF['COLOR_AC'] : CONF['COLOR_TODO']) + '" ';
  128.  
  129. if (ac) {
  130. rectHTML += '>' +
  131. '<title>' + title + '</title>' +
  132. '</rect>'
  133. }
  134. else {
  135. rectHTML += '/>';
  136. }
  137. svg.insertAdjacentHTML('beforeend', rectHTML);
  138. svg.setAttribute('height', y + (CONF['CELL_MARGIN'] + CONF['CELL_HEIGHT']));
  139. }
  140.  
  141. function initDrawProgressGraph() {
  142. var div = document.getElementsByClassName('progress-panel-base');
  143. if (!div[0]) div = document.getElementsByClassName('header__2ZIe')[0];
  144. else div = div[0].firstChild;
  145. var svgHTML =
  146. '<div><svg ' +
  147. 'width="' + CONF['SVG_WIDTH'] + '" ' +
  148. 'height="' + CONF['SVG_HEIGHT'] + '" ' +
  149. 'id="gm_progress_graph" xmlns="http://www.w3.org/2000/svg"' +
  150. '>' +
  151. '<rect width="100%" height="100%" style="fill:white;/> ' +
  152. '</svg></div>'
  153. div.insertAdjacentHTML('afterend', svgHTML);
  154. return true;
  155. }
  156.  
  157. function changeStatInfo() {
  158. var problemTable = document.getElementsByClassName('question-list-table');
  159. if (!problemTable[0])
  160. problemTable = document.getElementsByClassName('table__XKyc');
  161. if (problemTable[0]) {
  162. var switchHideLockedProblems = true;
  163. var switchDrawProgressBar = initDrawProgressGraph();
  164. var welcomes = statDifficulty(problemTable[0], switchHideLockedProblems, switchDrawProgressBar);
  165. updateStatBar(welcomes);
  166. }
  167. }
  168.  
  169. changeStatInfo()
  170.  
  171. window.addEventListener("DOMContentLoaded", function load() {
  172. window.removeEventListener("DOMContentLoaded", load, false);
  173. // For initial page load
  174. changeStatInfo();
  175. }, false);