Greasy Fork镜像 支持简体中文。

页面自动刷新

带有开始/停止控制的自动刷新脚本

  1. // ==UserScript==
  2. // @name 页面自动刷新
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 带有开始/停止控制的自动刷新脚本
  6. // @author xuexim
  7. // @match *://*/*
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_unregisterMenuCommand
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. const REFRESH_DELAY = 1000; // 刷新间隔(毫秒)
  18. const STORAGE_KEY = 'autoRefreshActive';
  19. let refreshInterval = null;
  20.  
  21. // Toast提示函数
  22. function showToast(msg, duration = 3000) {
  23. const existingToast = document.querySelector('.refresh-toast');
  24. if (existingToast) existingToast.remove();
  25.  
  26. const toast = document.createElement('div');
  27. toast.className = 'refresh-toast';
  28. toast.innerHTML = msg;
  29. toast.style.cssText = `
  30. font-size: .8rem;
  31. color: rgb(255, 255, 255);
  32. background-color: rgba(0, 0, 0, 0.6);
  33. padding: 50px 150px;
  34. border-radius: 4px;
  35. position: fixed;
  36. top: 30%;
  37. left: 50%;
  38. transform: translateX(-50%);
  39. width: 300px;
  40. text-align: center;
  41. transition: opacity 0.3s;
  42. z-index: 10000;
  43. `;
  44. document.body.appendChild(toast);
  45.  
  46. setTimeout(() => {
  47. toast.style.opacity = '0';
  48. setTimeout(() => toast.remove(), 300);
  49. }, duration);
  50. }
  51.  
  52. // 开始刷新
  53. function startRefresh() {
  54. if (refreshInterval) return;
  55.  
  56. GM_setValue(STORAGE_KEY, true); // 保存刷新状态
  57. refreshInterval = setInterval(() => {
  58. window.location.reload();
  59. }, REFRESH_DELAY);
  60.  
  61. showToast(`已开启自动刷新(${REFRESH_DELAY/1000}秒)`, 2000);
  62. updateMenu();
  63. }
  64.  
  65. // 停止刷新
  66. function stopRefresh() {
  67. if (refreshInterval) {
  68. clearInterval(refreshInterval);
  69. refreshInterval = null;
  70. GM_setValue(STORAGE_KEY, false); // 更新刷新状态
  71. showToast('已停止自动刷新', 2000);
  72. updateMenu();
  73. }
  74. }
  75.  
  76. // 更新菜单
  77. function updateMenu() {
  78. // 清除现有菜单
  79. menuIds.forEach(id => GM_unregisterMenuCommand(id));
  80. menuIds = [];
  81.  
  82. // 根据状态添加新菜单
  83. if (GM_getValue(STORAGE_KEY, false)) {
  84. menuIds.push(GM_registerMenuCommand('停止刷新', stopRefresh));
  85. } else {
  86. menuIds.push(GM_registerMenuCommand('开始刷新', startRefresh));
  87. }
  88. }
  89.  
  90. // 初始化
  91. let menuIds = []; // 存储菜单项ID
  92. try {
  93. // 检查保存的状态并恢复
  94. if (GM_getValue(STORAGE_KEY, false)) {
  95. refreshInterval = setInterval(() => {
  96. window.location.reload();
  97. }, REFRESH_DELAY);
  98. }
  99. updateMenu();
  100. } catch (e) {
  101. console.error('刷新脚本初始化失败:', e);
  102. showToast('脚本初始化失败', 3000);
  103. }
  104.  
  105. // 清理
  106. window.addEventListener('unload', () => {
  107. if (refreshInterval && !GM_getValue(STORAGE_KEY, false)) {
  108. clearInterval(refreshInterval);
  109. }
  110. });
  111. })();

QingJ © 2025

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