Anti-Idle Script

防止网页挂机的脚本,检测用户无操作时自动刷新或提示

  1. // ==UserScript==
  2. // @name Anti-Idle Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description 防止网页挂机的脚本,检测用户无操作时自动刷新或提示
  6. // @author wooluo
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 设置无操作时间阈值(毫秒)
  17. const IDLE_TIME_THRESHOLD = 300000; // 5分钟
  18. let lastActivityTime = Date.now();
  19. let warningShown = false;
  20.  
  21. // 检测用户活动
  22. function updateActivityTime() {
  23. lastActivityTime = Date.now();
  24. warningShown = false;
  25. }
  26.  
  27. // 添加事件监听器
  28. ['mousemove', 'keydown', 'click', 'scroll'].forEach(event => {
  29. window.addEventListener(event, updateActivityTime, true);
  30. });
  31.  
  32. // 模拟用户活动 - 每30秒轻微移动鼠标1像素
  33. setInterval(() => {
  34. const event = new MouseEvent('mousemove', {
  35. view: window,
  36. bubbles: true,
  37. cancelable: true,
  38. clientX: Math.random() * 10,
  39. clientY: Math.random() * 10
  40. });
  41. document.dispatchEvent(event);
  42. }, 30000);
  43. // 定期检查活动状态
  44. setInterval(() => {
  45. const currentTime = Date.now();
  46. const idleTime = currentTime - lastActivityTime;
  47.  
  48. if (idleTime > IDLE_TIME_THRESHOLD) {
  49. if (!warningShown) {
  50. // 显示警告
  51. const confirmed = confirm('检测到您长时间无操作,页面将在30秒后自动刷新!点击确定取消自动刷新。');
  52. if (confirmed) {
  53. lastActivityTime = Date.now(); // 重置活动时间
  54. } else {
  55. warningShown = true;
  56. setTimeout(() => {
  57. window.location.reload();
  58. }, 30000); // 30秒后刷新
  59. }
  60. }
  61. }
  62. }, 60000); // 每分钟检查一次
  63. // 2倍速倒计时功能
  64. setInterval(() => {
  65. const timeElements = document.querySelectorAll('*');
  66. timeElements.forEach(el => {
  67. if (el.textContent.includes('还需') && el.textContent.includes('分钟') && el.textContent.includes('秒')) {
  68. const text = el.textContent;
  69. const matches = text.match(/(\d+)分钟(\d+)秒/);
  70. if (matches) {
  71. let minutes = parseInt(matches[1]);
  72. let seconds = parseInt(matches[2]);
  73. // 计算2倍速后的时间
  74. let totalSeconds = minutes * 60 + seconds;
  75. totalSeconds = Math.max(0, totalSeconds - 2); // 每秒减2实现2倍速
  76. minutes = Math.floor(totalSeconds / 60);
  77. seconds = totalSeconds % 60;
  78. el.textContent = text.replace(/(\d+)分钟(\d+)秒/, `${minutes}分钟${seconds}秒`);
  79. }
  80. }
  81. });
  82. }, 1000);
  83. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址