Guest User

有没有人一起从零开始刷力扣,作者:@尤达大师

a guest
Jul 29th, 2022
2,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. // 脚本使用方法请参考 https://www.tampermonkey.net/faq.php 或 https://greasyfork.org/zh-CN (这行代码不要复制)
  2. // 这俩都是国外网站, 上不去的话直接百度 "油猴脚本使用方法" 就行(这行代码也不要复制)
  3.  
  4. // ==UserScript==
  5. // @name 有没有人一起从零开始刷力扣
  6. // @namespace likou-replace
  7. // @version 1.0
  8. // @description none
  9. // @author Permission
  10. // @match https://leetcode.cn/circle/article/48kq9d/*
  11. // @require https://code.jquery.com/jquery-3.4.1.min.js
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /* globals $, jQuery */
  16. 'use strict';
  17.  
  18. let proMap = new Map(),
  19. transMap = new Map(),
  20. buildMapComplete = false;
  21.  
  22. const getProblems = () => {
  23. $.get("https://leetcode.cn/api/problems/all/").then((response) => {
  24. getTrans(JSON.parse(response));
  25. });
  26. }
  27.  
  28. const getTrans = (picker) => {
  29. $.ajax({
  30. method: "POST",
  31. url: 'https://leetcode.cn/graphql/',
  32. headers: {
  33. "content-type": "application/json",
  34. "x-definition-name": "getQuestionTranslation",
  35. "x-operation-name": "getQuestionTranslation",
  36. "x-csrftoken": getCookie("csrftoken"),
  37. "x-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone
  38. },
  39. data: JSON.stringify({
  40. "operationName": "getQuestionTranslation",
  41. "variables": {},
  42. "query": "query getQuestionTranslation($lang: String) {translations: allAppliedQuestionTranslations(lang: $lang) {title questionId __typename}}"
  43. })
  44. }).then((trans) => {
  45. buildMap(picker, trans);
  46. });
  47. }
  48.  
  49.  
  50. const buildMap = (picker, trans) => {
  51. for (let pro of picker.stat_status_pairs) {
  52. proMap.set(pro.stat.frontend_question_id, pro.stat.question__title_slug);
  53. }
  54. for (let t of trans.data.translations) {
  55. transMap.set(t.questionId, t.title);
  56. }
  57. buildMapComplete = true;
  58. };
  59.  
  60. const getCookie = (name) => {
  61. const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
  62. const arr = document.cookie.match(reg);
  63. if (arr) {
  64. return unescape(arr[2]);
  65. } else {
  66. return null;
  67. }
  68. };
  69.  
  70. const replace = () => {
  71. let even = true;
  72. for (let problem of $("table tr td")) {
  73. if (!even) {
  74. let htmlString = "";
  75. let normalExit = true;
  76. for (let id of problem.textContent.split('、')) {
  77. if (isNaN(parseInt(id))) {
  78. normalExit = false;
  79. break;
  80. }
  81. htmlString += `<a href = 'https://leetcode.cn/problems/${proMap.get(id)}/' title = '${transMap.get(id)}' target = '_blank'>${id}</a>、`;
  82. }
  83. if (normalExit) {
  84. problem.innerHTML = `<td>${htmlString.substring(0, htmlString.length - 1)}</td>`;
  85. }
  86. }
  87. even = !even;
  88. }
  89.  
  90. }
  91.  
  92. getProblems();
  93.  
  94. const interval = setInterval(() => {
  95. if (buildMapComplete && $("table tr td").length !== 0) {
  96. clearInterval(interval);
  97. replace();
  98. }
  99. }, 5e2);
Advertisement
Add Comment
Please, Sign In to add comment