refactor: 拆分 src/ 模块并接入 esbuild 构建
把单文件脚本拆成 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 改成构建期注入版本号。
This commit is contained in:
1 parent
8200bfa0c5
commit
8193326b70
19 files changed
+4884
-1671
No files matched your search
+369
@@ -0,0 +1,369 @@
|
||||
// ======== 课程页 ========
|
||||
import { CFG, TAG } from './config.js';
|
||||
import { R, S, save } from './state.js';
|
||||
import { goToList, mediaPage } from './route.js';
|
||||
import { courseIdFromUrl, norm, textOf, truncate, findVisibleClickable } from './utils.js';
|
||||
import { later } from './lifecycle.js';
|
||||
import { setStatus, statusEl } from './panel.js';
|
||||
import {
|
||||
alignToFirstIncomplete, courseHasQuizLesson, detectQuiz, handleAntiIdle, courseTitle,
|
||||
hookAllVideos, findPlayableVideo, ensurePlaying, readSummary, readCountdown,
|
||||
readActiveLessonKey, findNextLessonElement, findNextButton, clickNode,
|
||||
fireClickSequence, lessonClickNodes, syncManualCourse, pauseAllMedia,
|
||||
} from './player.js';
|
||||
import { completeCurrent, failCurrent } from './complete.js';
|
||||
|
||||
export function courseTick() {
|
||||
R.listWithCurrentSince = 0;
|
||||
if (!S.current) {
|
||||
goToList();
|
||||
return;
|
||||
}
|
||||
|
||||
// 播放器可能是点「开始学习」后才创建的;每轮顺带接管一次,
|
||||
// 新视频一出现就静音并挂上 play/ended/timeupdate 监听。
|
||||
hookAllVideos();
|
||||
|
||||
const isMedia = mediaPage();
|
||||
const urlId = courseIdFromUrl();
|
||||
const curId = S.current.id || '';
|
||||
const title = courseTitle();
|
||||
const nt = norm(title);
|
||||
const nc = norm(S.current.title);
|
||||
const genericTitle = !nt || /^(loading|加载中|在线课堂|江丰云课堂|云课堂|课程详情|课程)$/.test(nt);
|
||||
const titleSame = genericTitle || !nc || nt === nc ||
|
||||
nt.indexOf(nc) >= 0 || nc.indexOf(nt) >= 0;
|
||||
// 有了 URL 里的 kngId 就以 id 为准:标题在 SPA 切页时经常短暂是 loading/
|
||||
// 在线课堂,旧逻辑会把它当成「课程不一致」在 12 秒后把课退回列表。
|
||||
const idKnown = !!(urlId && curId);
|
||||
const sameCourse = idKnown ? urlId === curId : titleSame;
|
||||
const graceOver = Date.now() - (S.current.startedAt || 0) > CFG.entryGraceMs;
|
||||
if (!sameCourse) {
|
||||
if (graceOver) {
|
||||
console.warn(TAG, '页面课程与队列不一致:队列', nc, curId,
|
||||
'页面', nt, urlId, location.href);
|
||||
failCurrent('页面课程与队列不一致');
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (S.phase !== 'learning') {
|
||||
S.phase = 'learning';
|
||||
save();
|
||||
}
|
||||
|
||||
// #/scan 只是进课过渡页:此时页面组件里可能还残留上一门课的汇总,
|
||||
// 等路由切到真正的播放页再判断,避免把旧课程的「已完成学习」当成新课程完成。
|
||||
if ((location.hash || '').indexOf('#/scan') === 0) return;
|
||||
|
||||
// 1) 防挂机验证(每 10 分钟弹一次,120 秒内不点「继续学习」就停止计时)
|
||||
if (handleAntiIdle()) return;
|
||||
|
||||
// 2) 答题/考试检测
|
||||
if (detectQuiz()) {
|
||||
failCurrent('检测到答题/考试,需要手动处理');
|
||||
return;
|
||||
}
|
||||
|
||||
// 3) 课程是否包含考试/测验类章节(只需检查一次)
|
||||
if (!R.quizChecked && document.querySelector('.yxtulcdsdk-catalog ul > li .tag-type')) {
|
||||
R.quizChecked = true;
|
||||
if (courseHasQuizLesson()) {
|
||||
failCurrent('课程包含考试/测验章节');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 4) 整门课程是否已完成 / 平台是否刚记录了一节
|
||||
const summary = readSummary();
|
||||
const prevSummaryX = R.lastSummary ? R.lastSummary.x : -1;
|
||||
if (summary) {
|
||||
// 三种完成文案统一走这里:x/y 满、百分比 100%、「已完成学习」。
|
||||
// 页面切换瞬间可能读到上一门课的残留文案,连续两次读到才确认。
|
||||
if (summary.complete) {
|
||||
const confirmed = !!(R.lastSummary && R.lastSummary.complete);
|
||||
R.lastSummary = summary;
|
||||
if (confirmed) {
|
||||
completeCurrent();
|
||||
return;
|
||||
}
|
||||
setStatus('课程已完成,确认中…');
|
||||
return;
|
||||
}
|
||||
|
||||
if (R.lastSummary && summary.x > R.lastSummary.x) {
|
||||
R.lastProgressAt = Date.now();
|
||||
const prevKey = R.lastLessonKey;
|
||||
const curKey = readActiveLessonKey();
|
||||
R.lastSummary = summary;
|
||||
|
||||
// 单视频/音频页的百分比是播放进度,不是「一节学完」,不能据此切节
|
||||
if (!isMedia && summary.kind !== 'percent' && (!prevKey || curKey === prevKey)) {
|
||||
// 平台已记账但没有自动切节 → 脚本主动切下一节
|
||||
setStatus('本节已记录,切换下一节…');
|
||||
advanceLesson();
|
||||
return;
|
||||
}
|
||||
setStatus(summary.kind === 'percent'
|
||||
? '学习进度:' + summary.x + '%'
|
||||
: '进度更新:' + summary.x + '/' + summary.y);
|
||||
return;
|
||||
}
|
||||
|
||||
R.lastSummary = summary;
|
||||
}
|
||||
|
||||
// 5) 对齐起点:进入课程后统一从「第一个未学完的课时」开始(即第一章第一节起顺序学),
|
||||
// 不沿用平台上次停留的位置。否则前面的课时会被跳过,整门课永远学不完。
|
||||
// 单视频/音频页没有目录,不走这一步。
|
||||
if (!isMedia && alignToFirstIncomplete()) return;
|
||||
|
||||
// 6) 计时与当前章节
|
||||
const cd = readCountdown();
|
||||
if (cd != null) {
|
||||
if (R.lastCountdown == null || cd < R.lastCountdown) {
|
||||
R.lastProgressAt = Date.now();
|
||||
}
|
||||
R.lastCountdown = cd;
|
||||
}
|
||||
|
||||
const lessonKey = readActiveLessonKey();
|
||||
if (lessonKey && lessonKey !== R.lastLessonKey) {
|
||||
R.lastLessonKey = lessonKey;
|
||||
R.doneLatchKey = '';
|
||||
R.waitStart = 0;
|
||||
R.waitSummaryX = -1;
|
||||
R.waitLessonKey = '';
|
||||
R.lastProgressAt = Date.now();
|
||||
setStatus('学习中:' + truncate(lessonKey, 28) + (R.alignFailed ? '(起点未对齐)' : ''));
|
||||
}
|
||||
|
||||
// 7) 本节是否已在本地结束(必须先判断,避免把已结束的视频重新拉回开头)
|
||||
const v = findPlayableVideo();
|
||||
// 视频 ended 是本节结束的主信号;倒计时文案在切节后可能变成下一节的时长。
|
||||
// doneLatchKey 用来记住「这节已经播完」:平台在完成后会把播放器重置到 0 秒,
|
||||
// video.ended 变回 false、倒计时也消失,若只看即时状态就会把视频从头重播。
|
||||
const latchKey = isMedia ? 'media' : (lessonKey || '');
|
||||
const cdDone = cd != null && cd <= 0;
|
||||
const ended = !!(v && v.ended);
|
||||
if (latchKey && R.doneLatchKey && R.doneLatchKey !== latchKey) R.doneLatchKey = '';
|
||||
if (latchKey && (cdDone || (ended && (cd == null || cd <= 3)))) {
|
||||
R.doneLatchKey = latchKey;
|
||||
}
|
||||
const localDone = ended || cdDone ||
|
||||
(!!latchKey && R.doneLatchKey === latchKey);
|
||||
|
||||
// 8) 播放器
|
||||
if (!localDone) {
|
||||
if (v) {
|
||||
ensurePlaying(v);
|
||||
const want = '学习中:' + truncate(lessonKey || S.current.title, 30);
|
||||
if (statusEl && (statusEl.textContent === '点击开始学习…' ||
|
||||
(isMedia && statusEl.textContent !== want))) {
|
||||
setStatus(want);
|
||||
}
|
||||
} else {
|
||||
const startBtn = findVisibleClickable(/^(开始学习|继续学习|继续观看)$/);
|
||||
if (startBtn && Date.now() - R.lastStartClickAt > CFG.startClickCooldownMs) {
|
||||
R.lastStartClickAt = Date.now();
|
||||
setStatus('点击开始学习…');
|
||||
console.log(TAG, '点击开始学习', textOf(startBtn) || startBtn.tagName);
|
||||
try { startBtn.click(); } catch (e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 9) 标记等待平台记录
|
||||
if (!localDone) {
|
||||
R.waitStart = 0;
|
||||
} else if (!R.waitStart) {
|
||||
R.waitStart = Date.now();
|
||||
R.waitSummaryX = prevSummaryX;
|
||||
R.waitLessonKey = lessonKey;
|
||||
setStatus(isMedia ? '课程已播完,等待平台记录…' : '本节结束,等待平台记录…');
|
||||
}
|
||||
|
||||
// 10) 等待平台记录本节完成
|
||||
if (R.waitStart) {
|
||||
const newKey = readActiveLessonKey();
|
||||
const s2 = readSummary();
|
||||
|
||||
// 整门课已完成(含「已完成学习」/百分比 100%)
|
||||
if (s2 && s2.complete) {
|
||||
const confirmed = !!(R.lastSummary && R.lastSummary.complete);
|
||||
R.lastSummary = s2;
|
||||
if (!confirmed) {
|
||||
setStatus('课程已完成,确认中…');
|
||||
return;
|
||||
}
|
||||
R.waitStart = 0;
|
||||
R.lastProgressAt = Date.now();
|
||||
completeCurrent();
|
||||
return;
|
||||
}
|
||||
|
||||
// 平台自动进入了下一节(只可能发生在有目录的多课时课程)
|
||||
if (!isMedia && R.waitLessonKey && newKey && newKey !== R.waitLessonKey) {
|
||||
R.waitStart = 0;
|
||||
R.lastLessonKey = newKey;
|
||||
R.doneLatchKey = '';
|
||||
R.lastProgressAt = Date.now();
|
||||
setStatus('已自动进入下一节');
|
||||
return;
|
||||
}
|
||||
|
||||
// 完成数增加 → 手动切下一节
|
||||
if (!isMedia && s2 && s2.x > R.waitSummaryX) {
|
||||
R.waitStart = 0;
|
||||
R.lastProgressAt = Date.now();
|
||||
if (s2.complete) {
|
||||
completeCurrent();
|
||||
} else {
|
||||
advanceLesson();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (Date.now() - R.waitStart > 30000) {
|
||||
if (isMedia) {
|
||||
// 单视频/音频:平台记账有延迟,继续等,不能把已播完的视频再拉回 0 秒
|
||||
R.waitStart = Date.now();
|
||||
setStatus('课程已播完,等待平台完成记录…');
|
||||
} else {
|
||||
R.waitStart = 0;
|
||||
setStatus('平台未更新进度,尝试下一节…');
|
||||
advanceLesson();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 11) 长时间无进展 → 放弃
|
||||
if (Date.now() - R.lastProgressAt > CFG.stuckMs) {
|
||||
failCurrent('长时间没有计时进展');
|
||||
}
|
||||
}
|
||||
|
||||
// 自动切下一节:先点标题(事件会冒泡到行容器 / li 上的句柄),
|
||||
// 平台没反应时再依次补点行容器、li、状态图标;每一步都先确认目标课时还没
|
||||
// 变成当前课时,避免平台自己已经切过去之后又被点回来。
|
||||
export function switchLesson(li) {
|
||||
if (!li) return;
|
||||
const want = norm(textOf(li));
|
||||
const nodes = lessonClickNodes(li);
|
||||
const svg = li.querySelector('svg');
|
||||
const steps = [
|
||||
{ at: 0, run: function () { clickNode(nodes[0]); } },
|
||||
{ at: 500, run: function () { if (nodes[1]) clickNode(nodes[1]); } },
|
||||
{ at: 1000, run: function () { fireClickSequence(li); } },
|
||||
{ at: 1500, run: function () { if (svg) fireClickSequence(svg); } },
|
||||
];
|
||||
for (const step of steps) {
|
||||
if (!step.at) {
|
||||
step.run();
|
||||
continue;
|
||||
}
|
||||
later(function () {
|
||||
if (want && readActiveLessonKey() === want) return;
|
||||
step.run();
|
||||
}, step.at);
|
||||
}
|
||||
}
|
||||
|
||||
export function advanceLesson() {
|
||||
if (Date.now() - R.lastAdvanceAt < 5000) return;
|
||||
R.lastAdvanceAt = Date.now();
|
||||
R.lastCountdown = null;
|
||||
R.lastLessonKey = '';
|
||||
R.doneLatchKey = '';
|
||||
R.waitStart = 0;
|
||||
|
||||
const next = findNextLessonElement();
|
||||
if (next) {
|
||||
setStatus('进入下一节');
|
||||
switchLesson(next);
|
||||
return;
|
||||
}
|
||||
const btn = findNextButton();
|
||||
if (btn) {
|
||||
setStatus('进入下一节');
|
||||
clickNode(btn);
|
||||
return;
|
||||
}
|
||||
|
||||
const s = readSummary();
|
||||
if (s && s.complete) {
|
||||
completeCurrent();
|
||||
} else {
|
||||
setStatus('没有找到下一节,等待课程完成…');
|
||||
}
|
||||
}
|
||||
|
||||
// ======== 手动课程自动连播 ========
|
||||
export function manualCourseTick() {
|
||||
hookAllVideos();
|
||||
if (handleAntiIdle()) return;
|
||||
|
||||
// 课程标题就绪前不做任何判断,避免在旧课程的目录上点来点去
|
||||
const title = courseTitle();
|
||||
if (!title || /^(loading|加载中)/i.test(title)) return;
|
||||
|
||||
// 手动进入课程时同样先对齐到「第一个未学完的课时」;每门课只对齐一次,
|
||||
// 之后用户自己点别的课时不会被拉回来
|
||||
syncManualCourse();
|
||||
if (!mediaPage() && alignToFirstIncomplete()) return;
|
||||
|
||||
const lessonKey = readActiveLessonKey();
|
||||
if (lessonKey && lessonKey !== R.lastLessonKey) {
|
||||
R.lastLessonKey = lessonKey;
|
||||
R.doneLatchKey = '';
|
||||
setStatus('学习中:' + truncate(lessonKey, 28));
|
||||
}
|
||||
|
||||
// 整门课已完成(平台汇总会变成「已完成学习」/100%):停在这里,别再重播
|
||||
const summary = readSummary();
|
||||
if (summary && summary.complete) {
|
||||
pauseAllMedia();
|
||||
setStatus('课程已完成:' + truncate(S.current ? S.current.title : title, 24));
|
||||
return;
|
||||
}
|
||||
|
||||
const v = findPlayableVideo();
|
||||
if (!v) {
|
||||
// 单视频/音频页初始只有封面和「开始学习」,手动模式也要能点开
|
||||
if (!mediaPage()) return;
|
||||
const startBtn = findVisibleClickable(/^(开始学习|继续学习|继续观看)$/);
|
||||
if (startBtn && Date.now() - R.lastStartClickAt > CFG.startClickCooldownMs) {
|
||||
R.lastStartClickAt = Date.now();
|
||||
setStatus('点击开始学习…');
|
||||
try { startBtn.click(); } catch (e) {}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const cd = readCountdown();
|
||||
if (cd != null) {
|
||||
if (R.lastCountdown == null || cd < R.lastCountdown) R.lastProgressAt = Date.now();
|
||||
R.lastCountdown = cd;
|
||||
}
|
||||
|
||||
if (v.ended || (cd != null && cd <= 0)) R.doneLatchKey = 'manual';
|
||||
const localDone = v.ended || (cd != null && cd <= 0) || R.doneLatchKey === 'manual';
|
||||
|
||||
if (localDone) {
|
||||
if (CFG.autoNext && Date.now() - R.manualAdvanceAt > 6000) {
|
||||
R.manualAdvanceAt = Date.now();
|
||||
const next = findNextLessonElement();
|
||||
if (next) {
|
||||
switchLesson(next);
|
||||
} else {
|
||||
const btn = findNextButton();
|
||||
if (btn) clickNode(btn);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ensurePlaying(v);
|
||||
}
|
||||
Reference in new issue
Block a user