// ==UserScript== // @name 广二师培训视频自动播放 + 自动关闭弹窗 + 自动下一活动(带状态提示) // @namespace denglao // @version 0.6 // @description 自动播放视频;任何弹窗自动关闭;自动点击下一活动;右下角显示挂机状态 // @author You // @match http://neancts.gdei.edu.cn/* // @grant none // @run-at document-idle // ==/UserScript== (function () { 'use strict'; /* ==================== 配置 ==================== */ const CFG = { scanInterval: 1800, videoInterval: 2500, cooldownAfterAction: 2200, nextCooldown: 3500, moDebounce: 500, debug: true, showHUD: true, // 是否显示右下角状态提示 hudText: '自动挂机学习中......' // 显示的文字 }; const log = (...a) => CFG.debug && console.log('%c[自动助手]', 'color:#0a84ff;font-weight:bold', ...a); let lastActionTime = 0; let lastNextTime = 0; let busy = false; let hudHost = null; /* ==================== 选择器 ==================== */ const POPUP_SELECTOR = [ '[role="dialog"]', '[role="alertdialog"]', '[aria-modal="true"]', '.el-dialog', '.el-message-box', '.ant-modal', '.layui-layer', '.ivu-modal', '.modal', '.modal-dialog', '.modal-content', '.dialog', '.popup', '.swal2-popup', '.weui-dialog', '[class*="dialog" i]', '[class*="modal" i]', '[class*="popup" i]', '[class*="overlay" i]', '[class*="mask" i]' ].join(','); const CLOSE_SELECTOR = [ '[aria-label*="关闭"]', '[aria-label*="close" i]', '[title*="关闭"]', '[title*="close" i]', '.el-dialog__headerbtn', '.ant-modal-close', '.layui-layer-close', '.ivu-modal-close', '.close', '.close-btn', '.modal-close', '[class*="close" i]' ].join(','); const CLOSE_TEXTS = [ '关闭', '關閉', '取消', '稍后', '稍後', '以后再说', '暂不', '不再提示', '跳过', '跳過', '我知道了', '知道了', '确定', '確定', '确认', '好的', '同意', 'OK', 'Ok', 'ok', '继续', '繼續', '继续观看', '开始学习', '开始' ]; const DANGER = /退出|删除|支付|购买|提交|放弃|结束|离开|拒绝|清空|重置/; const NEXT_TEXTS = ['下一活动', '下一活動', '下一个活动']; /* ==================== 工具 ==================== */ function isVisible(el) { if (!el || !el.isConnected) return false; const r = el.getBoundingClientRect(); if (r.width < 4 || r.height < 4) return false; const s = getComputedStyle(el); if (s.display === 'none' || s.visibility === 'hidden') return false; if (parseFloat(s.opacity) < 0.05) return false; return true; } function textOf(el) { if (!el) return ''; return (el.innerText || el.textContent || '').trim().slice(0, 30); } function doClick(el) { if (!el) return; try { el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })); el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })); el.click(); } catch (e) { try { el.click(); } catch (e2) {} } } /* ==================== 右下角 HUD 状态提示 ==================== */ function createHUD() { if (!CFG.showHUD) return; // 只在顶层窗口显示,iframe 内不显示 if (window.top !== window.self) return; // 已存在则跳过 if (hudHost && hudHost.isConnected) return; // ---- 宿主元素:fixed 定位 + 内联关键样式 ---- const host = document.createElement('div'); host.id = '__auto_study_hud__'; // 注意:all: initial 必须放在最前面,把页面能给的内联样式全部归零 host.style.cssText = 'all: initial;' + 'position: fixed !important;' + 'right: 20px !important;' + 'bottom: 20px !important;' + 'z-index: 2147483647 !important;' + 'pointer-events: none !important;' + // 绝不挡点击 'display: block !important;' + 'visibility: visible !important;' + 'opacity: 1 !important;'; // ---- Shadow DOM 样式隔离 ---- let shadow; try { shadow = host.attachShadow({ mode: 'closed' }); } catch (e) { return; // 极老旧浏览器不支持,直接放弃 } const style = document.createElement('style'); style.textContent = ` .hud { display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px; background: rgba(20, 22, 28, 0.82); color: #e8eaed; font: 500 13px/1 -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; letter-spacing: .3px; box-shadow: 0 6px 20px rgba(0,0,0,.35), 0 0 0 1px rgba(255,255,255,.08) inset; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); user-select: none; white-space: nowrap; animation: hudFadeIn .4s ease; } .dot { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; animation: hudPulse 1.8s ease-out infinite; } @keyframes hudPulse { 0% { box-shadow: 0 0 0 0 rgba(34,197,94,.75); } 70% { box-shadow: 0 0 0 10px rgba(34,197,94,0); } 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); } } @keyframes hudFadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } } `; shadow.appendChild(style); const box = document.createElement('div'); box.className = 'hud'; const dot = document.createElement('span'); dot.className = 'dot'; const text = document.createElement('span'); text.textContent = CFG.hudText; box.appendChild(dot); box.appendChild(text); shadow.appendChild(box); (document.body || document.documentElement).appendChild(host); hudHost = host; log('HUD 状态提示已创建'); } // 如果宿主被页面脚本移除,自动重建 function ensureHUD() { if (!CFG.showHUD) return; if (!hudHost || !hudHost.isConnected) { createHUD(); } } /* ==================== 视频自动播放 ==================== */ function tryPlay(v) { if (!v || !v.paused) return; try { const p = v.play(); if (p && p.catch) { p.catch(() => { if (!v.muted) { v.muted = true; v.play().catch(() => {}); } }); } } catch (e) {} } function autoPlay() { document.querySelectorAll('video').forEach(tryPlay); } /* ==================== 弹窗处理 ==================== */ function findTopPopup() { let best = null, bestZ = -1; document.querySelectorAll(POPUP_SELECTOR).forEach(el => { if (!isVisible(el)) return; const r = el.getBoundingClientRect(); if (r.width < 80 || r.height < 40) return; const z = parseInt(getComputedStyle(el).zIndex, 10) || 0; if (z >= bestZ) { bestZ = z; best = el; } }); return best; } function handlePopup(popup) { if (!popup) return false; // 1) 关闭图标 const closeBtn = popup.querySelector(CLOSE_SELECTOR); if (closeBtn && isVisible(closeBtn)) { doClick(closeBtn); log('关闭弹窗(图标):', textOf(closeBtn) || closeBtn.className); return true; } // 2) 文字按钮 const btns = popup.querySelectorAll( 'button,a,[role="button"],input[type="button"],input[type="submit"],[class*="btn" i]' ); for (const b of btns) { if (!isVisible(b)) continue; const t = textOf(b); if (!t || DANGER.test(t)) continue; if (CLOSE_TEXTS.some(x => t === x || t.includes(x))) { doClick(b); log('点击文字按钮:', t); return true; } } // 3) Esc 兜底 document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', keyCode: 27, bubbles: true })); document.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape', keyCode: 27, bubbles: true })); log('Esc 兜底关闭'); return true; } /* ==================== 主扫描 ==================== */ function scan() { ensureHUD(); // 保证右下角提示一直都在 if (busy) return; const now = Date.now(); if (now - lastActionTime < CFG.cooldownAfterAction) return; busy = true; try { // ① 弹窗 const popup = findTopPopup(); if (popup) { if (handlePopup(popup)) { lastActionTime = Date.now(); return; } } // ② 下一活动 if (now - lastNextTime >= CFG.nextCooldown) { const btns = document.querySelectorAll('button,a,[role="button"]'); for (const b of btns) { if (!isVisible(b)) continue; const t = textOf(b); if (t && NEXT_TEXTS.some(x => t === x)) { doClick(b); lastNextTime = Date.now(); log('点击下一活动'); break; } } } } finally { busy = false; } } /* ==================== 启动 ==================== */ function init() { log('脚本已启动 v0.6(带右下角状态提示)'); createHUD(); // 立即显示右下角提示 autoPlay(); setInterval(autoPlay, CFG.videoInterval); setInterval(scan, CFG.scanInterval); // MutationObserver —— 500ms 防抖 let timer = null; const mo = new MutationObserver(() => { if (timer) return; timer = setTimeout(() => { timer = null; scan(); }, CFG.moDebounce); }); mo.observe(document.documentElement, { childList: true, subtree: true }); window.addEventListener('focus', autoPlay); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init, { once: true }); } else { init(); } })();