feat: v2.9.0 终端实时面板,学习时长与学分统计
新增 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(假时钟口径)
This commit is contained in:
1 parent
c1e883d67f
commit
65bf63ea6a
17 files changed
+1608
-28
No files matched your search
+29
-4
@@ -13,6 +13,8 @@ import { runSelfCheck } from './selfcheck.mjs';
|
||||
|
||||
/** 看护循环间隔:既不能太密(CDP 往返),也要能及时补注入 */
|
||||
export const WATCHDOG_MS = 1500;
|
||||
/** 主监控轮询间隔:与面板的 1 秒刷新对齐,画面才不会一帧新一帧旧 */
|
||||
export const MONITOR_MS = 1000;
|
||||
/** 状态读不到多久后判定浏览器不可用 */
|
||||
const STATE_TIMEOUT_MS = 90000;
|
||||
/** 连续多少次登录失败判定为凭据/验证码问题(退出码 3) */
|
||||
@@ -37,11 +39,14 @@ const LOGIN_COOLDOWN_MS = 20000;
|
||||
* @param {string} o.binary 浏览器可执行文件
|
||||
* @param {string} o.scriptSrc 构建产物源码(注入用)
|
||||
* @param {object} o.stop stop.mjs 的 stop 句柄
|
||||
* @param {object} [o.tui] tui.mjs 的面板(不传 / 未启用时所有调用都是空操作)
|
||||
* @param {Function} o.log / o.warn / o.error 日志函数
|
||||
*/
|
||||
export async function runSession(o) {
|
||||
const { cfg, binary, scriptSrc, stop } = o;
|
||||
const log = o.log, warn = o.warn, error = o.error;
|
||||
// 面板是可选件:未启用时 set/snap 都是空操作,这里不需要分支判断
|
||||
const tui = o.tui || { set: () => {}, snap: () => {}, finish: () => {}, enabled: false };
|
||||
|
||||
let browser = null;
|
||||
let cdp = null;
|
||||
@@ -79,6 +84,8 @@ export async function runSession(o) {
|
||||
activeCleanup = async () => { await disposeSession(); unregister(); };
|
||||
|
||||
// ---- 起浏览器 + 建标签页 + 连 CDP ----
|
||||
// 注意:attempt 由 supervisor 管,这里不要传(传 undefined 会把它抹掉)
|
||||
tui.set({ phase: '启动浏览器', tone: 'info' });
|
||||
browser = await launchBrowser({
|
||||
binary,
|
||||
profileDir: cfg.PROFILE_DIR,
|
||||
@@ -88,6 +95,7 @@ export async function runSession(o) {
|
||||
log,
|
||||
});
|
||||
log('浏览器就绪:' + browser.version.Browser + ' profile=' + cfg.PROFILE_DIR);
|
||||
tui.set({ phase: '打开页面', tone: 'info' });
|
||||
|
||||
const target = await httpJson('http://127.0.0.1:' + cfg.PORT + '/json/new?' + encodeURIComponent(cfg.START_URL), 'PUT');
|
||||
const wsUrl = target && target.webSocketDebuggerUrl;
|
||||
@@ -138,6 +146,7 @@ export async function runSession(o) {
|
||||
const t = await page.evalJS('typeof window.__yunXueTangAuto');
|
||||
if (t === 'object') {
|
||||
injected++;
|
||||
tui.set({ injected, phase: '已注入脚本' });
|
||||
log('已注入脚本(第 ' + injected + ' 次):' + String(s.href).slice(0, 90));
|
||||
} else {
|
||||
warn('注入后未生成 window.__yunXueTangAuto(typeof=' + t + ')');
|
||||
@@ -163,6 +172,7 @@ export async function runSession(o) {
|
||||
if (Date.now() - loginLastAt < LOGIN_COOLDOWN_MS) return;
|
||||
loginLastAt = Date.now();
|
||||
loginAttempts++;
|
||||
tui.set({ phase: '自动登录(第 ' + loginAttempts + ' 次)', tone: 'warn' });
|
||||
log('登录页:自动登录(第 ' + loginAttempts + ' 次尝试)');
|
||||
try {
|
||||
const r = await page.evalJS(exprAutoLogin(cfg.USR, cfg.PWD));
|
||||
@@ -175,6 +185,7 @@ export async function runSession(o) {
|
||||
|
||||
if (loginAttempts) {
|
||||
loginAttempts = 0;
|
||||
tui.set({ phase: '已登录,准备刷课', tone: 'ok' });
|
||||
log('已离开登录页,登录成功');
|
||||
}
|
||||
|
||||
@@ -202,12 +213,16 @@ export async function runSession(o) {
|
||||
}
|
||||
if (!st.batchRunning && st.route !== 'other') {
|
||||
const ok = await page.tryEval(exprStartBatch(cfg.PREFER_SHORT));
|
||||
if (ok) log('已启动列表刷课' + (cfg.PREFER_SHORT ? '(优先短课程)' : ''));
|
||||
if (ok) {
|
||||
tui.set({ phase: '刷课中', tone: 'ok' });
|
||||
log('已启动列表刷课' + (cfg.PREFER_SHORT ? '(优先短课程)' : ''));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------- 自检模式 ----------------
|
||||
if (cfg.CHECK_ONLY) {
|
||||
tui.set({ phase: '环境自检中', tone: 'info' });
|
||||
const result = await runSelfCheck({
|
||||
page,
|
||||
tickWatchdog,
|
||||
@@ -260,7 +275,7 @@ export async function runSession(o) {
|
||||
if (Date.now() - lastStateAt > STATE_TIMEOUT_MS) {
|
||||
throw new Error('超过 ' + Math.round(STATE_TIMEOUT_MS / 1000) + ' 秒读不到脚本状态,重启浏览器');
|
||||
}
|
||||
await sleep(2000);
|
||||
await sleep(MONITOR_MS);
|
||||
continue;
|
||||
}
|
||||
const snap = JSON.parse(raw);
|
||||
@@ -268,10 +283,19 @@ export async function runSession(o) {
|
||||
if (Date.now() - lastStateAt > STATE_TIMEOUT_MS) {
|
||||
throw new Error('超过 ' + Math.round(STATE_TIMEOUT_MS / 1000) + ' 秒读不到脚本状态,重启浏览器');
|
||||
}
|
||||
await sleep(2000);
|
||||
await sleep(MONITOR_MS);
|
||||
continue;
|
||||
}
|
||||
lastStateAt = Date.now();
|
||||
// 面板每一轮都刷(快照本身就是最新状态,不需要另算);未启用时是空操作
|
||||
tui.snap(snap);
|
||||
if (!finishReason) {
|
||||
if (snap.runFinished) tui.set({ phase: '脚本已停止', tone: 'ok' });
|
||||
else if (snap.retryWait > 0) tui.set({ phase: '等待重试', tone: 'warn' });
|
||||
else if (snap.video && !snap.video.paused) tui.set({ phase: '刷课中', tone: 'ok' });
|
||||
else if (snap.route === 'list') tui.set({ phase: '列表页排队中', tone: 'info' });
|
||||
else tui.set({ phase: '运行中', tone: 'info' });
|
||||
}
|
||||
|
||||
// 编解码器缺失:视频元素有源但解码错误(MEDIA_ERR_DECODE / SRC_NOT_SUPPORTED)
|
||||
if (!codecWarned && snap.video && snap.video.src && snap.video.err >= 3) {
|
||||
@@ -321,12 +345,13 @@ export async function runSession(o) {
|
||||
break;
|
||||
}
|
||||
|
||||
await sleep(2000);
|
||||
await sleep(MONITOR_MS);
|
||||
}
|
||||
|
||||
// ---------------- 收尾 ----------------
|
||||
const elapsed = Math.round((Date.now() - startedAt) / 1000);
|
||||
const final = await page.tryEval(EXPR_FINAL_STATS).then((v) => (v ? JSON.parse(v) : {})).catch(() => ({}));
|
||||
tui.set({ phase: '本次会话结束,收尾中', tone: 'ok', completed: final.completed || 0 });
|
||||
log('本次会话结束:' + (finishReason || '收到退出信号') +
|
||||
' 用时 ' + Math.floor(elapsed / 60) + ' 分 ' + (elapsed % 60) + ' 秒' +
|
||||
' 本次完成 ' + (final.completed || 0) + ' 门,累计 ' + (final.doneCount || 0) + ' 门');
|
||||
|
||||
Reference in new issue
Block a user