// ==UserScript== // @name 自动关闭学习暂停弹窗 // @namespace https://scriptcat.org/scripts/ // @version 1.1.0 // @description 检测并自动点掉“长时间未操作 / 学习时长计时暂停”这类弹窗,并验证点击是否真的生效。只做这一件事,不模拟鼠标、不滚动、不改页面其它东西。 // @author zhx981350 // @match *://*/* // @grant GM_registerMenuCommand // @run-at document-idle // @noframes // @license MIT // ==/UserScript== /* * ===== 它做什么 ===== * 页面上出现含“长时间未操作 / 计时暂停”字样的弹层时,自动点掉里面的 * 「继续学习 / 确定 / 继续 / 我知道了」按钮。 * 点完会在 1.2 秒后复查:弹窗真消失了才算成功;连续 3 次点了还在, * 就判定“平台不认脚本点击”,自动降频并在控制台明确告诉你,不再空转。 * * ===== 它不做什么 ===== * - 不派发鼠标 / 键盘事件(合成事件 isTrusted=false,装了也没用) * - 不滚动、不点页面其它位置(有防误点保护,见 isDialogNode) * - 不改动任何业务数据,不碰考试、不提交答案 * * ===== 已知边界(重要)===== * 1. 浏览器禁止脚本伪造 isTrusted 事件。若平台要求“真实操作”才恢复计时, * 点掉弹窗后它可能立刻再弹,或时长依旧不涨。脚本会把这个情况明确报出来。 * 2. 原生 alert / confirm / 系统级对话框无法用页面脚本关闭。 * 3. 跨域 iframe 内部无法访问(同源 iframe 能扫到)。 * * ===== 用法 ===== * 装上即生效,无需任何操作。 * 脚本猫菜单:查看状态、清零计数、暂停 / 恢复、开关调试日志。 */ (function () { 'use strict'; const CONFIG = { enabled: true, debug: true, // 弹窗里出现这些字样就认定它是暂停弹窗 dialogTexts: [ '长时间未操作', '长时间无操作', '学习时长计时暂停', '计时暂停', '学习已暂停', ], // 允许点击的按钮文字:先找完全相等的,再退化为包含 buttonTexts: [ '继续学习', '继续观看', '继续', '我知道了', '确定', '好的', '知道了', '确认', ], scanInterval: 2500, // 快扫间隔 deepScanEvery: 6, // 每 N 次快扫做一次全文档深扫 minClickGap: 1500, // 两次点击最小间隔 verifyDelay: 1200, // 点完多久复查 stuckAfter: 3, // 连续几次“点了还在”就判定无效 stuckInterval: 30000, // 判定无效后的扫描间隔 maxDialogTextLength: 800, // 容器文字超过这个长度不当弹窗 maxUpLevels: 7, // 从弹窗文字向上找按钮的最大层数 maxButtonsInDialog: 10, // 弹窗内按钮超过这个数 → 更像页面容器,不当弹窗 fullscreenRatio: 0.92, // 占满视口到该比例且按钮很多 → 页面容器 scanFrames: true, }; const now = () => Date.now(); let clickCount = 0; let lastClickAt = 0; let failStreak = 0; let stuck = false; let pendingText = ''; let scanTick = 0; let timer = null; const log = (...a) => { if (CONFIG.debug) console.log('%c[关弹窗]', 'color:#059669;font-weight:600', ...a); }; /* --------------------------------------------------------------- * 1. 判断一个元素是不是暂停弹窗(带防误点保护) * --------------------------------------------------------------- */ function countControls(el) { let n = 0; try { n = el.querySelectorAll('a,button,input,[role="button"],[onclick]').length; } catch (e) {} return n; } function hasIdleText(text) { for (let i = 0; i < CONFIG.dialogTexts.length; i++) { if (text.indexOf(CONFIG.dialogTexts[i]) !== -1) return true; } return false; } function isDialogNode(el) { if (!el || el.nodeType !== 1) return false; // 整页容器绝不当作弹窗,否则会误点页面自己的按钮 if (el === document.body || el === document.documentElement) return false; let text = ''; try { text = el.textContent || ''; } catch (e) { return false; } if (!text || text.length > CONFIG.maxDialogTextLength) return false; if (!hasIdleText(text)) return false; let rect; try { rect = el.getBoundingClientRect(); } catch (e) { return false; } if (rect.width < 40 || rect.height < 16) return false; let vis = 'visible'; try { vis = getComputedStyle(el).visibility; } catch (e) {} if (vis === 'hidden') return false; const controls = countControls(el); // 按钮太多的容器是页面,不是弹窗 if (controls > CONFIG.maxButtonsInDialog) return false; // 占满视口又塞了一堆控件 → 页面容器 const vw = window.innerWidth || 1024; const vh = window.innerHeight || 768; if (rect.width > vw * CONFIG.fullscreenRatio && rect.height > vh * CONFIG.fullscreenRatio) { if (controls > 6) return false; } return true; } /* --------------------------------------------------------------- * 2. 找“继续 / 确定”按钮 * --------------------------------------------------------------- */ function labelOf(el) { let label = el.textContent || ''; if (!label && el.value) label = el.value; return String(label).replace(/\s+/g, ''); } // 真正的可交互元素(这些点了才算点) const REAL_BTN_SEL = 'button,a,input[type="button"],input[type="submit"],[role="button"],[onclick]'; // 宽泛候选:很多框架用 div/span 假装按钮 const SOFT_BTN_SEL = '.layui-layer-btn0,[class*="btn"],[class*="button"]'; function queryAll(scope, sel) { try { return scope.querySelectorAll(sel); } catch (e) { return []; } } // 文字匹配等级:2 = 完全相同,1 = 包含,0 = 不匹配 function matchLevel(label, wanted) { if (!label) return 0; for (let i = 0; i < wanted.length; i++) { if (label === wanted[i]) return 2; } for (let i = 0; i < wanted.length; i++) { if (label.length <= 8 && label.indexOf(wanted[i]) !== -1) return 1; } return 0; } function depthIn(el, scope) { let d = 0; let p = el; while (p && p !== scope) { d += 1; p = p.parentElement; if (d > 64) break; } return d; } function findButtonIn(scope) { const wanted = CONFIG.buttonTexts; // 第一优先:真正的交互元素,取匹配等级最高、层级最深的那个 const real = queryAll(scope, REAL_BTN_SEL); let best = null; let bestLevel = 0; let bestDepth = -1; for (let i = 0; i < real.length; i++) { const el = real[i]; const lv = matchLevel(labelOf(el), wanted); if (!lv) continue; const d = depthIn(el, scope); if (lv > bestLevel || (lv === bestLevel && d > bestDepth)) { best = el; bestLevel = lv; bestDepth = d; } } if (best) return best; // 第二优先:class 里带 btn/button 的元素,但必须“自己就是最里层” // 例:.layui-layer-btn 是个包着 的 div,点它没用,必须跳过 const soft = queryAll(scope, SOFT_BTN_SEL); for (let i = 0; i < soft.length; i++) { const el = soft[i]; const lv = matchLevel(labelOf(el), wanted); if (!lv) continue; // 内部还有真正可点的元素 → 说明它只是外壳 if (queryAll(el, REAL_BTN_SEL).length > 0) continue; // 内部还有同样文字的后代 → 说明它只是外壳 let hasLabelledChild = false; const kids = queryAll(el, '*'); for (let k = 0; k < kids.length; k++) { if (matchLevel(labelOf(kids[k]), wanted)) { hasLabelledChild = true; break; } } if (hasLabelledChild) continue; return el; } return null; } function shallowFind() { const selectors = [ '[class*="layer"]', '[class*="modal"]', '[class*="dialog"]', '[class*="popup"]', '[class*="alert"]', '[class*="tips"]', '[role="dialog"]', '[role="alertdialog"]', '[style*="position:fixed"]', '[style*="position: fixed"]', ]; let nodes = []; try { nodes = document.querySelectorAll(selectors.join(',')); } catch (e) {} const limit = Math.min(nodes.length, 500); for (let i = 0; i < limit; i++) { const el = nodes[i]; if (!isDialogNode(el)) continue; const btn = findButtonIn(el); if (btn) return { container: el, button: btn }; } return null; } function deepFind() { let all = []; try { all = document.querySelectorAll('div,p,section,span,td,li,h1,h2,h3,h4'); } catch (e) { return null; } const hits = []; const limit = Math.min(all.length, 6000); for (let i = 0; i < limit; i++) { const el = all[i]; if (!isDialogNode(el)) continue; hits.push(el); } if (!hits.length) return null; hits.sort((a, b) => (a.textContent || '').length - (b.textContent || '').length); for (let i = 0; i < hits.length && i < 8; i++) { let scope = hits[i]; for (let depth = 0; depth < CONFIG.maxUpLevels && scope; depth++) { const btn = findButtonIn(scope); if (btn) return { container: scope, button: btn }; scope = scope.parentElement; } } return null; } function findDialog() { return shallowFind() || deepFind(); } /* --------------------------------------------------------------- * 3. 点击 + 复查 * --------------------------------------------------------------- */ function dismiss(hit) { if (!hit || !hit.button) return; if (now() - lastClickAt < CONFIG.minClickGap) return; const btn = hit.button; const label = labelOf(btn); const snippet = (hit.container.textContent || '').trim().slice(0, 24); try { btn.scrollIntoView({ block: 'center' }); } catch (e) {} let ok = false; try { btn.click(); ok = true; } catch (e) { try { btn.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true })); ok = true; } catch (e2) {} } if (!ok) return; clickCount += 1; lastClickAt = now(); pendingText = snippet; log('第 ' + clickCount + ' 次关闭,点了「' + label + '」(弹窗:' + snippet + '…)'); setTimeout(verify, CONFIG.verifyDelay); } // 复查:弹窗真的消失了吗 function verify() { if (!CONFIG.enabled) return; let still = null; try { still = findDialog(); } catch (e) {} if (still) { failStreak += 1; if (failStreak >= CONFIG.stuckAfter && !stuck) { stuck = true; console.warn( '%c[关弹窗] 已连续 ' + failStreak + ' 次点击后弹窗仍在。' + '这说明该平台不认脚本触发的点击(或要求真实操作才恢复计时)。' + '脚本已自动降频到每 ' + CONFIG.stuckInterval / 1000 + ' 秒一次,避免空转。', 'color:#dc2626;font-weight:600' ); restartTimer(); } } else { if (failStreak > 0 || stuck) { log('弹窗已消失,计时应已恢复(此前连续失败 ' + failStreak + ' 次)'); } failStreak = 0; stuck = false; } } /* --------------------------------------------------------------- * 4. 同源 iframe * --------------------------------------------------------------- */ function scanFrames() { let frames = []; try { frames = document.querySelectorAll('iframe,frame'); } catch (e) { return; } for (let i = 0; i < frames.length; i++) { let doc = null; try { doc = frames[i].contentDocument; // 跨域会抛异常 } catch (e) { continue; } if (!doc || !doc.body) continue; try { const nodes = doc.querySelectorAll( '[class*="layer"],[class*="dialog"],[class*="modal"],[role="dialog"],[style*="position:fixed"]' ); for (let j = 0; j < nodes.length; j++) { const el = nodes[j]; const t = el.textContent || ''; if (!t || t.length > CONFIG.maxDialogTextLength) continue; if (!hasIdleText(t)) continue; const btn = findButtonIn(el); if (!btn) continue; try { btn.click(); clickCount += 1; lastClickAt = now(); log('第 ' + clickCount + ' 次关闭(iframe 内)'); verifyFrameLater(el); return; } catch (e) {} } } catch (e) {} } } // iframe 内点完也复查一次,避免“点了没用”时无限连点 function verifyFrameLater(el) { setTimeout(() => { let still = false; try { still = !!el && el.isConnected !== false && hasIdleText(el.textContent || ''); } catch (e) {} if (still) { failStreak += 1; if (failStreak >= CONFIG.stuckAfter && !stuck) { stuck = true; console.warn( '%c[关弹窗] iframe 内连续 ' + failStreak + ' 次点击后弹窗仍在,判定该平台不认脚本点击,已降频。', 'color:#dc2626;font-weight:600' ); restartTimer(); } } else { failStreak = 0; stuck = false; } }, CONFIG.verifyDelay); } /* --------------------------------------------------------------- * 5. 扫描循环 * --------------------------------------------------------------- */ function scanOnce() { if (!CONFIG.enabled) return; scanTick += 1; try { const hit = findDialog(); if (hit) { dismiss(hit); return; } if (CONFIG.scanFrames) scanFrames(); } catch (e) { log('扫描异常(已忽略)', e); } } function restartTimer() { clearInterval(timer); timer = setInterval(scanOnce, stuck ? CONFIG.stuckInterval : CONFIG.scanInterval); } /* --------------------------------------------------------------- * 6. MutationObserver:弹窗一出现就点,不等下一轮 * --------------------------------------------------------------- */ function watchMutations() { try { const mo = new MutationObserver((muts) => { if (!CONFIG.enabled) return; if (now() - lastClickAt < CONFIG.minClickGap) return; for (let i = 0; i < muts.length; i++) { const added = muts[i].addedNodes; for (let j = 0; j < added.length; j++) { const node = added[j]; if (!node || node.nodeType !== 1) continue; if (!isDialogNode(node)) continue; const btn = findButtonIn(node); if (btn) { dismiss({ container: node, button: btn }); return; } } } }); mo.observe(document.documentElement, { childList: true, subtree: true }); } catch (e) {} } /* --------------------------------------------------------------- * 7. 菜单 * --------------------------------------------------------------- */ function statusText() { if (!CONFIG.enabled) return '已暂停(不动作)'; if (stuck) return '运行中,但已判定“点击无效”,降频到 ' + CONFIG.stuckInterval / 1000 + 's'; return '运行中'; } function registerMenus() { if (typeof GM_registerMenuCommand !== 'function') return; const items = [ ['📊 状态与次数', () => { const msg = '状态:' + statusText() + '\n' + '已关闭次数:' + clickCount + '\n' + '最近一次:' + (lastClickAt ? new Date(lastClickAt).toLocaleTimeString() : '无') + '\n' + '连续失败:' + failStreak + ' 次'; console.log('[关弹窗] ' + msg.replace(/\n/g, ' | ')); try { alert(msg); } catch (e) {} }], ['🧹 清零计数', () => { clickCount = 0; lastClickAt = 0; failStreak = 0; stuck = false; restartTimer(); log('计数与状态已清零'); }], ['⏸ 暂停 / 恢复', () => { CONFIG.enabled = !CONFIG.enabled; log(CONFIG.enabled ? '已恢复' : '已暂停'); try { alert(CONFIG.enabled ? '自动关弹窗:已恢复' : '自动关弹窗:已暂停'); } catch (e) {} }], ['🔍 切换调试日志', () => { CONFIG.debug = !CONFIG.debug; try { alert('调试日志:' + (CONFIG.debug ? '开' : '关')); } catch (e) {} }], ]; items.forEach((it) => { try { GM_registerMenuCommand(it[0], it[1]); } catch (e) {} }); } /* --------------------------------------------------------------- * 8. 启动 * --------------------------------------------------------------- */ function boot() { registerMenus(); watchMutations(); restartTimer(); setTimeout(scanOnce, 800); setTimeout(scanOnce, 2200); console.log('[关弹窗] v1.1.0 已加载,等待“长时间未操作 / 计时暂停”弹窗'); } boot(); })();