Automatically Refresh Webpage

Auto-reload a specific webpage at a configurable interval

  1. // ==UserScript==
  2. // @name Automatically Refresh Webpage
  3. // @namespace Automatically Refresh Webpage
  4. // @description Auto-reload a specific webpage at a configurable interval
  5. // @version 1.7
  6. // @author aciid
  7. // @match http://ENTER_YOUR_OWN_URL_OR_IP_HERE/*
  8. // @supportURL https://greasyfork.org/en/scripts/491178
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let intervalId = null;
  18. let wasPaused = false; // Indicates if the script was paused due to hovering
  19. let isScriptRunning = false; // Tracks whether the script is actively running or stopped
  20.  
  21. // Function to start reloading
  22. function startReloading(interval) {
  23. console.log(`Starting reload every ${interval / 1000} seconds.`);
  24. if (intervalId !== null) clearInterval(intervalId); // Clear existing interval if any
  25. intervalId = setInterval(() => {
  26. console.log("Reloading page.");
  27. window.location.reload();
  28. }, interval);
  29. isScriptRunning = true;
  30. updateStatus('Running');
  31. }
  32.  
  33. // Function to stop reloading
  34. function stopReloading() {
  35. if (intervalId !== null) {
  36. clearInterval(intervalId);
  37. console.log("Reload stopped.");
  38. intervalId = null;
  39. }
  40. isScriptRunning = false;
  41. updateStatus('Stopped');
  42. }
  43.  
  44. // Function to update status displayed in UI
  45. function updateStatus(status) {
  46. document.getElementById('reloadStatus').textContent = `Status: ${status}`;
  47. }
  48.  
  49. // Load settings from localStorage
  50. const savedInterval = localStorage.getItem('autoReloadInterval') || 30000; // Default to 30 seconds
  51. let isReloadingEnabled = localStorage.getItem('isReloadingEnabled') === 'true';
  52.  
  53. // UI setup
  54. const reloadUI = document.createElement('div');
  55. reloadUI.style = 'position: fixed; bottom: 20px; right: 20px; z-index: 10000; padding: 15px; background-color: #f0f0f0; border: 2px solid #ccc; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); font-family: Arial, sans-serif;';
  56. reloadUI.innerHTML = `
  57. <div style="font-size: 14px; margin-bottom: 8px;"><strong>Refresh Interval (seconds):</strong></div>
  58. <div style="margin-bottom: 8px;">
  59. <input type="number" id="reloadInterval" value="${savedInterval / 1000}" min="1" style="width: 60px; padding: 5px;">
  60. </div>
  61. <div style="margin-bottom: 8px;">
  62. <button id="startReload" style="padding: 5px 10px;">Start</button>
  63. <button id="stopReload" style="padding: 5px 10px;">Stop</button>
  64. </div>
  65. <div id="reloadStatus" style="font-size: 12px;">Status: ${isReloadingEnabled ? 'Running' : 'Stopped'}</div>
  66. `;
  67. document.body.appendChild(reloadUI);
  68.  
  69. // Pause logic on hover
  70. reloadUI.addEventListener('mouseenter', function() {
  71. if (intervalId !== null && isScriptRunning) { // Only pause if it's currently running
  72. clearInterval(intervalId);
  73. intervalId = null;
  74. wasPaused = true; // Mark as paused due to hover
  75. updateStatus('Paused (mouse on UI)');
  76. }
  77. });
  78.  
  79. // Unpause logic
  80. reloadUI.addEventListener('mouseleave', function() {
  81. if (wasPaused && isScriptRunning) { // Only unpause if it was paused due to hover and the script was running before
  82. const interval = Math.max(1, parseInt(document.getElementById('reloadInterval').value, 10)) * 1000;
  83. startReloading(interval);
  84. wasPaused = false; // Reset pause state
  85. }
  86. });
  87.  
  88. // Start/Stop buttons
  89. const startButton = document.getElementById('startReload');
  90. const stopButton = document.getElementById('stopReload');
  91. const intervalInput = document.getElementById('reloadInterval');
  92.  
  93. startButton.onclick = function() {
  94. const interval = Math.max(1, parseInt(intervalInput.value, 10)) * 1000;
  95. localStorage.setItem('autoReloadInterval', interval);
  96. localStorage.setItem('isReloadingEnabled', 'true');
  97. isReloadingEnabled = true; // Update the running state
  98. startReloading(interval);
  99. };
  100.  
  101. stopButton.onclick = function() {
  102. localStorage.setItem('isReloadingEnabled', 'false');
  103. isReloadingEnabled = false; // Ensure the script is marked as not running
  104. stopReloading();
  105. };
  106.  
  107. // Automatically start reloading if enabled
  108. if (isReloadingEnabled) {
  109. startReloading(savedInterval);
  110. }
  111. })();