油猴脚本-自动刷新网页-后台版
因为浏览器切换到后台后JS可能不执行刷新工作,改成用标签实现 类似 <meta http-equiv="Refresh" content="113">
// ==UserScript== // @name Userscript_reload (无暂停功能) // @namespace http://tampermonkey.net/ // @version 2025-09-30 // @description 每x分钟自动刷新当前页面,无暂停功能,支持窗口拖动 // @author You // @match https://www.baidu.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 基础刷新间隔:2分钟 const BASE_REFRESH_MINUTES = 2; // 随机秒数:1-10秒 const RANDOM_SECONDS = Math.floor(Math.random() * 10) + 1; // 总刷新间隔(秒) const REFRESH_INTERVAL = BASE_REFRESH_MINUTES * 60 + RANDOM_SECONDS; let remainingTime = REFRESH_INTERVAL; let isDragging = false; let offsetX, offsetY; let countdownTimer = null; // 创建或更新meta刷新标签 function setMetaRefresh(seconds) { // 先移除可能存在的旧标签 removeMetaRefresh(); if (seconds > 0) { const meta = document.createElement('meta'); meta.httpEquiv = "Refresh"; meta.content = seconds; meta.id = "autoRefreshMeta"; document.head.appendChild(meta); console.log(`已设置自动刷新,${seconds}秒后刷新`); } } // 移除meta刷新标签 function removeMetaRefresh() { const oldMeta = document.getElementById('autoRefreshMeta'); if (oldMeta) { oldMeta.remove(); } } // 创建控制界面 function createControlPanel() { const panel = document.createElement('div'); panel.id = 'autoRefreshPanel'; panel.style.position = 'fixed'; panel.style.bottom = '20px'; panel.style.right = '20px'; panel.style.backgroundColor = 'rgba(0,0,0,0.7)'; panel.style.color = 'white'; panel.style.padding = '10px 15px'; panel.style.borderRadius = '5px'; panel.style.zIndex = '999999'; panel.style.fontFamily = 'Arial, sans-serif'; panel.style.fontSize = '14px'; panel.style.cursor = 'move'; // 标题栏 const header = document.createElement('div'); header.style.marginBottom = '8px'; header.style.fontWeight = 'bold'; header.style.userSelect = 'none'; header.textContent = '自动刷新控制'; // 状态显示 const statusDiv = document.createElement('div'); statusDiv.id = 'autoRefreshStatus'; statusDiv.textContent = `自动刷新: 启用 (剩余: ${formatTime(remainingTime)})`; statusDiv.style.marginBottom = '8px'; statusDiv.style.cursor = 'default'; // 组装面板(已移除暂停按钮) panel.appendChild(header); panel.appendChild(statusDiv); document.body.appendChild(panel); // 添加拖动功能 addDragEvents(panel); } // 添加拖动功能 function addDragEvents(element) { element.addEventListener('mousedown', (e) => { if (e.target === element || e.target === element.firstChild) { isDragging = true; const rect = element.getBoundingClientRect(); offsetX = e.clientX - rect.left; offsetY = e.clientY - rect.top; element.style.transition = 'none'; } }); document.addEventListener('mousemove', (e) => { if (!isDragging) return; const panel = document.getElementById('autoRefreshPanel'); const newX = e.clientX - offsetX; const newY = e.clientY - offsetY; const maxX = window.innerWidth - panel.offsetWidth; const maxY = window.innerHeight - panel.offsetHeight; const constrainedX = Math.max(0, Math.min(newX, maxX)); const constrainedY = Math.max(0, Math.min(newY, maxY)); panel.style.left = `${constrainedX}px`; panel.style.top = `${constrainedY}px`; panel.style.bottom = 'auto'; panel.style.right = 'auto'; }); document.addEventListener('mouseup', () => { if (isDragging) { isDragging = false; document.getElementById('autoRefreshPanel').style.transition = 'all 0.1s ease'; } }); document.addEventListener('mouseleave', () => { isDragging = false; document.getElementById('autoRefreshPanel').style.transition = 'all 0.1s ease'; }); } // 格式化时间显示 function formatTime(seconds) { const minutes = Math.floor(seconds / 60); const secs = seconds % 60; return `${minutes}:${secs.toString().padStart(2, '0')}`; } // 更新显示 function updateDisplay() { const statusDiv = document.getElementById('autoRefreshStatus'); if (statusDiv) { statusDiv.textContent = `自动刷新: 启用 (剩余: ${formatTime(remainingTime)})`; } } // 开始倒计时 function startCountdown() { clearInterval(countdownTimer); countdownTimer = setInterval(() => { remainingTime -= 1; updateDisplay(); // 每10秒更新一次meta标签,确保时间准确性 if (remainingTime % 10 === 0) { setMetaRefresh(remainingTime); } if (remainingTime <= 0) { clearInterval(countdownTimer); } }, 1000); } // 初始化 createControlPanel(); setMetaRefresh(REFRESH_INTERVAL); startCountdown(); // 页面卸载时清理 window.addEventListener('beforeunload', () => { clearInterval(countdownTimer); removeMetaRefresh(); }); })();
浙公网安备 33010602011771号