Anti-Idle Script

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

当前为 2025-05-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Anti-Idle Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  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('系统已开启防多课程同时学习功能,您之前已有正在学习中的课程,是否确定学习该新打开的课程?');
  52. if (confirmed) {
  53. localStorage.setItem('multiCourseAllowed', 'true');
  54. lastActivityTime = Date.now(); // 重置活动时间
  55. } else {
  56. if(localStorage.getItem('multiCourseAllowed') === 'true') {
  57. lastActivityTime = Date.now(); // 如果已允许多开,则重置活动时间
  58. } else {
  59. warningShown = true;
  60. setTimeout(() => {
  61. window.location.reload();
  62. }, 30000); // 30秒后刷新
  63. }
  64. }
  65. }
  66. }
  67. }, 60000); // 每分钟检查一次
  68. // 2倍速倒计时功能
  69. setInterval(() => {
  70. const timeElements = document.querySelectorAll('*');
  71. timeElements.forEach(el => {
  72. if (el.textContent.includes('还需') && el.textContent.includes('分钟') && el.textContent.includes('秒')) {
  73. const text = el.textContent;
  74. const matches = text.match(/(\d+)分钟(\d+)秒/);
  75. if (matches) {
  76. let minutes = parseInt(matches[1]);
  77. let seconds = parseInt(matches[2]);
  78. // 计算2倍速后的时间
  79. let totalSeconds = minutes * 60 + seconds;
  80. totalSeconds = Math.max(0, totalSeconds - 2); // 每秒减2实现2倍速
  81. minutes = Math.floor(totalSeconds / 60);
  82. seconds = totalSeconds % 60;
  83. el.textContent = text.replace(/(\d+)分钟(\d+)秒/, `${minutes}分钟${seconds}秒`);
  84. }
  85. }
  86. });
  87. }, 1000);
  88. })();

QingJ © 2025

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