// 环境自检(--check):只读,不登录、不点课程、不动进度。 // // 检查项:页面能不能打开、脚本能不能注入、面板是否唯一可见、版本是否可读、 // 这个 Chromium 到底带不带 H.264/AAC(服务器上最常见的问题:装了发行版 chromium 却放不了视频)。 import { sleep } from './cdp.mjs'; import { EXPR_SELF_CHECK } from './page.mjs'; /** * @param {object} o * @param {object} o.page page.mjs 的 page 对象 * @param {Function} o.tickWatchdog 会话里负责注入 / 重登的看护函数 * @param {Function} o.isDisconnected 判断 CDP 是否已断开 * @param {number} [o.timeoutMs] 注入自检的最长等待 * @returns {Promise<{ok:boolean, failed:string[], reason:string}>} */ export async function runSelfCheck(o) { const timeoutMs = o.timeoutMs || 60000; const log = o.log; // 等页面加载完 + 注入成功(tickWatchdog 会补注入) const info = await (async () => { const started = Date.now(); while (Date.now() - started < timeoutMs) { if (o.isDisconnected()) throw new Error('CDP 连接断开'); try { await o.tickWatchdog(); const v = await o.page.json(EXPR_SELF_CHECK); if (v && v.api === 'object') return v; } catch (e) { /* 页面仍在加载 */ } await sleep(1000); } throw new Error(Math.round(timeoutMs / 1000) + ' 秒内未能完成注入自检'); })(); const failed = []; const ck = (name, ok, detail) => { log((ok ? ' PASS ' : ' FAIL ') + name + (detail === undefined ? '' : ' → ' + JSON.stringify(detail))); if (!ok) failed.push(name); }; log('--- 无头环境自检(只读)---'); ck('页面可访问', /yunxuetang\.cn$/.test(new URL(info.url).host), info.url); ck('脚本已注入 window.__yunXueTangAuto', info.api === 'object', info.api); ck('面板渲染唯一', info.panels === 1 && info.panelVisible, { 面板数: info.panels, 可见: info.panelVisible }); ck('脚本版本可读', /^v\d+\.\d+\.\d+$/.test(info.version || ''), info.version); ck('视频编解码 H.264 可用', info.codecs.h264 === 'probably' || info.codecs.h264 === 'maybe', { h264: info.codecs.h264, aac: info.codecs.aac, vp9: info.codecs.vp9 }); log(' 提示:H.264 为 "maybe" 通常仍可播放;为空字符串说明该 Chromium 不带专有编解码器,'); log(' 请改装 google-chrome-stable(官方包带 H.264/AAC),否则课程视频会一直是 0 秒。'); log(' 页面识别的路由:' + info.route + ' 是否登录页:' + info.isLoginPage); log(' UA:' + String(info.ua).slice(0, 110)); if (info.fonts && info.fonts !== 'ok') { log(' 提示:中文字体可能缺失(fonts=' + info.fonts + '),只影响截图观感;需要截图时 apt install fonts-noto-cjk'); } return { ok: failed.length === 0, failed, reason: failed.length ? '自检失败:' + failed.join('、') : '', }; }