- 按课时类型计分:文档 1 / 视频 3 / 音频 2 / 微课 2,每次 3 分,单日封顶 24 分;满额换类,无内容即停 - 积分计数落 localStorage,跨刷新/重启保留、跨天清零;面板与无头 TUI 都显示今日积分 - 运行器自己收尾时通过 Bark 推送今日积分、完成门数、用时和结束原因;手动停止与 --check 不推送 - 新增 --points-mode / --bark-* 参数及对应 YUN_XUE_TANG_* 环境变量 - 补充 points / notify 离线回归测试与 OpenSpec 配置 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
167 lines
7.1 KiB
JavaScript
167 lines
7.1 KiB
JavaScript
// 页面侧接口:所有「注入后要调用的 JS 表达式」集中在这里,外加把 CDP 的求值包成
|
|
// 好用的 page 对象。改脚本内部 API 时只需要动这一个文件。
|
|
//
|
|
// page.evalJS(expr) 求值并返回值(异常会抛出)
|
|
// page.json(expr) 求值并把返回的 JSON 字符串解析成对象
|
|
// page.tryEval(expr) 求值但吞掉异常,返回 undefined(页面正在跳转时用)
|
|
import { sleep } from './cdp.mjs';
|
|
|
|
/**
|
|
* @param {object} cdp cdp.mjs 的客户端
|
|
*/
|
|
export function createPage(cdp) {
|
|
const evalJS = async (expr) => {
|
|
const r = await cdp.send('Runtime.evaluate', {
|
|
expression: expr, returnByValue: true, awaitPromise: true, userGesture: true,
|
|
});
|
|
if (r.exceptionDetails) {
|
|
const d = r.exceptionDetails;
|
|
throw new Error((d.exception && d.exception.description) || d.text);
|
|
}
|
|
return r.result.value;
|
|
};
|
|
const json = async (expr) => JSON.parse(await evalJS(expr));
|
|
const tryEval = async (expr) => {
|
|
try { return await evalJS(expr); } catch (e) { return undefined; }
|
|
};
|
|
return { evalJS, json, tryEval };
|
|
}
|
|
|
|
/** 环境自检:脚本是否注入、面板是否渲染、视频编解码能力、中文字体 */
|
|
export const EXPR_SELF_CHECK = `JSON.stringify((function () {
|
|
var a = window.__yunXueTangAuto;
|
|
var probe = document.createElement('video');
|
|
var codecs = {
|
|
h264: probe.canPlayType('video/mp4; codecs="avc1.42E01E"') || '',
|
|
aac: probe.canPlayType('audio/mp4; codecs="mp4a.40.2"') || '',
|
|
vp9: probe.canPlayType('video/webm; codecs="vp9"') || '',
|
|
};
|
|
return {
|
|
url: location.href,
|
|
ua: navigator.userAgent,
|
|
api: typeof a,
|
|
version: (document.getElementById('yun-xue-tang-version') || {}).textContent || '',
|
|
panels: document.querySelectorAll('#yun-xue-tang-panel').length,
|
|
panelVisible: (function () {
|
|
var el = document.getElementById('yun-xue-tang-panel');
|
|
if (!el) return false;
|
|
var r = el.getBoundingClientRect();
|
|
return r.width > 0 && r.height > 0;
|
|
})(),
|
|
isLoginPage: a ? a.isLoginPage() : null,
|
|
route: a ? a.route() : null,
|
|
codecs: codecs,
|
|
fonts: (function () {
|
|
// 中文字体缺失不影响逻辑(靠 DOM 文本匹配),仅影响截图观感
|
|
var c = document.createElement('canvas').getContext('2d');
|
|
if (!c) return 'n/a';
|
|
c.font = '16px sans-serif';
|
|
var w = c.measureText('云学堂助手').width;
|
|
var w2 = c.measureText('abcd').width;
|
|
return w > 0 && w2 > 0 ? 'ok' : 'unknown';
|
|
})(),
|
|
};
|
|
})())`;
|
|
|
|
/** 看护循环:判断是否需要注入 / 是否在登录页 / 当前 --max 生效值 */
|
|
export const EXPR_WATCH = `JSON.stringify({
|
|
href: location.href,
|
|
ready: typeof window.__yunXueTangAuto === 'object',
|
|
onLogin: !!(window.__yunXueTangAuto && window.__yunXueTangAuto.isLoginPage()),
|
|
hasPwd: !!document.querySelector('input[type=password]'),
|
|
bodyLen: (document.body && document.body.innerText || '').length,
|
|
maxCourses: (window.__yunXueTangAuto && window.__yunXueTangAuto.CFG) ? window.__yunXueTangAuto.CFG.maxCourses : null,
|
|
})`;
|
|
|
|
/** 主监控快照:课程、课时、进度、倒计时、视频状态 */
|
|
export const EXPR_SNAPSHOT = `JSON.stringify((function () {
|
|
var a = window.__yunXueTangAuto;
|
|
if (!a) return { noApi: true, href: location.href };
|
|
var v = document.querySelector('video, audio');
|
|
var items = [];
|
|
try { items = a.lessonItems(); } catch (e) {}
|
|
return {
|
|
href: location.href,
|
|
route: a.route(),
|
|
running: a.S.batchRunning,
|
|
runFinished: a.S.batchFinished,
|
|
phase: a.S.coursePhase,
|
|
completed: a.S.completedThisRun,
|
|
pendingRetries: Object.keys(a.S.courseRetries || {}).length,
|
|
course: a.S.activeCourse ? a.S.activeCourse.title : null,
|
|
lesson: (a.readActiveLessonKey ? a.readActiveLessonKey() : ''),
|
|
lessons: items.length,
|
|
summary: a.readSummary(),
|
|
countdown: a.readCountdown(),
|
|
// 本课程可获得多少学分:平台只把它写在倒计时文案里
|
|
// 「还需 2小时 7分钟 可完成本课程学习并获得6.52学分,加油!」
|
|
// 学完后倒计时会消失,所以必须在播放期间就读出来(运行器靠它累计到手学分)
|
|
credit: (function () {
|
|
var el = document.querySelector('.yxtulcdsdk-course-player__countdown');
|
|
if (!el) return null;
|
|
var t = String(el.innerText || el.textContent || '').replace(/\\s+/g, '');
|
|
var m = t.match(/获得([0-9]+(?:\\.[0-9]+)?)学分/);
|
|
return m ? Number(m[1]) : null;
|
|
})(),
|
|
status: (document.getElementById('yun-xue-tang-status') || {}).textContent || '',
|
|
// 每日积分:{ enabled, total, max, progress:[{label,count,cap}], text }
|
|
points: (a.points && a.points.snapshot ? a.points.snapshot() : null),
|
|
doneCount: Object.keys(a.S.completedTitles || {}).length,
|
|
retryCount: Object.keys(a.S.courseRetries || {}).length,
|
|
retryWait: (a.retryWaitSeconds ? a.retryWaitSeconds() : 0),
|
|
retryTitle: a.R.retryTitle || '',
|
|
video: v ? {
|
|
t: Math.round(v.currentTime), d: Math.round(v.duration || 0),
|
|
paused: v.paused, err: v.error ? v.error.code : 0, src: v.currentSrc ? 1 : 0,
|
|
} : null,
|
|
};
|
|
})())`;
|
|
|
|
/** 会话结尾的累计统计 */
|
|
export const EXPR_FINAL_STATS = `JSON.stringify((function () {
|
|
var a = window.__yunXueTangAuto;
|
|
if (!a) return {};
|
|
return {
|
|
completed: a.S.completedThisRun,
|
|
doneCount: Object.keys(a.S.completedTitles || {}).length,
|
|
retryCount: Object.keys(a.S.courseRetries || {}).length,
|
|
// 每日积分:收尾通知里要带上「今日积分 X/24 · 各类次数」
|
|
points: (a.points && a.points.snapshot ? a.points.snapshot() : null),
|
|
};
|
|
})())`;
|
|
|
|
/** 收尾:暂停媒体、停批量、存进度(浏览器关掉前调用) */
|
|
export const EXPR_PAUSE_AND_SAVE = `(function () {
|
|
var a = window.__yunXueTangAuto;
|
|
if (a) { try { a.pauseAllMedia(); a.S.batchRunning = false; a.save(); } catch (e) {} }
|
|
return true;
|
|
})()`;
|
|
|
|
/** 把 maxCourses 同步到页面(0 = 不限,必须一并推送) */
|
|
export const exprSetMaxCourses = (max) =>
|
|
`(function(){ var a = window.__yunXueTangAuto; if (a && a.CFG) a.CFG.maxCourses = ${Number(max)}; return true; })()`;
|
|
|
|
/** 启动 / 接续批量刷课,同时同步「优先短课程」「每日积分」偏好 */
|
|
export const exprStartBatch = (preferShort, pointsMode) => `(function () {
|
|
var a = window.__yunXueTangAuto;
|
|
try { localStorage.setItem('yun-xue-tang-prefer-short', '${preferShort ? '1' : '0'}'); } catch (e) {}
|
|
try { localStorage.setItem('yun-xue-tang-daily-points-mode', '${pointsMode ? '1' : '0'}'); } catch (e) {}
|
|
a.state.preferShortCourses = ${preferShort ? 'true' : 'false'};
|
|
a.state.pointsEnabled = ${pointsMode ? 'true' : 'false'};
|
|
try { a.points.setEnabled(${pointsMode ? 'true' : 'false'}); } catch (e) {}
|
|
a.startBatch();
|
|
return true;
|
|
})()`;
|
|
|
|
/** 登录页:填账号密码、勾协议、点登录并接着刷课 */
|
|
export const exprAutoLogin = (usr, pwd) => `(function () {
|
|
var a = window.__yunXueTangAuto;
|
|
if (!a) return 'no-api';
|
|
var ok = a.fillLoginForm(${JSON.stringify(usr)}, ${JSON.stringify(pwd)});
|
|
try { a.checkLoginAgreement(); } catch (e) {}
|
|
a.doLoginAndStart();
|
|
return ok ? 'filled' : 'no-fields';
|
|
})()`;
|
|
|
|
export { sleep };
|