LeetCode自动修正null和None

自动根据选择的编程语言修正测试用例的null文本,python自动修改为None,go和swift自动修改为nil,可自行修改代码自定义

  1. // ==UserScript==
  2. // @name LeetCode自动修正null和None
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-04-11
  5. // @description 自动根据选择的编程语言修正测试用例的null文本,python自动修改为None,go和swift自动修改为nil,可自行修改代码自定义
  6. // @author Ozymandias
  7. // @license MIT
  8. // @match https://leetcode.cn/problems/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Your code here...
  17. function waitForElement(id, callback) {
  18. const intervalId = setInterval(() => {
  19. const targetElements = document.querySelectorAll(id)
  20. if (targetElements.length >0) {
  21. targetElements.forEach((targetElement)=>{
  22. const targetButton=targetElement.querySelectorAll('button.rounded');
  23. if (targetButton!=null){
  24. if(targetButton[0]!=null){
  25. const textContent = targetButton[0].textContent
  26. if(textContent!=null){
  27. const buttonText = textContent.trim().toLowerCase(); // 获取并转换为小写,去除前后空格
  28. if (buttonText!=null &&buttonText!=""){
  29. clearInterval(intervalId);
  30. //console.info("当前使用语言:"+buttonText)
  31. callback(buttonText);
  32. }
  33. }
  34. }
  35. }
  36. })
  37. } else {
  38. console.log("等待按钮组件加载完成中");
  39. }
  40. }, 2000); // 延迟时间,以毫秒为单位
  41. }
  42. function replaceNullStringsInElfjSElements() {
  43. waitForElement('[id^="headlessui-popover-button-:r"]', element => {
  44. const buttonText =element.trim().toLowerCase(); // 获取并转换为小写,去除前后空格
  45. const targetElements = document.querySelectorAll('.elfjS pre'); // 选择目标元素
  46. targetElements.forEach((element) => {
  47. const currentText = element.textContent || element.innerText; // 获取元素文本内容,兼容不同属性
  48. if (currentText) {
  49. //console.log("currentText:"+currentText)
  50. var replacedText=null
  51. switch (buttonText){
  52. case "python":
  53. case "python3":
  54. replacedText = currentText.replace(/null/g, 'None'); // 使用正则表达式替换null字符串
  55. break
  56. case "ruby":
  57. case "swift":
  58. case "go":
  59. case "elixir":
  60. replacedText = currentText.replace(/null/g, 'nil'); // 使用正则表达式替换null字符串
  61. break
  62. //case "erlang":
  63. //replacedText = currentText.replace(/null/g, 'undefined'); // 使用正则表达式替换null字符串
  64. //break
  65. case "racket":
  66. replacedText = currentText.replace(/null/g, '#false'); // 使用正则表达式替换null字符串
  67. break
  68. default:
  69. replacedText = currentText
  70. break
  71.  
  72. }
  73. //console.log("replacedText:"+replacedText)
  74.  
  75. if (element.textContent !== undefined) {
  76. element.textContent = replacedText;
  77. } else if (element.innerText !== undefined) {
  78. element.innerText = replacedText;
  79. }
  80. }
  81.  
  82. });
  83. });
  84.  
  85.  
  86. }
  87.  
  88.  
  89. const targetNode = document.querySelectorAll('html')[0]; // 替换为您的组件容器节点
  90. const observerOptions = {
  91. childList: true, // 监听子节点的添加或删除
  92. subtree: true, // 递归监听目标节点及其所有后代节点
  93. //attributes: true, //目标节点的属性变化
  94. };
  95. const callback = (mutationsList) => {
  96. for (const mutation of mutationsList) {
  97. if (mutation.type === 'childList') {
  98. // console.log('组件重新渲染');
  99. // 在这里执行您希望在组件重新渲染时运行的函数
  100. replaceNullStringsInElfjSElements()
  101. }
  102. }
  103. };
  104.  
  105. const observer = new MutationObserver(callback);
  106. observer.observe(targetNode, observerOptions);
  107.  
  108. // 当不再需要观察时,记得停止观察并释放资源
  109. // observer.disconnect();
  110.  
  111.  
  112. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址