// ==UserScript== // @name Custom Auto Refresh // @namespace https://github.com/ymhomer/ym_Userscript // @version 2.0 // @description Define custom auto-refresh intervals for different websites, and manage them through a settings menu with options to view, modify, or clear. // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @downloadURL https://update.greasyfork.org/scripts/513942/Custom%20Auto%20Refresh.user.js // @updateURL https://update.greasyfork.org/scripts/513942/Custom%20Auto%20Refresh.meta.js // @license MIT // ==/UserScript== (function () { 'use strict'; const currentUrl = window.location.href; const currentRoot = window.location.hostname; const defaultRefreshInterval = 0; let allRefreshSettings = GM_getValue('allRefreshSettings', {}); let refreshInterval = allRefreshSettings[currentRoot] || defaultRefreshInterval; let refreshTimer; let manageSettingsDialogOpen = false; const startAutoRefresh = () => { if (refreshInterval > 0 && !manageSettingsDialogOpen) { refreshTimer = setInterval(() => { location.reload(); }, refreshInterval * 1000); } }; const stopAutoRefresh = () => { clearInterval(refreshTimer); }; const showDialog = (title, content, onCloseCallback = null) => { const dialog = document.createElement('div'); dialog.innerHTML = `

${title}

${content}
`; document.body.appendChild(dialog); const overlay = dialog.querySelector('.custom-overlay'); overlay.addEventListener('click', () => { document.body.removeChild(dialog); if (onCloseCallback) onCloseCallback(); }); return dialog.querySelector('.custom-dialog'); }; const refreshManageSettingsDialog = (dialog) => { const siteKeys = Object.keys(allRefreshSettings); let tableRows = siteKeys.map(site => ` ${site} ${allRefreshSettings[site]} seconds `).join(''); const dialogContent = ` ${tableRows}
Site Refresh Interval Modify Delete
`; dialog.querySelector('#dialogContent').innerHTML = dialogContent; bindManageSettingsEvents(dialog); }; const openSettingsDialog = (parentDialog = null) => { stopAutoRefresh(); const dialogContent = `
`; const dialog = showDialog("Set Refresh Interval", dialogContent); dialog.querySelector('#saveIntervalBtn').addEventListener('click', () => { const intervalInput = dialog.querySelector('#intervalInput').value; const applyToFullUrl = dialog.querySelector('#fullUrlCheckbox').checked; const targetUrl = applyToFullUrl ? currentUrl : currentRoot; if (intervalInput !== null) { refreshInterval = parseInt(intervalInput, 10); allRefreshSettings[targetUrl] = refreshInterval; GM_setValue('allRefreshSettings', allRefreshSettings); document.body.removeChild(dialog.parentNode); startAutoRefresh(); // Refresh Manage Settings dialog if open if (parentDialog) { refreshManageSettingsDialog(parentDialog); } } }); }; const manageAllSettingsDialog = () => { stopAutoRefresh(); manageSettingsDialogOpen = true; const dialog = showDialog("Manage Settings", '', () => { manageSettingsDialogOpen = false; refreshInterval = allRefreshSettings[currentRoot] || defaultRefreshInterval; startAutoRefresh(); }); refreshManageSettingsDialog(dialog); }; const bindManageSettingsEvents = (dialog) => { dialog.querySelector('#addSettingBtn').addEventListener('click', () => openSettingsDialog(dialog)); dialog.querySelectorAll('.modifyBtn').forEach(button => { button.addEventListener('click', () => { const site = button.getAttribute('data-site'); modifySettingDialog(dialog, site); }); }); dialog.querySelectorAll('.deleteBtn').forEach(button => { button.addEventListener('click', () => { const site = button.getAttribute('data-site'); deleteSetting(dialog, site); }); }); }; const modifySettingDialog = (parentDialog, site) => { const currentInterval = allRefreshSettings[site]; const dialogContent = ` `; const dialog = showDialog("Modify Refresh Interval", dialogContent); dialog.querySelector('#saveModifyBtn').addEventListener('click', () => { const newInterval = dialog.querySelector('#newIntervalInput').value; if (newInterval !== null) { allRefreshSettings[site] = parseInt(newInterval, 10); GM_setValue('allRefreshSettings', allRefreshSettings); document.body.removeChild(dialog.parentNode); refreshManageSettingsDialog(parentDialog); } }); }; const deleteSetting = (parentDialog, site) => { delete allRefreshSettings[site]; GM_setValue('allRefreshSettings', allRefreshSettings); refreshManageSettingsDialog(parentDialog); }; startAutoRefresh(); GM_registerMenuCommand("Manage Settings", manageAllSettingsDialog); })();