Leetcode ProblemSet Stat Better Display

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

Устаревшая версия за 05.02.2019. Перейдите к последней версии.

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