// ==UserScript== // @name 全能防切屏防暂停增强版 // @namespace http://tampermonkey.net/ // @version 3.0 // @description 屏蔽切屏焦点检测,定时模拟鼠标和键盘活动防挂机,去除自动连播 // @author You // @match *://*/* // @grant none // @run-at document-start // ==/UserScript== (function() { 'use strict'; console.log("🚀 防切屏防挂机脚本启动中..."); // ==================== 1. 篡改页面可见性 API ==================== // 覆盖 document.hidden Object.defineProperty(document, 'hidden', { get: function() { return false; }, configurable: true }); // 覆盖 document.visibilityState Object.defineProperty(document, 'visibilityState', { get: function() { return 'visible'; }, configurable: true }); // 覆盖 hasFocus const originalHasFocus = document.hasFocus; document.hasFocus = function() { return true; }; // ==================== 2. 拦截各类失焦切屏检测事件 ==================== const originAddEventListener = EventTarget.prototype.addEventListener; const bannedEvents = [ 'visibilitychange', 'blur', 'focusout', 'pagehide', 'pageshow', 'mouseleave', 'mouseout', 'focus', 'focusin' ]; EventTarget.prototype.addEventListener = function(type, listener, options) { // 拦截被禁止的事件 if (bannedEvents.includes(type)) { console.log(`🛡️ 已拦截事件监听: ${type}`); return; } return originAddEventListener.call(this, type, listener, options); }; // 拦截 window.addEventListener 的特殊处理 const windowAddEventListener = window.addEventListener; window.addEventListener = function(type, listener, options) { if (bannedEvents.includes(type)) { console.log(`🛡️ 已拦截 window 事件: ${type}`); return; } return windowAddEventListener.call(this, type, listener, options); }; // 拦截 document.addEventListener const documentAddEventListener = document.addEventListener; document.addEventListener = function(type, listener, options) { if (bannedEvents.includes(type)) { console.log(`🛡️ 已拦截 document 事件: ${type}`); return; } return documentAddEventListener.call(this, type, listener, options); }; // 清空全局焦点监听 window.onblur = null; window.onfocus = null; document.onvisibilitychange = null; document.onblur = null; document.onfocus = null; // 阻止 focus/blur 事件的默认行为 window.addEventListener('blur', function(e) { e.stopImmediatePropagation(); e.preventDefault(); }, true); // ==================== 3. 模拟真实用户活动 ==================== let lastActivityTime = Date.now(); // 生成随机数 function random(min, max) { return Math.random() * (max - min) + min; } // 模拟鼠标移动 function simulateMouseMove() { const x = random(100, window.innerWidth - 100); const y = random(100, window.innerHeight - 100); const moveEvent = new MouseEvent('mousemove', { clientX: x, clientY: y, screenX: x + window.screenX, screenY: y + window.screenY, bubbles: true, cancelable: true, view: window }); document.dispatchEvent(moveEvent); lastActivityTime = Date.now(); console.log(`🖱️ 模拟鼠标移动: (${Math.round(x)}, ${Math.round(y)})`); } // 模拟鼠标点击 function simulateMouseClick() { const x = random(100, window.innerWidth - 100); const y = random(100, window.innerHeight - 100); const downEvent = new MouseEvent('mousedown', { clientX: x, clientY: y, bubbles: true, cancelable: true, button: 0, buttons: 1, view: window }); const upEvent = new MouseEvent('mouseup', { clientX: x, clientY: y, bubbles: true, cancelable: true, button: 0, buttons: 0, view: window }); const clickEvent = new MouseEvent('click', { clientX: x, clientY: y, bubbles: true, cancelable: true, button: 0, view: window }); document.dispatchEvent(downEvent); setTimeout(() => { document.dispatchEvent(upEvent); document.dispatchEvent(clickEvent); }, random(50, 150)); lastActivityTime = Date.now(); console.log(`🖱️ 模拟鼠标点击: (${Math.round(x)}, ${Math.round(y)})`); } // 模拟键盘按键 function simulateKeyPress() { const keys = ['ArrowRight', 'ArrowLeft', ' ', 'Enter']; const key = keys[Math.floor(random(0, keys.length))]; const downEvent = new KeyboardEvent('keydown', { key: key, code: key === ' ' ? 'Space' : key, bubbles: true, cancelable: true, view: window }); const upEvent = new KeyboardEvent('keyup', { key: key, code: key === ' ' ? 'Space' : key, bubbles: true, cancelable: true, view: window }); document.dispatchEvent(downEvent); setTimeout(() => { document.dispatchEvent(upEvent); }, random(50, 100)); lastActivityTime = Date.now(); console.log(`⌨️ 模拟键盘按键: ${key === ' ' ? 'Space' : key}`); } // 模拟滚动 function simulateScroll() { const scrollAmount = random(-100, 100); window.scrollBy(0, scrollAmount); setTimeout(() => { window.scrollBy(0, -scrollAmount); }, random(500, 1000)); lastActivityTime = Date.now(); console.log(`📜 模拟页面滚动`); } // 综合模拟用户活动 function simulateUserActivity() { const activities = [ simulateMouseMove, simulateMouseClick, simulateKeyPress, simulateScroll ]; // 随机选择1-2个活动 const numActivities = Math.floor(random(1, 3)); for (let i = 0; i < numActivities; i++) { const activity = activities[Math.floor(random(0, activities.length))]; setTimeout(activity, random(0, 500)); } } // ==================== 4. 定时执行活动模拟 ==================== // 使用随机间隔,避免被检测 function scheduleNextActivity() { const interval = random(8000, 20000); // 8-20秒随机间隔 setTimeout(() => { simulateUserActivity(); scheduleNextActivity(); }, interval); } // 启动定时任务 scheduleNextActivity(); // 立即执行一次 simulateUserActivity(); // ==================== 5. 保持视频播放 ==================== function keepVideoPlaying() { const videos = document.querySelectorAll('video'); videos.forEach(video => { if (video.paused) { video.play().catch(e => console.log('视频播放被阻止:', e)); } // 移除暂停监听 video.onpause = null; }); } // 每秒检查一次视频状态 setInterval(keepVideoPlaying, 1000); // ==================== 6. 拦截页面离开提示 ==================== window.onbeforeunload = null; // ==================== 7. 防止页面休眠 ==================== // 使用 Web Worker 保持页面活跃 const workerScript = ` setInterval(() => { self.postMessage('keep-alive'); }, 5000); `; const blob = new Blob([workerScript], { type: 'application/javascript' }); const worker = new Worker(URL.createObjectURL(blob)); // ==================== 8. 覆盖 requestAnimationFrame ==================== // 确保即使页面在后台,动画帧也能正常执行 const originalRAF = window.requestAnimationFrame; window.requestAnimationFrame = function(callback) { return originalRAF.call(window, callback); }; // ==================== 9. 模拟窗口焦点 ==================== // 尝试覆盖 window.document 的 focused 状态(如果可配置) try { const originalDocument = window.document; Object.defineProperty(window, 'document', { get: function() { return originalDocument; }, configurable: true }); } catch (e) { console.log('⚠️ window.document 无法重定义,跳过此步骤'); } // ==================== 10. 控制台提示 ==================== console.log("✅ 防切屏+防挂机脚本已生效!"); console.log("📋 功能列表:"); console.log(" • 页面可见性篡改 - 已启用"); console.log(" • 焦点事件拦截 - 已启用"); console.log(" • 鼠标活动模拟 - 已启用 (8-20秒随机间隔)"); console.log(" • 键盘活动模拟 - 已启用"); console.log(" • 视频自动播放 - 已启用"); console.log(" • 后台运行保持 - 已启用"); // 创建可视化指示器 const indicator = document.createElement('div'); indicator.style.cssText = ` position: fixed; bottom: 10px; right: 10px; background: rgba(0, 200, 0, 0.8); color: white; padding: 5px 10px; border-radius: 5px; font-size: 12px; z-index: 999999; font-family: monospace; `; indicator.textContent = '🛡️ 防暂停运行中'; document.body.appendChild(indicator); })();