// ==UserScript== // @name 广东继教 · 师德师风 // @namespace gdstudy.helper // @version 5.2 // @description 进入广东省教师继续教育课程页自动播放视频;捕获 updateViewTime1 上报后按设定间隔循环重复上报;右下角显示状态与停止按钮 // @author - // @match https://jsxx.gds.edu.cn/study/* // @run-at document-start // @grant none // @noframes // ==/UserScript== (function () { 'use strict'; if (window.__gdStudyHelper) return; // 防止同一页面重复注入 window.__gdStudyHelper = true; /* ================= 可调参数 ================= */ var CFG = { gap: 3000, // 重复上报间隔(毫秒),默认 3 秒 maxGap: 15000, // 被服务器判定“过快/异常”时的退避上限 autoPlay: true, // 进入页面自动开始播放 autoResume: true, // 被暂停后自动恢复播放(有答题弹层时不抢) keepAlive: true, // 定时续期,防止 30 分钟挂机被退出登录 keepAliveMs: 14 * 60 * 1000 }; /* ================= 内部状态 ================= */ var lastReport = null; // 最近一次抓到的上报:{ url, body } var count = 0; // 本页重复上报次数 var gap = CFG.gap; // 当前生效间隔(异常时自动退避) var stopped = false; // 是否处于“停止上报”状态(用户点停或异常自动暂停) var running = false; // 循环是否在跑 var loopTimer = null; var inFlight = false; var failures = 0; // 连续失败次数 var lastSkipNote = 0; /* ================= 控制面板 ================= */ var elStatus, elMeta, elDot, btnToggle, btnOnce; function mountPanel() { if (document.getElementById('gd-study-panel') || !document.body) return; var box = document.createElement('div'); box.id = 'gd-study-panel'; box.style.cssText = [ 'position:fixed', 'right:16px', 'bottom:16px', 'z-index:2147483647', 'width:196px', 'padding:10px 12px', 'border-radius:10px', 'background:rgba(24,28,36,.92)', 'color:#e9edf5', 'font:12px/1.6 system-ui,"Microsoft YaHei",sans-serif', 'box-shadow:0 6px 20px rgba(0,0,0,.35)', 'border:1px solid rgba(255,255,255,.12)', 'user-select:none', 'text-align:left' ].join(';'); box.innerHTML = '
' + '自动学习助手' + '' + '
' + '
待机
' + '
尚未接收到上报数据
' + '
' + '' + '' + '
'; document.body.appendChild(box); elStatus = box.querySelector('#gd-status'); elMeta = box.querySelector('#gd-meta'); elDot = box.querySelector('#gd-dot'); btnToggle = box.querySelector('#gd-toggle'); btnOnce = box.querySelector('#gd-once'); btnToggle.addEventListener('click', function () { if (stopped) { setStoppedUI(false); failures = 0; gap = CFG.gap; if (lastReport) { startLoop(); } else { setStatus('待机', '#8b93a3'); } } else { halt('已手动停止'); } updateMeta(); }); btnOnce.addEventListener('click', function () { if (!lastReport) { flash('还没有接收到上报数据'); return; } repeatOnce(true); }); } function setStatus(text, color) { if (!elStatus) return; elStatus.textContent = text; elStatus.style.color = color || '#ffffff'; if (elDot) elDot.style.background = color || '#8b93a3'; } function setStoppedUI(v) { stopped = v; if (!btnToggle) return; btnToggle.textContent = v ? '继续上报' : '停止上报'; btnToggle.style.background = v ? '#3a7bd5' : '#e14b4b'; } function halt(reason) { stopLoop(); setStoppedUI(true); setStatus('停止上报', '#e14b4b'); if (reason) flash(reason); updateMeta(); } function updateMeta(extra) { if (!elMeta) return; var t = lastReport ? ('重复上报 ' + count + ' 次 · 间隔 ' + (gap / 1000) + 's') : '尚未接收到上报数据'; elMeta.textContent = extra ? (t + ' · ' + extra) : t; } function flash(msg) { if (!elMeta) return; elMeta.textContent = msg; setTimeout(updateMeta, 2500); } /* ================= 捕获 updateViewTime1 上报 ================= */ function hookXHR() { var XHR = window.XMLHttpRequest; if (!XHR || !XHR.prototype) return; var rawOpen = XHR.prototype.open; var rawSend = XHR.prototype.send; XHR.prototype.open = function (method, url) { this.__gdMethod = method; this.__gdUrl = url; return rawOpen.apply(this, arguments); }; XHR.prototype.send = function (body) { try { var url = String(this.__gdUrl || ''); var method = String(this.__gdMethod || 'GET').toUpperCase(); if (method === 'POST' && url.indexOf('updateViewTime1') !== -1 && body) { onReport(url, String(body)); } } catch (e) { /* 不影响页面自身请求 */ } return rawSend.apply(this, arguments); }; } function onReport(url, body) { lastReport = { url: url, body: body }; if (stopped) { setStatus('停止上报', '#e14b4b'); updateMeta(); return; } setStatus('接收到上报数据', '#f5a623'); updateMeta(); // 闪一下“接收到上报数据”,随即进入自动上报 setTimeout(function () { if (!stopped) { startLoop(); setStatus('正在自动上报', '#35c26a'); updateMeta(); } }, 900); } /* ================= 重复上报 ================= */ function startLoop() { if (stopped || !lastReport) return; running = true; setStatus('正在自动上报', '#35c26a'); schedule(gap); } function stopLoop() { running = false; clearTimeout(loopTimer); loopTimer = null; } function schedule(ms) { clearTimeout(loopTimer); if (!running || stopped) return; loopTimer = setTimeout(function () { repeatOnce(false).then(function () { schedule(gap); }); }, ms); } // 播放会话标识会随暂停/恢复重新生成,尽量跟上页面当前的会话 function currentSessionId() { try { for (var i = 0; i < localStorage.length; i++) { var k = localStorage.key(i); if (k && k.indexOf('video_playback_owner') === 0) { var val = localStorage.getItem(k) || ''; if (/^[0-9a-f]{8}-[0-9a-f]{8}-[0-9a-f]{8}-[0-9a-f]{8}$/.test(val)) return val; } } } catch (e) {} return null; } // 让 lastViewTime 跟着视频进度走,避免服务器认为时间没推进 function buildBody(body) { var v = videoEl(); var m = /lastViewTime=([0-9]+)/.exec(body); var prev = m ? parseInt(m[1], 10) : 0; var cur = v ? Math.round(v.currentTime) : prev; var next = Math.max(cur, prev + Math.max(1, Math.round(gap / 1000))); if (v && isFinite(v.duration) && v.duration > 0) { next = Math.min(next, Math.floor(v.duration)); } var out = body.replace(/lastViewTime=([0-9]+)/, 'lastViewTime=' + next); var sid = currentSessionId(); if (sid && /playbackSessionId=[^&]*/.test(out)) { out = out.replace(/playbackSessionId=[^&]*/, 'playbackSessionId=' + sid); } return out; } function repeatOnce(manual) { if (inFlight || !lastReport) return Promise.resolve(); var v = videoEl(); if (!manual && (!v || v.paused || v.ended)) { // 视频暂停或已播完时不发,避免用过期会话把页面自己的计时打断 if (!stopped && Date.now() - lastSkipNote > 15000) { lastSkipNote = Date.now(); flash('视频已暂停,暂不上报'); } return Promise.resolve(); } inFlight = true; var body = buildBody(lastReport.body); var url = lastReport.url; return fetch(url, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest' }, body: body }).then(function (resp) { return resp.text().then(function (txt) { return { status: resp.status, txt: txt }; }); }).then(function (r) { var data = null; try { data = JSON.parse(r.txt); } catch (e) {} if (r.status === 200 && data && data.responseCode === '00') { count++; failures = 0; gap = CFG.gap; // 恢复正常间隔 if (!stopped) setStatus('正在自动上报', '#35c26a'); updateMeta(); } else if (data && data.responseCode === '01') { var msg = String(data.responseMsg || ''); if (msg === 'more video is open') { halt('服务器提示有其他视频正在播放'); return; } failures++; gap = Math.min(gap * 2, CFG.maxGap); // too soon 等:放慢节奏 if (failures >= 6) { halt('连续异常,已暂停上报'); } else { flash('上报过快,退避到 ' + (gap / 1000) + 's'); } } else if (r.status === 302) { halt('登录已失效,请刷新页面重新登录'); return; } else if (r.status === 403 || r.status === 441 || r.status === 404 || r.status === 502) { failures++; gap = Math.min(gap * 2, CFG.maxGap); if (failures >= 6) { halt('连续异常(' + r.status + '),已暂停上报'); } else { flash('服务器返回 ' + r.status + ',退避到 ' + (gap / 1000) + 's'); } } else { // 返回非 JSON:多半是会话失效或接口变更 failures++; if (failures >= 3) { halt('上报异常(' + (r.status || '无响应') + '),已暂停'); } else { flash('上报异常(' + (r.status || '无响应') + ')'); } } if (manual) updateMeta(); }).catch(function () { if (manual) flash('立即上报失败'); }).then(function () { inFlight = false; }); } /* ================= 自动播放 ================= */ function videoEl() { return document.querySelector('video'); } function startPlay() { var v = videoEl(); if (!v) return; try { v.muted = true; } catch (e) {} try { v.autoplay = true; } catch (e) {} if (!v.paused) return; var p = window.player; // 页面自身的 ckplayer 实例 if (p && typeof p.videoPlay === 'function') { try { p.videoPlay(); } catch (e) {} } if (v.paused) { try { var pr = v.play(); if (pr && pr.catch) pr.catch(function () {}); } catch (e) {} } } function bootPlay() { if (!CFG.autoPlay) return; var tries = 0; var tick = setInterval(function () { tries++; if (videoEl()) { startPlay(); if (tries > 3) clearInterval(tick); // 视频已出现,再多点几次保险 } if (tries > 60) clearInterval(tick); }, 1000); } /* ================= 弹层处理 & 保活 ================= */ function hasModal() { // 答题弹层是用 iframe 打开的(src 指向 question 页面) var frames = document.querySelectorAll('iframe'); for (var i = 0; i < frames.length; i++) { var fsrc = String(frames[i].src || ''); if (fsrc.indexOf('question') !== -1 || fsrc.indexOf('video/question') !== -1) return true; } var body = document.body ? document.body.innerText || '' : ''; if (body.indexOf('请作答') !== -1) return true; if (body.indexOf('您已完成这个活动') !== -1) return true; return false; } function clickContinue() { var nodes = document.querySelectorAll('button,a,div,span,li'); for (var i = 0; i < nodes.length; i++) { var t = (nodes[i].textContent || '').trim(); if (t === '继续学习') { nodes[i].click(); return true; } } return false; } function keepAlive() { if (!CFG.keepAlive) return; setInterval(function () { try { fetch('/ncts/resetSession', { credentials: 'include' }); } catch (e) {} }, CFG.keepAliveMs); } function watchdog() { setInterval(function () { if (!document.body) return; // 挂机提示弹层:自动点“继续学习” if (clickContinue()) return; if (!CFG.autoResume || hasModal()) return; var v = videoEl(); if (!v || v.ended) return; if (v.paused) startPlay(); }, 5000); } /* ================= 启动 ================= */ hookXHR(); function init() { mountPanel(); bootPlay(); keepAlive(); watchdog(); updateMeta(); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();