Smart Page Auto Refresh

Smoothly refreshes web pages when pressing Ctrl+R

当前为 2024-11-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Smart Page Auto Refresh
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5
  5. // @description Smoothly refreshes web pages when pressing Ctrl+R
  6. // @author kequn yang
  7. // @match *://*
  8. // @match file:///*
  9. // @match http://127.0.0.1:*/*
  10. // @match http://localhost:*/*
  11. // @grant GM_addStyle
  12. // @run-at document-end
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. const REFRESH_INTERVAL = 1000;
  19. const STORAGE_KEY = 'autoRefreshEnabled';
  20. let isRefreshing = false;
  21. let refreshTimer = null;
  22. let lastContent = '';
  23.  
  24. // 打印使用说明
  25. console.log(`%cSmart Page Auto Refresh Instructions`, 'font-size: 16px; font-weight: bold; color: #2196F3');
  26. console.log(`%cAutomatically refresh web pages. Press Ctrl+R to start/stop page refresh.`, 'color: #4CAF50');
  27. console.log(`\n%cFor CORS problem of local file, start Chrome with these parameters:`, 'color: #FF5722');
  28. console.log(`%c# MacOS`, 'color: #9C27B0');
  29. console.log(`open -a "Google Chrome" --args --allow-file-access-from-files --disable-web-security --user-data-dir="~/ChromeDevSession"`);
  30. console.log(`\n%c# Windows`, 'color: #9C27B0');
  31. console.log(`chrome.exe --allow-file-access-from-files --disable-web-security --user-data-dir=C:\\ChromeDevSession`);
  32. console.log(`\n%c# Linux`, 'color: #9C27B0');
  33. console.log(`google-chrome --allow-file-access-from-files --disable-web-security --user-data-dir="~/ChromeDevSession"`);
  34.  
  35. function isLocalFile() {
  36. return window.location.protocol === 'file:';
  37. }
  38.  
  39. async function checkForChanges() {
  40. if (!isRefreshing) return;
  41.  
  42. try {
  43. const response = await fetch(window.location.href, {
  44. cache: 'no-store' // 禁用缓存,确保获取最新内容
  45. });
  46.  
  47. if (!response.ok) throw new Error('Network response was not ok');
  48. const newContent = await response.text();
  49.  
  50. // 仅在内容真正改变时才刷新
  51. if (newContent !== lastContent) {
  52. console.log('Content changed, refreshing...');
  53. lastContent = newContent;
  54. window.location.reload(); // 直接刷新页面
  55. }
  56. } catch (error) {
  57. console.error('Refresh error:', error);
  58. stopAutoRefresh(); // 发生错误时停止刷新
  59. }
  60. }
  61.  
  62. function startAutoRefresh() {
  63. if (isRefreshing) return;
  64.  
  65. // 存储初始内容
  66. fetch(window.location.href, {
  67. cache: 'no-store'
  68. }).then(response => response.text())
  69. .then(content => {
  70. lastContent = content;
  71. isRefreshing = true;
  72. localStorage.setItem(STORAGE_KEY, 'true');
  73.  
  74. if (refreshTimer) {
  75. clearInterval(refreshTimer);
  76. }
  77.  
  78. refreshTimer = setInterval(checkForChanges, REFRESH_INTERVAL);
  79. console.log('Auto refresh started');
  80. })
  81. .catch(error => {
  82. console.error('Error starting auto refresh:', error);
  83. });
  84. }
  85.  
  86. function stopAutoRefresh() {
  87. if (!isRefreshing) return;
  88.  
  89. isRefreshing = false;
  90. localStorage.setItem(STORAGE_KEY, 'false');
  91.  
  92. if (refreshTimer) {
  93. clearInterval(refreshTimer);
  94. refreshTimer = null;
  95. }
  96.  
  97. console.log('Auto refresh stopped');
  98. }
  99.  
  100. function handleKeyPress(e) {
  101. if (e.ctrlKey && (e.key === 'r' || e.key === 'R')) {
  102. e.preventDefault();
  103. if (isRefreshing) {
  104. stopAutoRefresh();
  105. } else {
  106. startAutoRefresh();
  107. }
  108. }
  109. }
  110.  
  111. // 初始化
  112. function initialize() {
  113. document.addEventListener('keydown', handleKeyPress);
  114.  
  115. // 获取保存的状态
  116. const savedState = localStorage.getItem(STORAGE_KEY);
  117. if (savedState === 'true') {
  118. startAutoRefresh();
  119. }
  120. }
  121.  
  122. // 运行初始化
  123. initialize();
  124.  
  125. // 在页面卸载前清理
  126. window.addEventListener('unload', () => {
  127. if (refreshTimer) {
  128. clearInterval(refreshTimer);
  129. }
  130. });
  131. })();