Greasy Fork is available in English.

LeetCode OneInput

一键复制所有样例输入

  1. // ==UserScript==
  2. // @name LeetCode OneInput
  3. // @namespace https://leetcode-cn.com/
  4. // @version 1.1
  5. // @description 一键复制所有样例输入
  6. // @author Mcginn
  7. // @match https://leetcode-cn.com/problems/*
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_setClipboard
  10. // @grant GM_addStyle
  11. // @grant GM_getResourceText
  12. // @resource notycss https://cdn.jsdelivr.net/npm/noty@3.1.4/lib/noty.min.css
  13. // @require https://cdn.jsdelivr.net/npm/noty@3.1.4/lib/noty.min.js
  14. // @require https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js
  15. // @run-at document-end
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. GM_addStyle(GM_getResourceText('notycss'));
  22.  
  23. function isDidit(character) {
  24. return '0' <= character && character <= '9';
  25. }
  26.  
  27. function parseInput(problemDescription) {
  28. var idx = 0, inputs = new Array();
  29. while (idx < problemDescription.length) {
  30. if ('[' == problemDescription[idx]) {
  31. var cnt = 0, indexStart = idx;
  32. while (idx < problemDescription.length) {
  33. switch (problemDescription[idx++]) {
  34. case '[':
  35. ++cnt;
  36. break;
  37. case ']':
  38. --cnt;
  39. break;
  40. default:
  41. break;
  42. }
  43. if (0 == cnt)
  44. break;
  45. }
  46. inputs.push(problemDescription.substring(indexStart, idx));
  47. } else if ('\"' == problemDescription[idx]) {
  48. var indexStart = idx++;
  49. while (idx < problemDescription.length && problemDescription[idx] != '\"') {
  50. ++idx;
  51. }
  52. inputs.push(problemDescription.substring(indexStart, ++idx));
  53. } else if ('0' <= problemDescription[idx] && problemDescription[idx] <= '9') {
  54. var indexStart = idx;
  55. while (idx < problemDescription.length && isDidit(problemDescription[idx])) {
  56. ++idx;
  57. }
  58. var indexEnd = idx;
  59. var strInput = problemDescription.substring(indexStart, indexEnd);
  60. inputs.push(strInput);
  61. } else {
  62. ++idx;
  63. }
  64. }
  65. return inputs
  66. }
  67.  
  68.  
  69. function parseExampleInput(problemDescription) {
  70. var arrayInput = new Array();
  71. var regexpInput = /输入[::]((.|\n)+?)输出/g
  72. var ret;
  73. while (ret = regexpInput.exec(problemDescription)) {
  74. var inputs = ret[1];
  75. console.log('inputs = ' + inputs);
  76. arrayInput = arrayInput.concat(parseInput(inputs));
  77. }
  78. var strInput = arrayInput.join('\n');
  79. return strInput;
  80. }
  81.  
  82. function copyAllTestCases() {
  83. new Noty({
  84. type: 'info',
  85. layout: 'topRight',
  86. text: 'Trying to find and copy all testcases',
  87. timeout: 2000
  88. }).show();
  89.  
  90. var selector = 'div.description__2b0C';
  91. if ($(selector) && $(selector).text().length > 0) {
  92. var exampleInputStr = parseExampleInput($(selector).text());
  93. GM_setClipboard(exampleInputStr);
  94. new Noty({
  95. type: "success",
  96. layout: "topRight",
  97. text: "Have copy all testcases: \n" + exampleInputStr,
  98. timeout: 5000
  99. }).show();
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }
  105.  
  106. var checkExist = setInterval(
  107. function() {
  108. if (copyAllTestCases()) {
  109. clearInterval(checkExist);
  110. }
  111. }, 2000);
  112.  
  113. GM_registerMenuCommand("一键复制", copyAllTestCases);
  114. })();