Greasy Fork is available in English.

LeetCode Turbo

Replace monaco to vanilla textarea.

2023-11-14 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name LeetCode Turbo
  3. // @description Replace monaco to vanilla textarea.
  4. // @namespace https://greatest.deepsurf.us/users/197529
  5. // @version 0.1.0
  6. // @author kkocdko
  7. // @license Unlicense
  8. // @match *://leetcode.cn/problems/*
  9. // @run-at document-start
  10. // ==/UserScript==
  11. "use strict";
  12.  
  13. const globalThis = this.unsafeWindow || this;
  14. const originFetch = globalThis.fetch;
  15. const textarea = document.createElement("textarea");
  16. textarea.style =
  17. "font-family: monospace; height: 100%; width: 100%; padding: 6px 10px; white-space: pre; outline: none;";
  18. const replaceEditorTimer = setInterval(() => {
  19. try {
  20. document.querySelector("#editor").replaceWith(textarea);
  21. clearInterval(replaceEditorTimer);
  22. } catch (_) {}
  23. }, 500);
  24. globalThis.fetch = (input, init) => {
  25. if (input?.includes("/lc-monaco/") || input?.includes("/monaco-tm/")) {
  26. throw Error("Monaco editor blocked.");
  27. }
  28. if (input?.endsWith("/submit") || input?.endsWith("/submit/")) {
  29. init.body = JSON.stringify({
  30. ...JSON.parse(init.body),
  31. typed_code: textarea.value,
  32. });
  33. }
  34. return originFetch(input, init);
  35. };
  36. const titleSlug = location.href.split("/problems/")[1].split("/")[0];
  37. fetch("https://leetcode.cn/graphql/", {
  38. headers: { "content-type": "application/json" },
  39. method: "POST",
  40. body: JSON.stringify({
  41. operationName: "questionEditorData",
  42. variables: { titleSlug },
  43. query: `
  44. query questionEditorData($titleSlug: String!) {
  45. question(titleSlug: $titleSlug) {
  46. codeSnippets {
  47. langSlug
  48. code
  49. }
  50. }
  51. }
  52. `,
  53. }),
  54. })
  55. .then((v) => v.json())
  56. .then((v) => {
  57. textarea.value = v.data.question.codeSnippets.find(
  58. (v) => v.langSlug == "cpp"
  59. ).code;
  60. });
  61.  
  62. // more conservative requestAnimationFrame
  63. // const originRequestAnimationFrame = globalThis.requestAnimationFrame;
  64. // let rafCounter = 0;
  65. // let rafBoost = false;
  66. // globalThis.addEventListener("pointermove", () => {
  67. // rafBoost = true;
  68. // });
  69. // globalThis.requestAnimationFrame = (callback) => {
  70. // // if (rafBoost || rafCounter === 0) {
  71. // originRequestAnimationFrame(callback);
  72. // // } else {
  73. // setTimeout(() => {
  74. // originRequestAnimationFrame(callback);
  75. // }, 900);
  76. // // }
  77. // rafCounter = (rafCounter + 1) % 4;
  78. // };
  79.  
  80. // https://leetcode.cn/problems/intersection-of-two-arrays-ii/description/
  81.  
  82. // ublock append ||static.leetcode.cn/lc-monaco/
  83.  
  84. /*
  85. fetch("https://leetcode.cn/graphql/", {
  86. headers: { "content-type": "application/json" },
  87. method: "POST",
  88. body: JSON.stringify({
  89. query:
  90. "\n query questionTitle($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n title\n titleSlug\n isPaidOnly\n difficulty\n likes\n dislikes\n categoryTitle\n }\n}\n ",
  91. variables: { titleSlug: "intersection-of-two-arrays-ii" },
  92. operationName: "questionTitle",
  93. }),
  94. })
  95. .then((v) => v.text())
  96. .then((v) => console.log(v));
  97.  
  98. // 获取预设代码片段
  99. fetch("https://leetcode.cn/graphql/", {
  100. headers: { "content-type": "application/json" },
  101. method: "POST",
  102. body: JSON.stringify({
  103. query:
  104. "\n query questionEditorData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n codeSnippets {\n lang\n langSlug\n code\n }\n envInfo\n enableRunCode\n hasFrontendPreview\n frontendPreviews\n }\n}\n ",
  105. variables: { titleSlug: "intersection-of-two-arrays-ii" },
  106. operationName: "questionEditorData",
  107. }),
  108. })
  109. .then((v) => v.json())
  110. .then((v) => console.log(v));
  111.  
  112. fetch("https://leetcode.cn/graphql/", {
  113. headers: { "content-type": "application/json" },
  114. method: "POST",
  115. body: JSON.stringify({
  116. query:
  117. "\n query questionContent($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n editorType\n mysqlSchemas\n dataSchemas\n }\n}\n ",
  118. variables: { titleSlug: "intersection-of-two-arrays-ii" },
  119. operationName: "questionContent",
  120. }),
  121. });
  122.  
  123. fetch("https://leetcode.cn/graphql/", {
  124. headers: { "content-type": "application/json" },
  125. method: "POST",
  126. body: JSON.stringify({
  127. query:
  128. "\n query questionTranslations($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n translatedTitle\n translatedContent\n }\n}\n ",
  129. variables: { titleSlug: "intersection-of-two-arrays-ii" },
  130. operationName: "questionTranslations",
  131. }),
  132. });
  133. */