当前位置: 首页 > news >正文

油猴脚本-自动刷新网页

// ==UserScript==
// @name         Userscript_reload
// @namespace    http://tampermonkey.net/
// @version      2025-09-29
// @description  每x分钟自动刷新当前页面,可手动暂停/继续,支持窗口拖动
// @author       You
// @match        https://www.baidu.com/*
// @grant        none
// ==/UserScript==(function() {'use strict';// 刷新间隔:x分钟 +(1--10)随机秒const REFRESH_INTERVAL =(5*60+Math.floor(Math.random() * 11))*1000;let timer = null;let remainingTime = REFRESH_INTERVAL;let isRunning = true;let isDragging = false;let offsetX, offsetY;// 创建控制界面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'; // 状态区恢复默认光标// 控制按钮const toggleBtn = document.createElement('button');toggleBtn.textContent = '暂停';toggleBtn.style.marginLeft = '10px';toggleBtn.style.padding = '3px 8px';toggleBtn.style.cursor = 'pointer';toggleBtn.style.border = 'none';toggleBtn.style.borderRadius = '3px';toggleBtn.style.backgroundColor = '#ff4444';toggleBtn.style.color = 'white';toggleBtn.style.cursor = 'default'; // 按钮恢复默认光标toggleBtn.addEventListener('click', toggleRefresh);// 组装面板panel.appendChild(header);panel.appendChild(statusDiv);panel.appendChild(toggleBtn);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`;// 清除原来的bottom和right属性,改用left和toppanel.style.bottom = 'auto';panel.style.right = 'auto';});// 鼠标释放时停止拖动document.addEventListener('mouseup', () => {if (isDragging) {isDragging = false;const panel = document.getElementById('autoRefreshPanel');panel.style.transition = 'all 0.1s ease'; // 恢复轻微动画}});// 鼠标离开窗口时停止拖动document.addEventListener('mouseleave', () => {isDragging = false;const panel = document.getElementById('autoRefreshPanel');panel.style.transition = 'all 0.1s ease';});}// 格式化时间显示 (毫秒 -> 分:秒)function formatTime(ms) {const totalSeconds = Math.floor(ms / 1000);const minutes = Math.floor(totalSeconds / 60);const seconds = totalSeconds % 60;return `${minutes}:${seconds.toString().padStart(2, '0')}`;}// 更新倒计时显示function updateDisplay() {const statusDiv = document.getElementById('autoRefreshStatus');if (statusDiv) {statusDiv.textContent = `自动刷新: ${isRunning ? '启用' : '暂停'} (剩余: ${formatTime(remainingTime)})`;}}// 切换刷新状态 (暂停/继续)function toggleRefresh() {isRunning = !isRunning;const btn = document.querySelector('#autoRefreshPanel button');if (isRunning) {btn.textContent = '暂停';btn.style.backgroundColor = '#ff4444';startTimer();} else {btn.textContent = '继续';btn.style.backgroundColor = '#00C851';clearInterval(timer);}updateDisplay();}// 开始倒计时function startTimer() {clearInterval(timer);timer = setInterval(() => {if (isRunning) {remainingTime -= 1000;updateDisplay();if (remainingTime <= 0) {// 刷新页面location.reload();}}}, 1000);}// 初始化createControlPanel();startTimer();// 页面卸载时清除定时器window.addEventListener('beforeunload', () => {clearInterval(timer);});
})();

  

http://www.hskmm.com/?act=detail&tid=20713

相关文章:

  • PostgreSQL数据库查询表是否被锁,以及解锁表的办法
  • 用信号量机制实现互斥,同步,前驱
  • 详细介绍:HDFS和MapReduce——Hadoop的两大核心技
  • 【AI 哲学思考】从大模型演进到生命隐喻:个性、极限与先天后天之问
  • 【AI 哲学思考】记忆的形态:从人脑到 AI 的存储之问
  • ISP DMA TEST
  • 三脚电感在报警器芯片里的实际作用与用法
  • 洛谷题单指南-进阶数论-P5091 【模板】扩展欧拉定理
  • jenkins maven nacos springboot profile实现多环境配置
  • RAG is really dead? 大模型和知识之间的桥梁没了? - spader
  • opencv学习记录4
  • 深入解析:Java-136 深入浅出 MySQL Spring Boot @Transactional 使用指南:事务传播、隔离级别与异常回滚策略
  • .NET操作Excel:高效材料读写与批量运行
  • Qwen-Image技术报告
  • IOS-和安卓-AR-游戏开发指南-全-
  • Winform/C# 输出到Release VS中Release模式下生成去掉生成pdb文件
  • 【OpenCV】12 图像轮廓
  • IntroJS-即时入门-全-
  • 数字设计的新篇章:前沿技术与未来趋势
  • 2025 镀锌方管厂家最新权威推荐排行榜:聚焦行业标杆与新锐品牌,镀锌方管优质厂家深度解析
  • mysql启动方式导致链接数max_connections查询的值不一致
  • cmakelist
  • 供应商协同平台:打造高效安全供应链的关键
  • 互斥锁和信号量机制
  • NSIS为当前用户安装和为所有用户安装的选择
  • 数据中台厂商选型|解决方案厂商与独立中台厂商详细解读
  • 深度学习项目全流程实践与核心技术解析:从数据处理到模型优化 - 教程
  • 直接使用的NLog帮助类
  • 【每日一面】setTimeout 延时为 0 的情况
  • AI元人文:悟空博弈框架