fix: 移除课程门数上限,避免与每日积分目标冲突

maxCourses 按整门课计数,而积分按课时计;只剩考试的课会白占名额,
曾出现 5 门用光但今日视频积分只拿到 1/3 就停止的情况。现在只由
今日积分完成、没有可学内容或 budgetMinutes 决定收尾。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
voidandClaude Sonnet 4.6 committed 2026-11-02 19:36:57 +08:00
1 parent 0eb4c40629
commit f41fc72fe2
20 files changed
+45 -106

No files matched your search

+4 -12
View File
@@ -23,7 +23,6 @@ export const SCRIPT_FILE = path.join(ROOT, 'yun-xue-tang-assistant.user.js');
// 一层:内置默认值
// ---------------------------------------------------------------------------
const DEFAULTS = {
maxCourses: 0, // 0 = 不限(一直刷到没有未学完的课)
budgetMinutes: 0, // 0 = 不限
preferShort: true,
pointsMode: true, // 每日积分模式:按课时类型计分,四类封顶后停止(文档1/视频3/音频2/微课2)
@@ -56,7 +55,6 @@ const DEPRECATED = {
};
/** 旧参数名 -> 配置字段(保持向后兼容,dev/tests/ 下的脚本还在用) */
const ALIASES = {
'--max': 'maxCourses',
'--budget': 'budgetMinutes',
'--restart': 'restartMinutes',
'--idle': 'idleRestartMinutes',
@@ -110,10 +108,10 @@ if (CONFIG_FILE) {
// 命令行解析:--key value / --key=value / --flag
// ---------------------------------------------------------------------------
const BOOL_KEYS = ['preferShort', 'pointsMode', 'headless', 'check', 'tui', 'color'];
const NUM_KEYS = ['maxCourses', 'budgetMinutes', 'restartMinutes', 'idleRestartMinutes', 'port', 'logMaxBytes', 'barkTtl'];
const NUM_KEYS = ['budgetMinutes', 'restartMinutes', 'idleRestartMinutes', 'port', 'logMaxBytes', 'barkTtl'];
const STR_KEYS = ['origin', 'url', 'profile', 'logFile', 'chrome', 'extraArgs', 'config', 'barkUrl', 'barkGroup'];
/** kebab-case → camelCase:--max-courses → maxCourses */
/** kebab-case → camelCase:--budget-minutes → budgetMinutes */
function toCamel(flag) {
return flag.replace(/^--/, '').replace(/-([a-z])/g, (m, c) => c.toUpperCase());
}
@@ -207,7 +205,6 @@ const envStr = (name, cur) => {
// ---------------------------------------------------------------------------
const merged = { ...DEFAULTS, ...fileConfig };
merged.maxCourses = envNum('YUN_XUE_TANG_MAX_COURSES', merged.maxCourses);
merged.budgetMinutes = envNum('YUN_XUE_TANG_BUDGET_MINUTES', merged.budgetMinutes);
merged.restartMinutes = envNum('YUN_XUE_TANG_RESTART_MINUTES', merged.restartMinutes);
merged.idleRestartMinutes = envNum('YUN_XUE_TANG_IDLE_RESTART_MINUTES', merged.idleRestartMinutes);
@@ -249,7 +246,6 @@ for (const [key, raw] of Object.entries(cliRaw)) {
const problems = [...numBad];
if (numBad.length === 0) {
if (!(merged.port >= 1 && merged.port <= 65535)) problems.push('port 必须在 1-65535 之间,收到 ' + merged.port);
if (!(merged.maxCourses >= 0)) problems.push('maxCourses 不能为负,收到 ' + merged.maxCourses);
if (!(merged.budgetMinutes >= 0)) problems.push('budgetMinutes 不能为负,收到 ' + merged.budgetMinutes);
if (!(merged.restartMinutes >= 0)) problems.push('restartMinutes 不能为负,收到 ' + merged.restartMinutes);
if (!(merged.idleRestartMinutes >= 0)) problems.push('idleRestartMinutes 不能为负,收到 ' + merged.idleRestartMinutes);
@@ -282,8 +278,6 @@ export const START_URL = merged.url ? String(merged.url) : ORIGIN + '/login.html
export const USR = process.env.YUN_XUE_TANG_USR || '';
export const PWD = process.env.YUN_XUE_TANG_PWD || '';
export const PORT = merged.port;
/** 本次最多完成几门课(0 = 不限) */
export const MAX_COURSES = merged.maxCourses;
/** 运行多少分钟后收尾(0 = 不限) */
export const BUDGET_MINUTES = merged.budgetMinutes;
export const PROFILE_DIR = merged.profile
@@ -352,7 +346,6 @@ export function describeRun() {
checkOnly: CHECK_ONLY,
headless: !VISIBLE,
port: PORT,
maxCourses: MAX_COURSES > 0 ? MAX_COURSES : '不限',
budgetMinutes: BUDGET_MINUTES > 0 ? BUDGET_MINUTES : '不限',
preferShort: PREFER_SHORT,
pointsMode: POINTS_MODE,
@@ -370,7 +363,6 @@ export function describeRun() {
export function helpText() {
const rows = [
['配置字段', '命令行', '环境变量', '当前生效', '说明'],
['maxCourses', '--max-courses', 'YUN_XUE_TANG_MAX_COURSES', MAX_COURSES, '本次最多完成几门课;0 = 不限'],
['budgetMinutes', '--budget-minutes', 'YUN_XUE_TANG_BUDGET_MINUTES', BUDGET_MINUTES, '跑多少分钟后收尾;0 = 不限'],
['preferShort', '--prefer-short', 'YUN_XUE_TANG_PREFER_SHORT', PREFER_SHORT, '优先总时长最短的课程'],
['pointsMode', '--points-mode', 'YUN_XUE_TANG_POINTS_MODE', POINTS_MODE, '每日积分模式:文档1/视频3/音频2/微课2,封顶即停'],
@@ -410,11 +402,11 @@ export function helpText() {
lines.push('');
lines.push('示例:');
lines.push(' node runner/headless.mjs --check # 只做环境自检');
lines.push(' node runner/headless.mjs --max-courses 3 # 本次最多 3 门');
lines.push(' node runner/headless.mjs --budget-minutes 15 # 最多跑 15 分钟');
lines.push(' node runner/headless.mjs --headless 0 # 开有界面浏览器调试');
lines.push(' node runner/headless.mjs --no-tui # 不用面板,回到逐行日志');
lines.push(' node runner/headless.mjs --config ./config/runner.config.mjs # 显式指定配置文件');
lines.push('');
lines.push('旧参数名(仍然兼容):--max / --budget / --restart / --idle / --log / --log-max / --visible');
lines.push('旧参数名(仍然兼容):--budget / --restart / --idle / --log / --log-max / --visible');
return lines.join('\n');
}
+1 -2
View File
@@ -7,7 +7,7 @@
* 这个文件只做「组装」:解析参数 → 启动前预检 → 交给 supervisor 跑。
* 具体实现在同目录的模块里:
*
* config.mjs 参数与环境变量(--max / YUN_XUE_TANG_* 全部集中在这里)
* config.mjs 参数与环境变量(--budget / YUN_XUE_TANG_* 全部集中在这里)
* platform.mjs 平台差异(有无图形会话、沙箱参数、进程树收尾、端口排查命令)
* preflight.mjs 启动前检查(产物 / Node 版本 / 凭据 / 浏览器 / 端口)
* log.mjs 终端 + 文件日志(超上限轮转)
@@ -32,7 +32,6 @@
* 常用参数 / 环境变量:
* --check 只自检:浏览器可启动、页面可访问、脚本可注入、视频编解码可用,
* 不登录、不点课程、不动进度
* --max N 本次最多完成 N 门课(0 = 不限,默认读脚本自身配置)
* --budget N 运行 N 分钟后收尾(0 = 不限,默认 0)
* --profile DIR 持久化 profile 目录(默认 ./.run-profile)
* --port N CDP 调试端口(默认 9336)
+1 -6
View File
@@ -63,14 +63,13 @@ export const EXPR_SELF_CHECK = `JSON.stringify((function () {
};
})())`;
/** 看护循环:判断是否需要注入 / 是否在登录页 / 当前 --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,
})`;
/** 主监控快照:课程、课时、进度、倒计时、视频状态 */
@@ -137,10 +136,6 @@ export const EXPR_PAUSE_AND_SAVE = `(function () {
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;
+8 -26
View File
@@ -1,13 +1,13 @@
// 单次会话:一个浏览器进程的生命周期。
//
// 顺序:起浏览器 → 建标签页 → 连 CDP → 转发页面日志 → 每 1.5 秒看护
// (补注入 / 自动重登 / 启动批量 / 同步 --max)→ 主监控(进度、卡死、退出条件)→ 收尾。
// (补注入 / 自动重登 / 启动批量)→ 主监控(进度、卡死、退出条件)→ 收尾。
// 崩溃或断线时直接抛错,由 supervisor.mjs 负责重建浏览器。
import { connect, httpJson, sleep } from './cdp.mjs';
import { launchBrowser } from './browser.mjs';
import {
createPage, EXPR_WATCH, EXPR_SNAPSHOT, EXPR_FINAL_STATS, EXPR_PAUSE_AND_SAVE,
exprSetMaxCourses, exprStartBatch, exprAutoLogin,
exprStartBatch, exprAutoLogin,
} from './page.mjs';
import { runSelfCheck } from './selfcheck.mjs';
@@ -57,7 +57,6 @@ export async function runSession(o) {
let loginLastAt = 0;
let finished = false;
let finishReason = '';
let limitReached = false;
let lastSig = '';
let lastStateAt = Date.now();
let codecWarned = false;
@@ -65,8 +64,6 @@ export async function runSession(o) {
// 拿不到主循环的控制流,必须把错误寄存下来,由主循环抛出。
let fatalError = null;
const consoleErrors = [];
/** 是否达到配置的上限(maxCourses=0 表示不限) */
const limitReachedByConfig = (completed) => cfg.MAX_COURSES > 0 && completed >= cfg.MAX_COURSES;
const page = {
evalJS: null, json: null, tryEval: null,
@@ -131,7 +128,7 @@ export async function runSession(o) {
});
// ---------------- 看护循环 ----------------
// 每 WATCHDOG_MS 跑一次:新 document 补注入;掉登录自动重登;启动/接续批量;同步 --max。
// 每 WATCHDOG_MS 跑一次:新 document 补注入;掉登录自动重登;启动/接续批量。
const tickWatchdog = async () => {
if (disconnected || stop.stopping) return;
const s = await page.tryEval(EXPR_WATCH).then((v) => (v ? JSON.parse(v) : null));
@@ -157,12 +154,6 @@ export async function runSession(o) {
return;
}
// 运行策略同步到页面:maxCourses > 0 时覆盖脚本内置的 CFG.maxCourses;
// 配成 0 表示「不限」,也要推 0 过去,否则页面里仍是脚本自己的 10 门上限。
if (s.maxCourses !== null && s.maxCourses !== cfg.MAX_COURSES) {
await page.tryEval(exprSetMaxCourses(cfg.MAX_COURSES));
}
// 掉登录:用环境变量里的账号自动重登(限速 + 次数上限)
if (s.onLogin) {
if (loginAttempts >= MAX_LOGIN_ATTEMPTS) {
@@ -200,13 +191,10 @@ export async function runSession(o) {
const st = JSON.parse(raw);
if (st.batchFinished) {
if (limitReachedByConfig(st.completed)) {
limitReached = true;
finishReason = '已完成 ' + st.completed + ' 门(达到上限 maxCourses=' + cfg.MAX_COURSES + ')';
} else if (cfg.RESTART_MINUTES > 0) {
if (cfg.RESTART_MINUTES > 0) {
finishReason = '脚本已停止(本次完成 ' + st.completed + ' 门),按 YUN_XUE_TANG_RESTART_MINUTES 稍后重新开始';
} else {
finishReason = '脚本已停止(本次完成 ' + st.completed + ' 门;无更多可学课程或达到脚本内上限)';
finishReason = '脚本已停止(本次完成 ' + st.completed + ' 门;无更多可学课程)';
}
finished = true;
return;
@@ -237,7 +225,6 @@ export async function runSession(o) {
}
log('模式:无头 ' + (browser.headless ? 'headless' : '否') +
' 上限 ' + (cfg.MAX_COURSES > 0 ? cfg.MAX_COURSES + ' 门' : '不限') +
' 预算 ' + (cfg.BUDGET_MINUTES > 0 ? cfg.BUDGET_MINUTES + ' 分钟' : '不限') +
' 短课程优先 ' + (cfg.PREFER_SHORT ? '开' : '关') +
' 每日积分 ' + (cfg.POINTS_MODE ? '开' : '关') +
@@ -323,7 +310,7 @@ export async function runSession(o) {
if (snap.lesson) parts.push('课时 ' + String(snap.lesson).slice(0, 24));
if (snap.summary && snap.summary.text) parts.push(String(snap.summary.text).slice(0, 24));
if (snap.countdown != null) parts.push('倒计时 ' + snap.countdown + 's');
parts.push('本次完成 ' + snap.completed + (cfg.MAX_COURSES > 0 ? '/' + cfg.MAX_COURSES : ''));
parts.push('本次完成 ' + snap.completed);
if (snap.retryWait) parts.push('等待重试 ' + snap.retryWait + 's');
else if (snap.retryCount) parts.push('待重试 ' + snap.retryCount);
log(parts.join(' '));
@@ -334,12 +321,7 @@ export async function runSession(o) {
log('✔ 已完成 ' + snap.completed + ' 门(累计记录 ' + snap.doneCount + ' 门)');
}
// 到达上限 / 脚本停止 / 长时间无进展
if (limitReachedByConfig(snap.completed)) {
limitReached = true;
finishReason = '已完成 ' + snap.completed + ' 门(达到上限 maxCourses=' + cfg.MAX_COURSES + ')';
break;
}
// 脚本停止 / 长时间无进展
if (snap.runFinished) { finishReason = '脚本已停止:' + (snap.status || '未知原因'); break; }
if (Date.now() - lastProgressAt > stallLimit) {
finishReason = '超过 ' + Math.round(stallLimit / 60000) + ' 分钟没有任何进展(最后停在 ' +
@@ -366,7 +348,7 @@ export async function runSession(o) {
activeCleanup = null;
return {
finishReason, limitReached, completed: final.completed || 0, elapsed, injected, ok: true,
finishReason, completed: final.completed || 0, elapsed, injected, ok: true,
points: final.points || null,
};
}
+3 -4
View File
@@ -41,7 +41,6 @@ export async function runSupervisor(o) {
else if (typeof stop.request === 'function') stop.request(reason);
},
});
tui.set({ maxCourses: cfg.MAX_COURSES });
log('=== 云学堂助手 · 无头运行器' + (cfg.CHECK_ONLY ? '(自检)' : '') + ' 启动 ===');
log('Node ' + process.version + ' ' + platformName + '/' + process.arch);
@@ -102,8 +101,8 @@ export async function runSupervisor(o) {
// 只在最终收尾推就等于永远不推。重复结论由 notify() 的识别键挡掉。
tui.set({ phase: '发送 Bark 通知', tone: 'info' });
await notify(finishResult(last));
// 正常结束:只有「脚本自己停了 / 无进展」且配了重启间隔才再来一轮
const restartable = !last.limitReached && cfg.RESTART_MINUTES > 0;
// 正常结束:配了重启间隔就再来一轮
const restartable = cfg.RESTART_MINUTES > 0;
if (!restartable) break;
log('按 YUN_XUE_TANG_RESTART_MINUTES=' + cfg.RESTART_MINUTES + ' 等待后重新开始…');
tui.set({ phase: '等待 ' + cfg.RESTART_MINUTES + ' 分钟后重开', tone: 'ok' });
@@ -134,7 +133,7 @@ export async function runSupervisor(o) {
const stalled = !!(last && /没有任何进展/.test(last.finishReason || ''));
const exitCode = cfg.CHECK_ONLY ? (last && last.ok ? 0 : 1) : (stalled ? 1 : 0);
// 0 = 正常收尾(达到上限 / 预算 / 无更多课程);1 = 无进展卡死;3 = 连续登录失败
// 0 = 正常收尾(预算 / 积分完成 / 无更多课程);1 = 无进展卡死;3 = 连续登录失败
const phase = cfg.CHECK_ONLY
? (exitCode === 0 ? '自检通过' : '自检失败')
: stalled ? '退出:长时间无进展' : '正常收尾';
+1 -3
View File
@@ -171,7 +171,6 @@ export function createTui(opts = {}) {
summary: '',
countdown: null,
completed: 0,
maxCourses: 0,
doneCount: 0,
retryCount: 0,
retryWait: 0,
@@ -264,8 +263,7 @@ export function createTui(opts = {}) {
function countValue(width) {
// 字段按重要性递降:本次/累计是骨架,其余在窄终端里从右往左丢,不硬截断
const parts = ['本次 ' + state.completed + (state.maxCourses > 0 ? '/' + state.maxCourses : ''),
'累计 ' + state.doneCount];
const parts = ['本次 ' + state.completed, '累计 ' + state.doneCount];
const extra = [];
if (state.lessons) extra.push('课时数 ' + state.lessons);
if (state.retryWait > 0) extra.push(C.yellow('等待重试 ' + state.retryWait + 's'));