leetcode自动计时

try to take over the world!

  1. // ==UserScript==
  2. // @name leetcode自动计时
  3. // @license MIT
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.2
  6. // @description try to take over the world!
  7. // @author You
  8. // @match *://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. console.log("Waiting for .fa-arrows-rotate or .fa-alarm-clock to appear...");
  18. restartTimer();
  19.  
  20. function clickElementWithRetry(selector) {
  21. const element = document.querySelector(selector);
  22. if (element){
  23. console.log(`点击 ${selector}`);
  24. element.parentNode.click();
  25. return new Promise( (resolve, reject) => {
  26. resolve();
  27. });
  28. }
  29. return new Promise((resolve, reject) => {
  30. setTimeout(() => {
  31. console.log(`开始执行:选择器为 ${selector}`);
  32. let retryCount = 0;
  33. let intervalId = setInterval(function() {
  34. retryCount++;
  35.  
  36. // 在这里执行需要重试的操作
  37. const element = document.querySelector(selector);
  38. console.log(`重试第 ${retryCount} 次,选择器为 ${selector}`);
  39.  
  40. // 如果查询到非空元素或者重试次数达到50次,则停止重试
  41. if (element || retryCount >= 50) {
  42. clearInterval(intervalId);
  43. if (element) {
  44. console.log(`点击 ${selector},退出循环`);
  45. element.parentNode.click();
  46. } else {
  47. console.log("重试结束,未找到元素或达到最大重试次数。");
  48. }
  49. }
  50. }, 200);
  51.  
  52. resolve();
  53. }, 2000);
  54. });
  55.  
  56. }
  57.  
  58. function restartTimer(){
  59. clickElementWithRetry('.fa-alarm-clock');
  60. clickElementWithRetry('.fa-arrows-rotate').then(() => {
  61. clickElementWithRetry('.fa-circle-play');
  62. });
  63. }
  64.  
  65. // "Ctrl + ;" 重新计时
  66. document.addEventListener('keydown', function(event) {
  67. if (event.ctrlKey && event.key === ';') {
  68. restartTimer();
  69.  
  70. const messageElement = document.createElement('div');
  71. messageElement.textContent = '重新计时!';
  72. messageElement.style.position = 'fixed';
  73. messageElement.style.top = '50%';
  74. messageElement.style.left = '50%';
  75. messageElement.style.transform = 'translate(-50%, -50%)';
  76. messageElement.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  77. messageElement.style.color = 'white';
  78. messageElement.style.padding = '10px';
  79. messageElement.style.borderRadius = '5px';
  80. messageElement.style.transition = 'opacity 0.5s';
  81.  
  82. // 插入到页面中
  83. document.body.appendChild(messageElement);
  84.  
  85. // 逐渐淡化并在 200ms 后消失
  86. setTimeout(() => {
  87. messageElement.style.opacity = '0';
  88. setTimeout(() => {
  89. document.body.removeChild(messageElement);
  90. }, 500); // 500ms 后移除元素
  91. }, 200); // 200ms 后开始淡化
  92.  
  93. }
  94. });
  95.  
  96. })();

QingJ © 2025

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