把单文件脚本拆成 src/ 下 13 个 ES 模块,用 esbuild 打回同名 .user.js, 产物文件名不变,dev/ 热加载链路无需改动。 - build.mjs:esbuild 打包(--watch / --minify),版本号只从 package.json 读取,同时替换 meta.js 的 @version 与 config.js 的 VERSION - 统一版本号来源,消除 package.json / meta.js / config.js 三处手写 - 补回 'use strict'(esbuild IIFE 默认不带,属真实行为回归) - 清理重复实现:pauseAllMedia、listData、shortQuery、drain、clearPending、 cbAuto 模块级引用、共用 cssText - 去掉搬代码时残留的 IIFE 缩进、死字段、二次兜底默认值、过期注释 - README / dev/README 同步构建流程与目录结构 行为无变化,仅 src/config.js 的 CFG 改成构建期注入版本号。
95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
// ======== 主循环 ========
|
|
'use strict';
|
|
import { CFG, VERSION, TAG } from './config.js';
|
|
import { R, S, state, save } from './state.js';
|
|
import { every, later, onWindow } from './lifecycle.js';
|
|
import { courseIdFromUrl } from './utils.js';
|
|
import { route, mediaPage } from './route.js';
|
|
import { ensurePanel, setStatus } from './panel.js';
|
|
import { isLoginPage, loginPageTick, doLoginAndStart, fillLoginForm, startBatch, consumePendingStart, setTick } from './login.js';
|
|
import {
|
|
listTick, listVm, shortCatalogKey, courseIdOfCard, ensureShortCatalog,
|
|
chooseShortCourse, sortCardsByDuration, formatDuration,
|
|
pendingShortItems, forgetShortCourse,
|
|
} from './list.js';
|
|
import { courseTick, manualCourseTick } from './course.js';
|
|
import {
|
|
hookAllVideos, readSummary, readCountdown, lessonItems,
|
|
findNextLessonElement, clickLesson, alignToFirstIncomplete,
|
|
isLessonCompleted, findFirstIncompleteLessonElement, dumpCatalog,
|
|
} from './player.js';
|
|
|
|
function tick() {
|
|
try {
|
|
if (window.top !== window.self) return;
|
|
ensurePanel();
|
|
if (isLoginPage()) loginPageTick();
|
|
if (consumePendingStart()) return;
|
|
|
|
if (!S.running) {
|
|
if (route() === 'course' && state.enabled) {
|
|
manualCourseTick();
|
|
} else {
|
|
R.manualCourseKey = ''; // 离开课程页后,下次进入同一门课要重新对齐
|
|
}
|
|
return;
|
|
}
|
|
|
|
const r = route();
|
|
if (r === 'list') {
|
|
listTick();
|
|
} else if (r === 'course') {
|
|
courseTick();
|
|
}
|
|
} catch (e) {
|
|
console.error(TAG, e);
|
|
}
|
|
}
|
|
|
|
// 把主循环回填给 login.startBatch(它在点击「一键刷课」后需要立即 tick 一次)
|
|
setTick(tick);
|
|
|
|
// iframe 内只负责接管视频
|
|
if (window.top !== window.self) {
|
|
every(function () {
|
|
try { hookAllVideos(); } catch (e) {}
|
|
}, CFG.tickMs);
|
|
} else {
|
|
ensurePanel();
|
|
hookAllVideos();
|
|
every(tick, CFG.tickMs);
|
|
onWindow(window, 'hashchange', function () {
|
|
later(tick, 300);
|
|
});
|
|
}
|
|
|
|
// 调试入口(仅本地使用,可在控制台查看状态)
|
|
try {
|
|
Object.defineProperty(window, '__ytAuto', {
|
|
value: {
|
|
S, R, state, tick, save, route,
|
|
mediaPage, courseIdFromUrl,
|
|
readCountdown, readSummary,
|
|
isLoginPage, loginPageTick,
|
|
doLoginAndStart, fillLoginForm,
|
|
startBatch, consumePendingStart,
|
|
findNextLessonElement, lessonItems,
|
|
dumpCatalog, clickLesson,
|
|
alignToFirstIncomplete,
|
|
isLessonCompleted,
|
|
findFirstIncompleteLessonElement,
|
|
listVm, shortCatalogKey,
|
|
courseIdOfCard,
|
|
ensureShortCatalog, chooseShortCourse,
|
|
sortCardsByDuration, formatDuration,
|
|
pendingShortItems, forgetShortCourse,
|
|
},
|
|
configurable: true,
|
|
});
|
|
} catch (e) { /* ignore */ }
|
|
|
|
console.log('%c云学堂助手 v' + VERSION + ' 已加载', 'color:#3c9;font-weight:bold');
|
|
setStatus(S.runFinished
|
|
? '本次已完成 ' + S.completedThisRun + ' 门(已停止)'
|
|
: (S.running ? '自动刷课运行中' : '待机'));
|