// ==UserScript== // @name 湛江霞山区干部在线学习挂机助手 // @namespace http://tampermonkey.net/ // @version 0.5 // @description 当网页最小化或切换到其他标签页时,视频仍然继续播放,放心去搬砖 // @author Monkey // @match https://xs.zjgbzx.gov.cn/ // @grant GM_notification // @grant GM_setValue // @grant GM_getValue // ==/UserScript== (function() { 'use strict'; // 显示10秒后自动消失的启动通知 function showStartNotification() { // 使用GM_notification API如果可用 if (typeof GM_notification !== 'undefined') { const notification = GM_notification({ title: '湛江霞山区干部在线挂机学习助手已启动', text: '可以最小化浏览器去搬砖啦!\n视频会继续播放~', timeout: 0, // 手动控制关闭 onclick: () => { /* 点击处理 */ } }); // 10秒后自动关闭 setTimeout(() => { if (notification && notification.remove) { notification.remove(); } }, 10000); } else { // 备用方案:创建自定义弹窗 createCustomPopup(); } } // 创建自定义弹窗 function createCustomPopup() { const popup = document.createElement('div'); popup.style.position = 'fixed'; popup.style.top = '20px'; popup.style.right = '20px'; popup.style.backgroundColor = '#4CAF50'; popup.style.color = 'white'; popup.style.padding = '15px'; popup.style.borderRadius = '5px'; popup.style.boxShadow = '0 4px 8px rgba(0,0,0,0.2)'; popup.style.zIndex = '999999'; popup.style.maxWidth = '300px'; popup.style.transition = 'opacity 0.5s'; popup.innerHTML = `

湛江霞山区干部在线挂机学习助手已启动

可以最小化浏览器去搬砖啦!
视频会继续播放~

`; document.body.appendChild(popup); // 倒计时动画 const countdown = popup.querySelector('#countdown'); let width = 100; const interval = setInterval(() => { width -= 1; countdown.style.width = width + '%'; if (width <= 0) { clearInterval(interval); popup.style.opacity = '0'; setTimeout(() => popup.remove(), 500); } }, 100); // 10秒后自动关闭 setTimeout(() => { popup.style.opacity = '0'; setTimeout(() => popup.remove(), 500); }, 10000); // 点击立即关闭 popup.addEventListener('click', () => { clearInterval(interval); popup.style.opacity = '0'; setTimeout(() => popup.remove(), 500); }); } // 只处理有用户交互的视频 const userInteractedVideos = new WeakSet(); function handleVideoInteraction(e) { userInteractedVideos.add(e.target); e.target.removeEventListener('play', handleVideoInteraction); } // 检测页面可见性变化 function handleVisibilityChange() { const videos = document.querySelectorAll('video'); if (document.hidden) { // 页面被隐藏 videos.forEach(video => { if (!video.paused && userInteractedVideos.has(video)) { video.setAttribute('data-was-playing', 'true'); } }); } else { // 页面变为可见 videos.forEach(video => { if (video.getAttribute('data-was-playing') === 'true' && userInteractedVideos.has(video)) { video.play().catch(e => console.debug('自动播放失败:', e.message)); video.removeAttribute('data-was-playing'); } }); } } // 防止页面失去焦点时暂停 function preventPauseOnBlur() { document.querySelectorAll('video').forEach(video => { // 标记用户交互过的视频 if (!userInteractedVideos.has(video)) { video.addEventListener('play', handleVideoInteraction); } // 防止自动暂停 video.addEventListener('pause', function(e) { if (document.hidden && !video.ended && !video.seeking && userInteractedVideos.has(video)) { video.play().catch(e => console.debug('阻止暂停失败:', e.message)); } }); }); } // 初始化 function init() { // 显示启动通知 showStartNotification(); // 监听页面可见性变化 document.addEventListener('visibilitychange', handleVisibilityChange); // 处理现有视频 preventPauseOnBlur(); // 监听新添加的视频元素 const observer = new MutationObserver(function(mutations) { let videosAdded = false; mutations.forEach(function(mutation) { if (mutation.addedNodes.length) { videosAdded = true; } }); if (videosAdded) { preventPauseOnBlur(); } }); observer.observe(document.body, { childList: true, subtree: true }); console.debug('网页最小化继续播放脚本已加载'); } // 延迟初始化以确保页面加载 if (document.readyState === 'complete') { init(); } else { window.addEventListener('load', init); } })();