网页自动刷新面板

自动取消勾选的递减次数刷新工具,状态持久化存储

  1. // ==UserScript==
  2. // @name 网页自动刷新面板
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.2
  5. // @description 自动取消勾选的递减次数刷新工具,状态持久化存储
  6. // @author 明灯花月夜
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const panel = document.createElement('div');
  16. panel.style.position = 'fixed';
  17. panel.style.bottom = '20px';
  18. panel.style.right = '20px';
  19. panel.style.padding = '15px';
  20. panel.style.background = '#f5f5f5';
  21. panel.style.border = '1px solid #ddd';
  22. panel.style.borderRadius = '6px';
  23. panel.style.boxShadow = '0 2px 12px rgba(0,0,0,0.1)';
  24. panel.style.zIndex = '99999';
  25. panel.style.maxWidth = '250px';
  26.  
  27. // 创建带验证的输入框 [[8]]
  28. const createInput = (id, label, defaultValue) => {
  29. const container = document.createElement('div');
  30. container.style.margin = '8px 0';
  31. const input = document.createElement('input');
  32. input.type = 'number';
  33. input.min = '0';
  34. input.style.width = '60px';
  35. input.style.marginLeft = '8px';
  36. input.value = localStorage.getItem(id) || defaultValue;
  37. input.addEventListener('input', () => {
  38. input.value = Math.max(0, parseInt(input.value) || 0);
  39. });
  40.  
  41. container.appendChild(document.createTextNode(label));
  42. container.appendChild(input);
  43. return { container, input };
  44. };
  45.  
  46. const checkbox = document.createElement('input');
  47. checkbox.type = 'checkbox';
  48. checkbox.id = 'auto-refresh-checkbox';
  49.  
  50. const label = document.createElement('label');
  51. label.htmlFor = 'auto-refresh-checkbox';
  52. label.appendChild(document.createTextNode('启用自动刷新'));
  53.  
  54. const { container: delayContainer, input: delayInput } =
  55. createInput('refreshDelay', '延迟(秒):', 5);
  56. const { container: countContainer, input: countInput } =
  57. createInput('refreshCount', '总次数(0=无限):', 0);
  58.  
  59. panel.appendChild(checkbox);
  60. panel.appendChild(label);
  61. panel.appendChild(delayContainer);
  62. panel.appendChild(countContainer);
  63. document.body.appendChild(panel);
  64.  
  65. // 加载状态 [[5]]
  66. let remainingCount = parseInt(localStorage.getItem('refreshRemaining')) || 0;
  67. const savedEnabled = localStorage.getItem('autoRefreshEnabled') === 'true';
  68. checkbox.checked = savedEnabled && (remainingCount > 0 || countInput.value == 0);
  69.  
  70. let refreshTimer;
  71.  
  72. // 启动刷新逻辑 [[9]]
  73. function startRefresh() {
  74. const total = parseInt(countInput.value) || 0;
  75. const delay = parseInt(delayInput.value) || 5;
  76. // 初始化剩余次数 [[7]]
  77. remainingCount = total === 0 ? 0 : (total || 0);
  78. localStorage.setItem('refreshRemaining', remainingCount);
  79. if (refreshTimer) clearTimeout(refreshTimer);
  80. function refreshLoop() {
  81. if (!checkbox.checked) return;
  82.  
  83. // 次数控制逻辑 [[2]]
  84. if (total > 0 && remainingCount <= 0) {
  85. stopRefresh();
  86. return;
  87. }
  88.  
  89. if (total > 0) {
  90. remainingCount--;
  91. localStorage.setItem('refreshRemaining', remainingCount);
  92. countInput.value = remainingCount; // 实时更新显示
  93. }
  94.  
  95. // 最后一次刷新时取消勾选 [[6]]
  96. if (total > 0 && remainingCount === 0) {
  97. checkbox.checked = false;
  98. localStorage.setItem('autoRefreshEnabled', false);
  99. }
  100.  
  101. window.location.reload();
  102. }
  103.  
  104. // 使用setTimeout实现精确控制 [[9]]
  105. refreshTimer = setTimeout(() => {
  106. refreshLoop();
  107. if (checkbox.checked) {
  108. setTimeout(arguments.callee, delay * 1000);
  109. }
  110. }, delay * 1000);
  111. }
  112.  
  113. function stopRefresh() {
  114. clearTimeout(refreshTimer);
  115. checkbox.checked = false;
  116. localStorage.setItem('autoRefreshEnabled', false);
  117. }
  118.  
  119. // 复选框事件 [[7]]
  120. checkbox.addEventListener('change', (e) => {
  121. localStorage.setItem('autoRefreshEnabled', e.target.checked);
  122. if (e.target.checked) {
  123. localStorage.setItem('refreshDelay', delayInput.value);
  124. localStorage.setItem('refreshCount', countInput.value);
  125. startRefresh();
  126. } else {
  127. stopRefresh();
  128. }
  129. });
  130.  
  131. // 输入变化处理 [[5]]
  132. const saveInput = (input, key) => {
  133. input.addEventListener('change', () => {
  134. const value = parseInt(input.value) || 0;
  135. localStorage.setItem(key, value);
  136. if (checkbox.checked) {
  137. stopRefresh();
  138. startRefresh();
  139. }
  140. });
  141. };
  142.  
  143. saveInput(delayInput, 'refreshDelay');
  144. saveInput(countInput, 'refreshCount');
  145.  
  146. // 初始化检查 [[7]]
  147. if (checkbox.checked) {
  148. // 恢复剩余次数显示
  149. countInput.value = remainingCount > 0 ? remainingCount : countInput.value;
  150. startRefresh();
  151. }
  152. })();

QingJ © 2025

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