新增 runner/tui.mjs(~470 行):前台运行时自动开一块实时面板, 框线布局显示状态 / 课程 / 学时 / 课时 / 视频进度条 / 进度 / 最近日志。 - 学习统计 runner/study.mjs:只累计视频真在播的时长(暂停、重试退避、 页面加载都不计),学分按课程去重(重试 / 刷新不重复计) - 页面侧新增 credit 快照:平台只把本门学分写在倒计时文案里, 学完文案就消失,所以边播边记 - log.mjs 增加 setConsoleSink:面板接管终端那一路,日志文件仍是纯文本 - 面板里按 q 等价 Ctrl+C,走 stop.request 同一条优雅收尾路径 - 尺寸自适应:宽度跟随终端(不封顶 120 列),无 resize 事件的平台 靠定时刷新发现尺寸变化;变窄时按旧帧在新宽度下的折行数回退光标, 折完超过窗口高就整屏重置;宽度 / 高度不足按重要性丢字段 - 非 TTY(重定向 / 管道 / 计划任务 / --check)自动降级为逐行日志, 不写 ANSI 控制字符;--no-tui / YUN_XUE_TANG_TUI=0 可手动关闭, --no-color / NO_COLOR 关颜色 - 监控轮询从 2000ms 调到 1000ms,与面板刷新对齐 - supervisor 收尾时先收面板再逐行打退出日志,并汇总本次学习时长 / 学分 - check.mjs 增加裸 CR 检查:脚本里的 config\runner 曾被写成裸 CR, 变成 config<CR>unner 导致找不到配置文件 自测:npm run test:tui(渲染 / 列宽 / 极端尺寸)、npm run test:study(假时钟口径)
160 lines
6.6 KiB
JavaScript
160 lines
6.6 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 || '',
|
|
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,
|
|
};
|
|
})())`;
|
|
|
|
/** 收尾:暂停媒体、停批量、存进度(浏览器关掉前调用) */
|
|
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) => `(function () {
|
|
var a = window.__yunXueTangAuto;
|
|
try { localStorage.setItem('yun-xue-tang-prefer-short', '${preferShort ? '1' : '0'}'); } catch (e) {}
|
|
a.state.preferShortCourses = ${preferShort ? 'true' : 'false'};
|
|
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 };
|