把单文件脚本拆成 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 改成构建期注入版本号。
2499 lines
81 KiB
JavaScript
2499 lines
81 KiB
JavaScript
// ==UserScript==
|
||
// @name 云学堂助手
|
||
// @namespace yun-xue-tang-assistant.local
|
||
// @version 2.7.0
|
||
// @description 云学堂助手:自动登录、从第一节顺序听课、自动切节、处理防挂机验证,并按列表批量推进课程(单视频/音频课程也支持,学完自动进入下一门,可优先短课程)
|
||
// @match *://kfmic.yunxuetang.cn/*
|
||
// @match *://*.yunxuetang.cn/*
|
||
// @grant none
|
||
// @run-at document-idle
|
||
// ==/UserScript==
|
||
|
||
/*
|
||
* 用法:
|
||
* 1. 浏览器安装 Tampermonkey,新建脚本,粘贴本文件全部内容并保存。
|
||
* 2. 打开 https://kfmic.yunxuetang.cn/login.html,在右下角面板填账号/密码,
|
||
* 点「一键登录并刷课」;登录成功后会自动进入课程列表并开始批量学习。
|
||
* 3. 也可以手动登录后,在列表页点「一键刷课」。
|
||
*
|
||
* 密码保存:
|
||
* - 只有勾选「记住密码」才会写入本机 localStorage,使用 base64 混淆(不是加密,
|
||
* 本机其他脚本仍可读取)。公用电脑请不要勾选。
|
||
* - 不勾选时密码只用于本次登录,不会落盘。
|
||
*
|
||
* 重要说明:
|
||
* - 平台按“真实播放时长”计时,快进、倍速都不省时间,脚本只做自动播放和自动切节。
|
||
* - 同时支持多课时课程(#/course/play)和单视频/音频/课件播放页(#/video/play 等):
|
||
* 单视频课程会自动点「开始学习」;平台汇总变成「已完成学习」/100%/x=y 时判定整门课
|
||
* 完成,自动回列表并进入下一门。
|
||
* - 进入课程(批量或手动打开)都会先对齐到「第一个未学完的课时」,即从第一章第一节起
|
||
* 顺序学习,不沿用平台上次停留的位置(否则前面的课时会被跳过,整门课永远学不完)。
|
||
* 切换成功与否以目录高亮为准:没切过去会重试并如实提示「未能切换」,不会假装已切换。
|
||
* - 试跑稳定后,把 config.js 里的 CFG.maxCourses 改成 0 即可不限门数(会持续很久)。
|
||
* - 平台若有防挂机检测/答题环节,脚本会跳过该课程并在面板提示。
|
||
* 防挂机验证(每 10 分钟一次、120 秒限时)会自动点「继续学习」;
|
||
* 答题/考试、滑块拼图类验证仍会跳过该课程。
|
||
* - 验证码/滑块需要手动完成,脚本不会绕过。
|
||
* - 课程/单位如有相关规定,请自行确认风险。
|
||
*
|
||
* 短课程优先:
|
||
* - 面板勾选「优先短课程(时长正序)」后,批量刷课会先读课程列表接口里的总时长,
|
||
* 按「未学完课程的总时长从短到长」选课,而不是按列表顺序。接口不可用时会自动
|
||
* 退回为「当前页按时长正序」,再不行就按原来的列表顺序,不会卡住批量任务。
|
||
*
|
||
* 开发热加载:
|
||
* - 本文件可直接被 dev/dev-loader.user.js 热注入:脚本暴露 window.__ytDispose(),
|
||
* 加载器在注入新版本前会先调用它清理定时器、监听器与面板,因此保存文件后
|
||
* 约 1 秒内即可生效,无需手动刷新页面。
|
||
*/
|
||
|
||
"use strict";
|
||
(() => {
|
||
// src/config.js
|
||
var CFG = {
|
||
mute: true,
|
||
// 静音(自动播放更稳)
|
||
autoNext: true,
|
||
// 手动打开课程时,本节结束后自动下一节
|
||
tickMs: 2e3,
|
||
// 主循环间隔
|
||
startClickCooldownMs: 3e3,
|
||
// 「开始学习」重复点击冷却(3 秒内没起播就补点)
|
||
entryGraceMs: 3e4,
|
||
// 进课后标题/课程 id 尚未就绪的宽限,避免慢加载被误判为「课程不一致」
|
||
stuckMs: 5 * 60 * 1e3,
|
||
// 连续多久没有计时进展就放弃当前课程
|
||
maxCourses: 10,
|
||
// 本次最多完成多少门;0 = 不限
|
||
maxConsecutiveFailures: 5,
|
||
// 连续失败多少门后自动停止
|
||
alignToFirstIncomplete: true,
|
||
// 进入课程先从「第一个未学完的课时」学起;false = 沿用平台上次位置
|
||
preferShortCourses: true
|
||
// 批量选课时优先总时长更短的课程(正序)
|
||
};
|
||
var VERSION = "2.7.0";
|
||
var LS_ENABLED = "yt-auto-play-enabled";
|
||
var LS_BATCH = "yt-batch-v2";
|
||
var LS_LOGIN = "yt-login-v1";
|
||
var LS_PENDING = "yt-auto-start-after-login";
|
||
var LS_PREFER_SHORT = "yt-prefer-short";
|
||
var SS_SHORT_CATALOG = "yt-short-catalog-v1";
|
||
var SHORT_CATALOG_TTL = 5 * 60 * 1e3;
|
||
var PANEL_ID = "yt-auto-panel";
|
||
var TAG = "[云学堂助手]";
|
||
|
||
// src/state.js
|
||
var state = {
|
||
enabled: true,
|
||
// 手动打开的课程:自动连播
|
||
preferShort: CFG.preferShortCourses
|
||
// 批量选课:优先时长短的课程(正序)
|
||
};
|
||
try {
|
||
state.enabled = localStorage.getItem(LS_ENABLED) !== "0";
|
||
} catch (e) {
|
||
}
|
||
try {
|
||
const v = localStorage.getItem(LS_PREFER_SHORT);
|
||
if (v === "0" || v === "1") state.preferShort = v === "1";
|
||
} catch (e) {
|
||
}
|
||
function defaultBatch() {
|
||
return {
|
||
running: false,
|
||
runFinished: false,
|
||
completedThisRun: 0,
|
||
consecutiveFailures: 0,
|
||
listUrl: "",
|
||
listPage: 1,
|
||
current: null,
|
||
// { title, id, status, startedAt, page, duration }
|
||
returning: false,
|
||
phase: "idle",
|
||
// idle | entering | learning
|
||
done: {},
|
||
// title -> timestamp
|
||
failed: {}
|
||
// title -> { reason, at }
|
||
};
|
||
}
|
||
var S = defaultBatch();
|
||
try {
|
||
const raw = localStorage.getItem(LS_BATCH);
|
||
if (raw) S = Object.assign(defaultBatch(), JSON.parse(raw));
|
||
} catch (e) {
|
||
}
|
||
function save() {
|
||
try {
|
||
localStorage.setItem(LS_BATCH, JSON.stringify(S));
|
||
} catch (e) {
|
||
}
|
||
}
|
||
var R = {
|
||
hooked: /* @__PURE__ */ new WeakSet(),
|
||
lastCardClickAt: 0,
|
||
entryRetries: 0,
|
||
// 点卡片后仍停在列表时,重试进入课程的次数
|
||
lastPageJumpAt: 0,
|
||
pageChangeAt: 0,
|
||
pageChangeSig: "",
|
||
listWithCurrentSince: 0,
|
||
lastGoListAt: 0,
|
||
lastStartClickAt: 0,
|
||
lastAdvanceAt: 0,
|
||
manualAdvanceAt: 0,
|
||
lastLessonKey: "",
|
||
doneLatchKey: "",
|
||
// 本节已播完(倒计时归零/video.ended),防止平台把视频拉回开头重播
|
||
lastSummary: null,
|
||
lastCountdown: null,
|
||
lastProgressAt: Date.now(),
|
||
waitStart: 0,
|
||
waitSummaryX: -1,
|
||
waitLessonKey: "",
|
||
quizChecked: false,
|
||
initialLessonChecked: false,
|
||
phaseSince: 0,
|
||
alignTries: 0,
|
||
alignStable: 0,
|
||
alignFailed: false,
|
||
manualCourseKey: "",
|
||
lastIdleClickAt: 0,
|
||
idlePassed: 0,
|
||
idleDialogText: "",
|
||
idleDialogClicks: 0,
|
||
idleGaveUp: false,
|
||
restoreAttempts: 0,
|
||
loginFilled: false,
|
||
loginSubmittingAt: 0,
|
||
loginAutoTried: false,
|
||
loginBoundUser: null,
|
||
loginBoundPwd: null,
|
||
// 短课程优先:课程时长队列(只在当前标签页会话内缓存)
|
||
shortCatalog: null,
|
||
shortCatalogLoading: false,
|
||
shortCatalogFailedAt: 0,
|
||
shortFetchStartedAt: 0,
|
||
shortJumpAt: 0,
|
||
shortNoVmSince: 0,
|
||
shortWarnedAt: 0
|
||
};
|
||
function resetCourseRuntime() {
|
||
R.lastLessonKey = "";
|
||
R.doneLatchKey = "";
|
||
R.lastSummary = null;
|
||
R.lastCountdown = null;
|
||
R.lastProgressAt = Date.now();
|
||
R.waitStart = 0;
|
||
R.waitSummaryX = -1;
|
||
R.waitLessonKey = "";
|
||
R.quizChecked = false;
|
||
R.initialLessonChecked = false;
|
||
R.phaseSince = 0;
|
||
R.alignTries = 0;
|
||
R.alignStable = 0;
|
||
R.alignFailed = false;
|
||
R.manualCourseKey = "";
|
||
R.lastAdvanceAt = 0;
|
||
R.restoreAttempts = 0;
|
||
R.entryRetries = 0;
|
||
}
|
||
|
||
// src/lifecycle.js
|
||
var TIMERS = [];
|
||
var DELAYS = [];
|
||
var WINDOW_LISTENERS = [];
|
||
var ELEMENT_LISTENERS = [];
|
||
function every(fn, ms) {
|
||
const id = setInterval(fn, ms);
|
||
TIMERS.push(id);
|
||
return id;
|
||
}
|
||
function later(fn, ms) {
|
||
const id = setTimeout(function() {
|
||
const i = DELAYS.indexOf(id);
|
||
if (i >= 0) DELAYS.splice(i, 1);
|
||
try {
|
||
fn();
|
||
} catch (e) {
|
||
console.error(TAG, e);
|
||
}
|
||
}, ms);
|
||
DELAYS.push(id);
|
||
return id;
|
||
}
|
||
function onWindow(target, type, fn) {
|
||
target.addEventListener(type, fn);
|
||
WINDOW_LISTENERS.push([target, type, fn]);
|
||
}
|
||
function onElement(el, type, fn) {
|
||
el.addEventListener(type, fn);
|
||
ELEMENT_LISTENERS.push([el, type, fn]);
|
||
}
|
||
function detachListener(x) {
|
||
x[0].removeEventListener(x[1], x[2]);
|
||
}
|
||
function drain(arr, fn) {
|
||
arr.splice(0).forEach(function(x) {
|
||
try {
|
||
fn(x);
|
||
} catch (e) {
|
||
}
|
||
});
|
||
}
|
||
function dispose() {
|
||
drain(TIMERS, clearInterval);
|
||
drain(DELAYS, clearTimeout);
|
||
drain(WINDOW_LISTENERS, detachListener);
|
||
drain(ELEMENT_LISTENERS, detachListener);
|
||
try {
|
||
document.querySelectorAll("video[data-yt-hooked]").forEach(function(v) {
|
||
v.removeAttribute("data-yt-hooked");
|
||
});
|
||
} catch (e) {
|
||
}
|
||
const panel = document.getElementById(PANEL_ID);
|
||
if (panel && panel.parentNode) panel.parentNode.removeChild(panel);
|
||
try {
|
||
delete window.__ytAuto;
|
||
} catch (e) {
|
||
}
|
||
try {
|
||
if (window.__ytDispose === dispose) delete window.__ytDispose;
|
||
} catch (e) {
|
||
}
|
||
console.log(TAG, "旧实例已释放");
|
||
}
|
||
try {
|
||
Object.defineProperty(window, "__ytDispose", {
|
||
value: dispose,
|
||
configurable: true,
|
||
writable: true
|
||
});
|
||
} catch (e) {
|
||
}
|
||
|
||
// src/utils.js
|
||
var norm = (s) => String(s || "").replace(/\s/g, "");
|
||
var truncate = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
|
||
function textOf(el) {
|
||
try {
|
||
return norm(el.innerText || el.textContent || "");
|
||
} catch (e) {
|
||
return "";
|
||
}
|
||
}
|
||
function isVisible(el) {
|
||
try {
|
||
const win = el.ownerDocument && el.ownerDocument.defaultView || window;
|
||
const r = el.getBoundingClientRect();
|
||
if (r.width === 0 || r.height === 0) return false;
|
||
const cs = win.getComputedStyle(el);
|
||
return cs.display !== "none" && cs.visibility !== "hidden" && Number(cs.opacity) > 0;
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
function findVisibleButton(re) {
|
||
const els = document.querySelectorAll('button, [role="button"], a');
|
||
for (const el of els) {
|
||
if (re.test(textOf(el)) && isVisible(el)) return el;
|
||
}
|
||
return null;
|
||
}
|
||
function findVisibleClickable(re) {
|
||
const btn = findVisibleButton(re);
|
||
if (btn && !btn.disabled) return btn;
|
||
const els = document.querySelectorAll("div, span");
|
||
for (const el of els) {
|
||
if (el.children.length > 0) continue;
|
||
if (!isVisible(el) || !re.test(textOf(el))) continue;
|
||
if (el.closest && el.closest("button[disabled]")) continue;
|
||
return el;
|
||
}
|
||
return null;
|
||
}
|
||
function courseIdFromUrl() {
|
||
const m = String(location.hash || "").match(/[?&]kngId=([^&]+)/);
|
||
if (!m) return "";
|
||
try {
|
||
return decodeURIComponent(m[1]);
|
||
} catch (e) {
|
||
return m[1];
|
||
}
|
||
}
|
||
function collectDocuments(win, out) {
|
||
win = win || window;
|
||
out = out || [];
|
||
try {
|
||
if (win.document) out.push(win.document);
|
||
} catch (e) {
|
||
}
|
||
try {
|
||
for (let i = 0; i < win.frames.length; i++) {
|
||
collectDocuments(win.frames[i], out);
|
||
}
|
||
} catch (e) {
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// src/route.js
|
||
function route() {
|
||
const h = location.hash || "";
|
||
if (h.indexOf("#/list") === 0) return "list";
|
||
if (h.indexOf("#/scan") === 0) return "course";
|
||
if (/^#\/[a-z0-9_-]+\/play(?:[?/]|$)/i.test(h)) return "course";
|
||
return "other";
|
||
}
|
||
function mediaPage() {
|
||
return /^#\/(?:video|audio|ppt|document|article)\/play(?:[?/]|$)/i.test(location.hash || "");
|
||
}
|
||
function goToList(force) {
|
||
if (!force && Date.now() - R.lastGoListAt < 5e3) return;
|
||
R.lastGoListAt = Date.now();
|
||
const url = S.listUrl || location.origin + "/kng/#/list";
|
||
if (route() === "list") {
|
||
S.returning = true;
|
||
save();
|
||
return;
|
||
}
|
||
try {
|
||
location.href = url;
|
||
} catch (e) {
|
||
location.hash = "#/list";
|
||
}
|
||
}
|
||
|
||
// src/login.js
|
||
var tick = function() {
|
||
};
|
||
function setTick(fn) {
|
||
tick = fn;
|
||
}
|
||
function clearPending() {
|
||
try {
|
||
localStorage.removeItem(LS_PENDING);
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function isLoginPage() {
|
||
if (/\/login(\.html)?($|[?#])/i.test(location.pathname + location.search)) return true;
|
||
if (/^#\/login/i.test(location.hash)) return true;
|
||
return !!document.querySelector('input[type="password"]') && /登录/.test(document.title);
|
||
}
|
||
function loginFields() {
|
||
const pwd = [...document.querySelectorAll('input[type="password"]')].find(isVisible) || null;
|
||
const all = [...document.querySelectorAll("input")].filter(isVisible);
|
||
let user = all.find(function(el) {
|
||
return /员工邮箱|账号|用户名|邮箱|手机号/.test(el.placeholder || "");
|
||
}) || null;
|
||
if (!user && pwd) {
|
||
const pi = all.indexOf(pwd);
|
||
for (let i = pi - 1; i >= 0; i--) {
|
||
const el = all[i];
|
||
if (el.type === "text" || el.type === "email" || !el.type) {
|
||
user = el;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return { user, pwd };
|
||
}
|
||
function setNativeValue(el, value) {
|
||
if (!el) return false;
|
||
try {
|
||
const proto = el.tagName === "TEXTAREA" ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
|
||
const setter = Object.getOwnPropertyDescriptor(proto, "value").set;
|
||
setter.call(el, value);
|
||
} catch (e) {
|
||
el.value = value;
|
||
}
|
||
el.dispatchEvent(new Event("input", { bubbles: true }));
|
||
el.dispatchEvent(new Event("change", { bubbles: true }));
|
||
el.dispatchEvent(new Event("blur", { bubbles: true }));
|
||
return true;
|
||
}
|
||
function findLoginVm() {
|
||
const btn = findVisibleButton(/^登录$/);
|
||
let vm = btn && btn.__vue__;
|
||
while (vm && vm.$options.name !== "Login") vm = vm.$parent;
|
||
if (vm) return vm;
|
||
for (const el of document.querySelectorAll("#app, #app *")) {
|
||
const v = el.__vue__;
|
||
if (v && v.pwdLoginForm) return v;
|
||
}
|
||
return null;
|
||
}
|
||
function fillLoginForm(user, pass) {
|
||
const f = loginFields();
|
||
if (!f.user || !f.pwd) return false;
|
||
setNativeValue(f.user, user);
|
||
setNativeValue(f.pwd, pass);
|
||
const vm = findLoginVm();
|
||
if (vm && vm.pwdLoginForm) {
|
||
vm.pwdLoginForm.userName = user;
|
||
vm.pwdLoginForm.password = pass;
|
||
}
|
||
return true;
|
||
}
|
||
function checkLoginAgreement() {
|
||
const labels = [...document.querySelectorAll("label")].filter(function(l) {
|
||
return /已阅读并同意|隐私政策/.test(l.innerText || "") && isVisible(l);
|
||
});
|
||
if (!labels.length) return true;
|
||
for (const label of labels) {
|
||
const cb = label.querySelector('input[type="checkbox"]');
|
||
if (!cb) continue;
|
||
if (!cb.checked) {
|
||
try {
|
||
label.click();
|
||
} catch (e) {
|
||
}
|
||
}
|
||
if (!cb.checked) {
|
||
try {
|
||
cb.click();
|
||
} catch (e) {
|
||
}
|
||
}
|
||
if (cb.checked) return true;
|
||
}
|
||
return false;
|
||
}
|
||
function b64enc(s) {
|
||
const bytes = new TextEncoder().encode(String(s));
|
||
let bin = "";
|
||
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
||
return btoa(bin);
|
||
}
|
||
function b64dec(s) {
|
||
const bin = atob(String(s));
|
||
const bytes = new Uint8Array(bin.length);
|
||
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
||
return new TextDecoder().decode(bytes);
|
||
}
|
||
function saveCreds(user, pass, remember, auto) {
|
||
try {
|
||
if (!remember) {
|
||
localStorage.removeItem(LS_LOGIN);
|
||
return;
|
||
}
|
||
localStorage.setItem(LS_LOGIN, b64enc(JSON.stringify({ u: user, p: pass, auto: !!auto })));
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function loadCreds() {
|
||
try {
|
||
const raw = localStorage.getItem(LS_LOGIN);
|
||
if (!raw) return null;
|
||
const obj = JSON.parse(b64dec(raw));
|
||
if (!obj || !obj.u || !obj.p) return null;
|
||
return { u: String(obj.u), p: String(obj.p), auto: !!obj.auto };
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
function clearSavedCreds() {
|
||
try {
|
||
localStorage.removeItem(LS_LOGIN);
|
||
} catch (e) {
|
||
}
|
||
if (loginRememberEl) loginRememberEl.checked = false;
|
||
}
|
||
function startBatch() {
|
||
if (S.runFinished) {
|
||
S.completedThisRun = 0;
|
||
S.consecutiveFailures = 0;
|
||
S.runFinished = false;
|
||
}
|
||
S.running = true;
|
||
if (!S.current) S.phase = "idle";
|
||
save();
|
||
setStatus("自动刷课已开启");
|
||
const r = route();
|
||
if (r === "list" || r === "course" && S.current) {
|
||
tick();
|
||
} else {
|
||
goToList();
|
||
}
|
||
updatePanel();
|
||
}
|
||
function consumePendingStart() {
|
||
let ts = 0;
|
||
try {
|
||
ts = Number(localStorage.getItem(LS_PENDING) || 0);
|
||
} catch (e) {
|
||
}
|
||
if (!ts) return false;
|
||
if (Date.now() - ts > 5 * 60 * 1e3) {
|
||
clearPending();
|
||
return false;
|
||
}
|
||
if (isLoginPage()) return false;
|
||
clearPending();
|
||
startBatch();
|
||
return true;
|
||
}
|
||
function watchLoginResult() {
|
||
if (!isLoginPage()) return;
|
||
const vm = findLoginVm();
|
||
if (vm && vm.captchaVisible) {
|
||
setStatus("需要手动完成验证码/滑块,完成后会自动继续");
|
||
return;
|
||
}
|
||
if (vm && typeof vm.pwd_tips === "string" && vm.pwd_tips.trim()) {
|
||
clearPending();
|
||
R.loginSubmittingAt = 0;
|
||
setStatus("登录失败:" + truncate(vm.pwd_tips.trim(), 50));
|
||
return;
|
||
}
|
||
const txt = document.body.innerText || "";
|
||
const err = txt.match(/(用户名密码错误[^\n]{0,60}|账号或密码错误[^\n]{0,60}|密码错误[^\n]{0,60}|账号不存在[^\n]{0,60}|登录失败[^\n]{0,60})/);
|
||
if (err) {
|
||
clearPending();
|
||
R.loginSubmittingAt = 0;
|
||
setStatus("登录失败:" + truncate(err[0], 50));
|
||
return;
|
||
}
|
||
if (/验证码|安全验证|滑块/.test(txt) && R.loginSubmittingAt) {
|
||
setStatus("需要手动完成验证码/滑块,完成后会自动继续");
|
||
return;
|
||
}
|
||
if (R.loginSubmittingAt && Date.now() - R.loginSubmittingAt > 12e3) {
|
||
R.loginSubmittingAt = 0;
|
||
setStatus("尚未登录成功:请检查账号密码,或手动完成验证");
|
||
}
|
||
}
|
||
function doLoginAndStart() {
|
||
const f = loginFields();
|
||
const user = (loginUserEl ? loginUserEl.value.trim() : "") || (f.user ? String(f.user.value || "").trim() : "");
|
||
const pass = (loginPassEl ? loginPassEl.value : "") || (f.pwd ? String(f.pwd.value || "") : "");
|
||
if (!user || !pass) {
|
||
setStatus("请先填写账号和密码");
|
||
return;
|
||
}
|
||
if (loginUserEl) loginUserEl.value = user;
|
||
if (loginPassEl) loginPassEl.value = pass;
|
||
const remember = !!(loginRememberEl && loginRememberEl.checked);
|
||
const auto = !!(loginAutoEl && loginAutoEl.checked);
|
||
saveCreds(user, pass, remember, auto);
|
||
fillLoginForm(user, pass);
|
||
if (!checkLoginAgreement()) {
|
||
setStatus("请先勾选《隐私政策》");
|
||
return;
|
||
}
|
||
try {
|
||
localStorage.setItem(LS_PENDING, String(Date.now()));
|
||
} catch (e) {
|
||
}
|
||
R.loginSubmittingAt = Date.now();
|
||
R.loginAutoTried = true;
|
||
setStatus("正在登录…");
|
||
const vm = findLoginVm();
|
||
if (vm) {
|
||
try {
|
||
if (vm.pwdLoginForm) {
|
||
vm.pwdLoginForm.userName = user;
|
||
vm.pwdLoginForm.password = pass;
|
||
}
|
||
try {
|
||
vm.agreeSecretProtocol = true;
|
||
} catch (e) {
|
||
}
|
||
const p = vm.handleLogin();
|
||
if (p && typeof p.catch === "function") {
|
||
p.catch(function(e) {
|
||
setStatus("登录异常:" + (e && e.message ? e.message : e));
|
||
});
|
||
}
|
||
} catch (e) {
|
||
setStatus("登录异常:" + (e && e.message ? e.message : e));
|
||
}
|
||
} else {
|
||
const btn = findVisibleButton(/^登录$/);
|
||
if (btn) {
|
||
try {
|
||
btn.click();
|
||
} catch (e) {
|
||
}
|
||
} else {
|
||
setStatus("未找到登录按钮,请手动点击");
|
||
}
|
||
}
|
||
later(watchLoginResult, 2500);
|
||
}
|
||
function loginPageTick() {
|
||
if (!isLoginPage()) return;
|
||
const f = loginFields();
|
||
if (f.user && f.pwd && (R.loginBoundUser !== f.user || R.loginBoundPwd !== f.pwd)) {
|
||
R.loginBoundUser = f.user;
|
||
R.loginBoundPwd = f.pwd;
|
||
const sync = function() {
|
||
if (loginUserEl) loginUserEl.value = f.user.value;
|
||
if (loginPassEl) loginPassEl.value = f.pwd.value;
|
||
};
|
||
onElement(f.user, "input", sync);
|
||
onElement(f.pwd, "input", sync);
|
||
}
|
||
if (!R.loginFilled) {
|
||
if (f.user && f.pwd) {
|
||
R.loginFilled = true;
|
||
const creds = loadCreds();
|
||
if (creds) {
|
||
fillLoginForm(creds.u, creds.p);
|
||
if (loginUserEl) loginUserEl.value = creds.u;
|
||
if (loginPassEl) loginPassEl.value = creds.p;
|
||
if (loginRememberEl) loginRememberEl.checked = true;
|
||
if (loginAutoEl) loginAutoEl.checked = !!creds.auto;
|
||
setStatus("已填充保存的账号,可点「一键登录并刷课」");
|
||
if (creds.auto && !R.loginAutoTried) {
|
||
R.loginAutoTried = true;
|
||
later(function() {
|
||
doLoginAndStart();
|
||
}, 800);
|
||
}
|
||
} else {
|
||
setStatus("填写账号密码后,点「一键登录并刷课」");
|
||
}
|
||
}
|
||
}
|
||
if (R.loginSubmittingAt) watchLoginResult();
|
||
}
|
||
|
||
// src/player.js
|
||
function applyMute(v) {
|
||
if (!CFG.mute) return;
|
||
try {
|
||
if (!v.muted) v.muted = true;
|
||
v.defaultMuted = true;
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function playVideo(v) {
|
||
if (!v) return;
|
||
if (state.enabled || S.running) {
|
||
try {
|
||
const p = v.play();
|
||
if (p && typeof p.catch === "function") p.catch(() => {
|
||
});
|
||
} catch (e) {
|
||
}
|
||
}
|
||
}
|
||
function hookVideo(v) {
|
||
if (!v) return;
|
||
if (v.getAttribute && v.getAttribute("data-yt-hooked") === "1") return;
|
||
if (R.hooked.has(v)) return;
|
||
R.hooked.add(v);
|
||
try {
|
||
v.setAttribute("data-yt-hooked", "1");
|
||
} catch (e) {
|
||
}
|
||
applyMute(v);
|
||
onElement(v, "play", function() {
|
||
applyMute(v);
|
||
R.lastProgressAt = Date.now();
|
||
});
|
||
onElement(v, "ended", function() {
|
||
R.lastProgressAt = Date.now();
|
||
});
|
||
onElement(v, "timeupdate", function() {
|
||
R.lastProgressAt = Date.now();
|
||
});
|
||
}
|
||
function hookAllVideos() {
|
||
for (const doc of collectDocuments()) {
|
||
let vids;
|
||
try {
|
||
vids = doc.querySelectorAll("video, audio");
|
||
} catch (e) {
|
||
continue;
|
||
}
|
||
for (const v of vids) hookVideo(v);
|
||
}
|
||
}
|
||
function findPlayableVideo() {
|
||
const vids = [...document.querySelectorAll("video, audio")].filter(function(v) {
|
||
return v.duration > 0 || v.currentSrc;
|
||
});
|
||
return vids.find(function(v) {
|
||
return !v.paused;
|
||
}) || vids.find(function(v) {
|
||
return isVisible(v) && v.duration > 0;
|
||
}) || vids.find(function(v) {
|
||
return v.duration > 0;
|
||
}) || vids[0] || null;
|
||
}
|
||
function ensurePlaying(v) {
|
||
if (!v) return;
|
||
applyMute(v);
|
||
if (S.current) {
|
||
try {
|
||
if (Math.abs(v.playbackRate - 1) > 0.01) v.playbackRate = 1;
|
||
} catch (e) {
|
||
}
|
||
}
|
||
if (!v.paused) return;
|
||
if (v.ended) return;
|
||
playVideo(v);
|
||
}
|
||
function readSummary() {
|
||
const el = document.querySelector('[class*="course-summary"]');
|
||
if (!el) return null;
|
||
const t = textOf(el);
|
||
const m = t.match(/已完成(\d+)\/(\d+)/);
|
||
if (m) {
|
||
const x = Number(m[1]);
|
||
const y = Number(m[2]);
|
||
return {
|
||
x,
|
||
y,
|
||
percent: y > 0 ? Math.round(x / y * 100) : 0,
|
||
complete: y > 0 && x >= y,
|
||
kind: "ratio",
|
||
text: t
|
||
};
|
||
}
|
||
const pct = t.match(/已完成(\d+)%/);
|
||
if (pct) {
|
||
const x = Number(pct[1]);
|
||
return { x, y: 100, percent: x, complete: x >= 100, kind: "percent", text: t };
|
||
}
|
||
if (/已完成学习|学习完成|已完成本课程|已学完/.test(t)) {
|
||
return { x: 100, y: 100, percent: 100, complete: true, kind: "done", text: t };
|
||
}
|
||
const bar = el.querySelector('[role="progressbar"]');
|
||
if (bar) {
|
||
const now = Number(bar.getAttribute("aria-valuenow"));
|
||
const max = Number(bar.getAttribute("aria-valuemax")) || 100;
|
||
if (Number.isFinite(now)) {
|
||
return {
|
||
x: now,
|
||
y: max,
|
||
percent: Math.round(now / max * 100),
|
||
complete: now >= max,
|
||
kind: "percent",
|
||
text: t
|
||
};
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
function readCountdown() {
|
||
const el = document.querySelector(".yxtulcdsdk-course-player__countdown");
|
||
if (!el) return null;
|
||
const t = textOf(el);
|
||
const m = t.match(/还需(?:(\d+)小时)?(?:(\d+)分钟)?(?:(\d+)秒)?/);
|
||
if (m && (m[1] || m[2] || m[3])) {
|
||
return Number(m[1] || 0) * 3600 + Number(m[2] || 0) * 60 + Number(m[3] || 0);
|
||
}
|
||
if (/已完成|学习完成|已学完/.test(t)) return 0;
|
||
return null;
|
||
}
|
||
function readActiveLessonKey() {
|
||
const active = document.querySelector(".yxtulcdsdk-catalog .liactive");
|
||
if (!active) return "";
|
||
const li = active.closest("li");
|
||
return norm(li ? li.innerText : active.innerText);
|
||
}
|
||
function lessonItems() {
|
||
return [...document.querySelectorAll(".yxtulcdsdk-catalog ul > li")].filter(function(li) {
|
||
return !li.querySelector("ul");
|
||
});
|
||
}
|
||
function findNextLessonElement() {
|
||
const activeLi = lessonActiveLi();
|
||
if (!activeLi) return null;
|
||
const lis = lessonItems();
|
||
const idx = lis.indexOf(activeLi);
|
||
if (idx < 0 || idx + 1 >= lis.length) return null;
|
||
return lis[idx + 1];
|
||
}
|
||
function isLessonCompleted(li) {
|
||
try {
|
||
const text = textOf(li);
|
||
if (/已学完|已完成/.test(text)) return true;
|
||
const hint = [
|
||
li.getAttribute("aria-label") || "",
|
||
li.getAttribute("title") || "",
|
||
String(li.className || "")
|
||
].join(" ");
|
||
if (/已学完|已完成|finished|completed|is-done/i.test(hint)) return true;
|
||
const ds = [...li.querySelectorAll("svg path")].map(function(p) {
|
||
return String(p.getAttribute("d") || "");
|
||
});
|
||
return ds.some(function(d) {
|
||
return d.replace(/,/g, " ").indexOf("M10 2.75") >= 0;
|
||
});
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
function findFirstIncompleteLessonElement() {
|
||
for (const li of lessonItems()) {
|
||
if (!isLessonCompleted(li)) return li;
|
||
}
|
||
return null;
|
||
}
|
||
function pauseAllMedia() {
|
||
try {
|
||
document.querySelectorAll("video, audio").forEach(function(x) {
|
||
try {
|
||
x.pause();
|
||
} catch (e) {
|
||
}
|
||
});
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function lessonActiveLi() {
|
||
const el = document.querySelector(".yxtulcdsdk-catalog .liactive");
|
||
return el ? el.closest("li") : null;
|
||
}
|
||
function layoutAvailable() {
|
||
try {
|
||
const r = document.body.getBoundingClientRect();
|
||
return r.width > 0 && r.height > 0;
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
function dumpCatalog() {
|
||
return lessonItems().map(function(li, i) {
|
||
return {
|
||
i: i + 1,
|
||
text: truncate(textOf(li), 36),
|
||
active: li.classList.contains("liactive") || !!li.querySelector(".liactive"),
|
||
done: isLessonCompleted(li),
|
||
cls: String(li.className || "").slice(0, 48)
|
||
};
|
||
});
|
||
}
|
||
function fireMouse(el, type) {
|
||
try {
|
||
el.dispatchEvent(new MouseEvent(type, {
|
||
bubbles: true,
|
||
cancelable: true,
|
||
view: window,
|
||
button: 0
|
||
}));
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function firePointer(el, type) {
|
||
try {
|
||
el.dispatchEvent(new PointerEvent(type, {
|
||
bubbles: true,
|
||
cancelable: true,
|
||
pointerId: 1,
|
||
pointerType: "mouse",
|
||
isPrimary: true
|
||
}));
|
||
} catch (e) {
|
||
fireMouse(el, type.replace("pointer", "mouse"));
|
||
}
|
||
}
|
||
function clickNode(el) {
|
||
if (!el) return false;
|
||
try {
|
||
if (typeof el.click === "function") {
|
||
el.click();
|
||
return true;
|
||
}
|
||
} catch (e) {
|
||
}
|
||
fireMouse(el, "click");
|
||
return true;
|
||
}
|
||
function fireClickSequence(el) {
|
||
if (!el) return;
|
||
firePointer(el, "pointerdown");
|
||
fireMouse(el, "mousedown");
|
||
firePointer(el, "pointerup");
|
||
fireMouse(el, "mouseup");
|
||
clickNode(el);
|
||
}
|
||
function lessonClickNodes(li) {
|
||
const title = li.querySelector(".item") || li.querySelector(".yxtf-tooltip") || li.querySelector("span");
|
||
const row = li.querySelector('[class*="flex-space-between"]') || li.querySelector("div");
|
||
const svg = li.querySelector("svg");
|
||
return [title, row, li, svg].filter(Boolean);
|
||
}
|
||
function clickLesson(li, attempt) {
|
||
if (!li) return;
|
||
const nodes = lessonClickNodes(li);
|
||
if (!nodes.length) return;
|
||
const title = nodes[0];
|
||
const row = nodes[1] && nodes[1] !== li ? nodes[1] : null;
|
||
const svg = li.querySelector("svg");
|
||
if (attempt <= 2) {
|
||
clickNode(title);
|
||
if (attempt === 2 && row) clickNode(row);
|
||
return;
|
||
}
|
||
if (attempt >= 4) {
|
||
const startBtn = [...li.querySelectorAll('button, [role="button"], span, div')].find(function(el) {
|
||
return el.children.length <= 1 && /^(开始学习|继续学习)$/.test(textOf(el));
|
||
});
|
||
if (startBtn) clickNode(startBtn);
|
||
}
|
||
fireClickSequence(title);
|
||
const targets = [title, row, li, svg].filter(Boolean);
|
||
const target = targets[(attempt - 3) % targets.length];
|
||
if (target && target !== title) fireClickSequence(target);
|
||
}
|
||
function confirmLessonDialog() {
|
||
const pat = /^(确定|确认|是|切换|确定切换|继续切换|继续学习|继续观看|知道了)$/;
|
||
const boxes = [...document.querySelectorAll(
|
||
'.yxt-dialog__wrapper, [class*="dialog"], [class*="modal"], [role="dialog"]'
|
||
)].filter(isVisible);
|
||
let buttons = [];
|
||
for (const box of boxes) {
|
||
buttons = buttons.concat([...box.querySelectorAll('button, [role="button"]')]);
|
||
}
|
||
if (!buttons.length) {
|
||
buttons = [...document.querySelectorAll('button, [role="button"]')];
|
||
}
|
||
for (const el of buttons) {
|
||
if (!isVisible(el)) continue;
|
||
if (pat.test(textOf(el))) {
|
||
clickNode(el);
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
function findNextButton() {
|
||
const els = [...document.querySelectorAll(
|
||
'[class*="kng-operate"] div, [class*="kng-operate"] span, [class*="kng-operate"] button'
|
||
)];
|
||
for (const el of els) {
|
||
if (textOf(el) === "下一个" && isVisible(el)) return el;
|
||
}
|
||
const generic = [...document.querySelectorAll("div, span, button, a")];
|
||
for (const el of generic) {
|
||
if (el.children.length <= 1 && textOf(el) === "下一个" && isVisible(el)) return el;
|
||
}
|
||
return null;
|
||
}
|
||
function courseHasQuizLesson() {
|
||
return [...document.querySelectorAll(".yxtulcdsdk-catalog ul > li .tag-type")].some(function(t) {
|
||
return /考试|测验|问卷|答题/.test(textOf(t));
|
||
});
|
||
}
|
||
function detectQuiz() {
|
||
const pat = /^(开始答题|继续答题|提交答案|提交试卷|开始考试)$/;
|
||
for (const doc of collectDocuments()) {
|
||
let els;
|
||
try {
|
||
els = doc.querySelectorAll('button, [role="button"], a');
|
||
} catch (e) {
|
||
continue;
|
||
}
|
||
for (const el of els) {
|
||
if (pat.test(textOf(el)) && isVisible(el)) return true;
|
||
}
|
||
}
|
||
const dialogs = [...document.querySelectorAll('.yxt-dialog__wrapper, [class*="dialog"]')];
|
||
for (const d of dialogs) {
|
||
if (!isVisible(d)) continue;
|
||
const t = textOf(d);
|
||
if (/开始考试|提交试卷|开始答题/.test(t)) return true;
|
||
}
|
||
return false;
|
||
}
|
||
function findAntiIdleDialog() {
|
||
const pat = /防挂机|不要走开|学习将被停止|请点击.?继续学习/;
|
||
let best = null;
|
||
let bestLen = Infinity;
|
||
for (const doc of collectDocuments()) {
|
||
let cands;
|
||
try {
|
||
cands = doc.querySelectorAll('[class*="dialog"], [class*="modal"], [role="dialog"]');
|
||
} catch (e) {
|
||
continue;
|
||
}
|
||
for (const el of cands) {
|
||
if (!isVisible(el)) continue;
|
||
const t = textOf(el);
|
||
if (t.length > 400) continue;
|
||
if (!pat.test(t)) continue;
|
||
if (t.length < bestLen) {
|
||
best = el;
|
||
bestLen = t.length;
|
||
}
|
||
}
|
||
}
|
||
return best;
|
||
}
|
||
function findAntiIdleButton(dialog) {
|
||
if (!dialog) return null;
|
||
const want = /^(继续学习|我知道了|继续观看)$/;
|
||
const pick = function(sel) {
|
||
return [...dialog.querySelectorAll(sel)].find(function(el2) {
|
||
return want.test(textOf(el2)) && isVisible(el2);
|
||
}) || null;
|
||
};
|
||
const el = pick("button") || pick('[role="button"]') || pick("a") || pick("div") || pick("span");
|
||
if (!el) return findVisibleButton(/^(继续学习)$/);
|
||
if (el.tagName !== "BUTTON") {
|
||
const inner = el.querySelector("button");
|
||
if (inner) return inner;
|
||
}
|
||
return el;
|
||
}
|
||
function antiIdleNeedsHuman(dialog) {
|
||
if (!dialog) return false;
|
||
try {
|
||
const cands = dialog.querySelectorAll(
|
||
'input[type="range"], [class*="slider"], [class*="captcha"], [class*="drag-verify"], [class*="verify-slider"]'
|
||
);
|
||
for (const el of cands) if (isVisible(el)) return true;
|
||
return false;
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
function handleAntiIdle() {
|
||
const dialog = findAntiIdleDialog();
|
||
if (!dialog) return false;
|
||
const btn = findAntiIdleButton(dialog);
|
||
if (!btn) {
|
||
if (antiIdleNeedsHuman(dialog)) {
|
||
setStatus("防挂机验证需要人工完成(滑块/拼图)");
|
||
failCurrent("防挂机验证需要人工完成");
|
||
} else {
|
||
setStatus("检测到防挂机验证,未找到「继续学习」按钮");
|
||
}
|
||
return true;
|
||
}
|
||
if (Date.now() - R.lastIdleClickAt < 2500) return true;
|
||
const dialogText = truncate(textOf(dialog), 40);
|
||
const sameDialog = R.idleDialogText === dialogText;
|
||
if (sameDialog && R.idleDialogClicks >= 10) {
|
||
if (!R.idleGaveUp) {
|
||
R.idleGaveUp = true;
|
||
console.warn(
|
||
TAG,
|
||
"防挂机验证弹层点了 10 次仍未关闭,停止点击,等待人工处理",
|
||
String(dialog.outerHTML || "").slice(0, 800)
|
||
);
|
||
setStatus("防挂机验证未能通过,等待人工处理");
|
||
}
|
||
return true;
|
||
}
|
||
R.idleDialogText = dialogText;
|
||
R.idleDialogClicks = sameDialog ? R.idleDialogClicks + 1 : 1;
|
||
R.idleGaveUp = false;
|
||
R.lastIdleClickAt = Date.now();
|
||
R.idlePassed++;
|
||
try {
|
||
btn.click();
|
||
} catch (e) {
|
||
}
|
||
console.log(TAG, "防挂机验证:已点击「继续学习」(第", R.idlePassed, "次)");
|
||
setStatus("已通过防挂机验证(第 " + R.idlePassed + " 次)");
|
||
updatePanel();
|
||
return true;
|
||
}
|
||
function courseTitle() {
|
||
return String(document.title || "").replace(/\s*-\s*[^-]*(云课堂|云学堂)\s*$/, "").trim();
|
||
}
|
||
function alignToFirstIncomplete() {
|
||
if (!CFG.alignToFirstIncomplete || R.initialLessonChecked) return false;
|
||
const lis = lessonItems();
|
||
if (!lis.length) return false;
|
||
if (!R.phaseSince) R.phaseSince = Date.now();
|
||
if (Date.now() - R.phaseSince < 1500) return true;
|
||
const activeLi = lessonActiveLi();
|
||
const activeIdx = activeLi ? lis.indexOf(activeLi) : -1;
|
||
const targetLi = findFirstIncompleteLessonElement();
|
||
const targetIdx = targetLi ? lis.indexOf(targetLi) : -1;
|
||
if (targetIdx < 0) {
|
||
R.initialLessonChecked = true;
|
||
return false;
|
||
}
|
||
if (targetIdx === activeIdx) {
|
||
R.alignStable++;
|
||
if (R.alignStable < 2) return true;
|
||
R.initialLessonChecked = true;
|
||
R.lastLessonKey = readActiveLessonKey();
|
||
console.log(TAG, "起点已对齐:第", targetIdx + 1, "节", truncate(textOf(targetLi), 24));
|
||
setStatus("从第 " + (targetIdx + 1) + " 节开始:" + truncate(textOf(targetLi), 18));
|
||
return false;
|
||
}
|
||
R.alignStable = 0;
|
||
if (R.alignTries >= 8) {
|
||
R.initialLessonChecked = true;
|
||
R.alignFailed = true;
|
||
setStatus("未能切换到第 " + (targetIdx + 1) + " 节,按当前位置继续");
|
||
console.warn(TAG, "起点对齐失败:点击课时后平台未切换,目录快照:", dumpCatalog());
|
||
return false;
|
||
}
|
||
R.alignTries++;
|
||
if (layoutAvailable() && !isVisible(targetLi)) {
|
||
let ul = targetLi.parentElement;
|
||
while (ul && ul.tagName !== "UL") ul = ul.parentElement;
|
||
const collapsed = ul && getComputedStyle(ul).display === "none";
|
||
const chapterLi = ul && ul.parentElement ? ul.parentElement.closest("li") : null;
|
||
if (collapsed && chapterLi && R.alignTries <= 3) {
|
||
setStatus("展开章节:" + truncate(textOf(chapterLi), 20));
|
||
clickNode(chapterLi.querySelector(".hand") || chapterLi);
|
||
return true;
|
||
}
|
||
}
|
||
if (R.alignTries > 1 && confirmLessonDialog()) {
|
||
setStatus("已确认切换提示,继续…");
|
||
return true;
|
||
}
|
||
console.log(
|
||
TAG,
|
||
"对齐起点:第",
|
||
activeIdx >= 0 ? activeIdx + 1 : "?",
|
||
"节 → 第",
|
||
targetIdx + 1,
|
||
"节",
|
||
"(第 " + R.alignTries + " 次)"
|
||
);
|
||
setStatus("正在切换到第 " + (targetIdx + 1) + " 节…" + (R.alignTries > 1 ? "(第 " + R.alignTries + " 次)" : ""));
|
||
clickLesson(targetLi, R.alignTries);
|
||
return true;
|
||
}
|
||
function syncManualCourse() {
|
||
const key = courseTitle() || location.href;
|
||
if (R.manualCourseKey === key) return true;
|
||
R.manualCourseKey = key;
|
||
R.initialLessonChecked = false;
|
||
R.phaseSince = 0;
|
||
R.alignTries = 0;
|
||
R.alignStable = 0;
|
||
R.alignFailed = false;
|
||
R.lastLessonKey = "";
|
||
R.doneLatchKey = "";
|
||
R.waitStart = 0;
|
||
return true;
|
||
}
|
||
|
||
// src/list.js
|
||
function scanCards() {
|
||
return [...document.querySelectorAll("li.kng-list-new__item")].map((li, i) => {
|
||
const titleEl = li.querySelector(".yxtf-tooltip");
|
||
const tags = [...li.querySelectorAll(".kng-list-new__tag")].map(textOf);
|
||
const status = tags.find((t) => /已学完|学习中|未开始|未学习|已完成/.test(t)) || tags.find((t) => t !== "课程") || "";
|
||
return {
|
||
li,
|
||
i,
|
||
title: norm(titleEl ? titleEl.innerText : ""),
|
||
status,
|
||
tags
|
||
};
|
||
});
|
||
}
|
||
function currentPage() {
|
||
const el = document.querySelector(".yxtf-pager .number.active");
|
||
if (!el) return null;
|
||
const n = Number(textOf(el));
|
||
return Number.isFinite(n) ? n : null;
|
||
}
|
||
function maxPageNumber() {
|
||
const nums = [...document.querySelectorAll(".yxtf-pager .number")].map((e) => Number(textOf(e))).filter((n) => Number.isFinite(n) && n > 0);
|
||
return nums.length ? Math.max.apply(null, nums) : null;
|
||
}
|
||
function cardsSignature(cards) {
|
||
cards = cards || scanCards();
|
||
if (!cards.length) return "";
|
||
return cards.length + "|" + cards[0].title + "|" + cards[cards.length - 1].title;
|
||
}
|
||
function jumpToPage(target) {
|
||
if (!target) return false;
|
||
if (Date.now() - R.lastPageJumpAt < 3e3) return false;
|
||
R.lastPageJumpAt = Date.now();
|
||
R.pageChangeAt = Date.now();
|
||
R.pageChangeSig = cardsSignature();
|
||
const input = document.querySelector(
|
||
'.yxtf-pagination__editor input, .yxtf-pagination input[type="number"]'
|
||
);
|
||
if (input) {
|
||
try {
|
||
const setter = Object.getOwnPropertyDescriptor(
|
||
window.HTMLInputElement.prototype,
|
||
"value"
|
||
).set;
|
||
setter.call(input, String(target));
|
||
} catch (e) {
|
||
input.value = String(target);
|
||
}
|
||
input.dispatchEvent(new Event("input", { bubbles: true }));
|
||
input.dispatchEvent(new Event("change", { bubbles: true }));
|
||
input.dispatchEvent(new KeyboardEvent("keydown", {
|
||
key: "Enter",
|
||
code: "Enter",
|
||
keyCode: 13,
|
||
which: 13,
|
||
bubbles: true
|
||
}));
|
||
console.log(TAG, "跳转到第", target, "页");
|
||
return true;
|
||
}
|
||
const btn = [...document.querySelectorAll(".yxtf-pager .number")].find((e) => Number(textOf(e)) === target);
|
||
if (btn) {
|
||
btn.click();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
function nextPage() {
|
||
if (Date.now() - R.lastPageJumpAt < 3e3) return;
|
||
R.lastPageJumpAt = Date.now();
|
||
R.pageChangeAt = Date.now();
|
||
R.pageChangeSig = cardsSignature();
|
||
const btn = document.querySelector(".yxtf-pagination .btn-next");
|
||
if (btn && !btn.disabled) {
|
||
btn.click();
|
||
console.log(TAG, "翻到下一页");
|
||
}
|
||
}
|
||
function formatDuration(seconds) {
|
||
const n = Number(seconds);
|
||
if (!Number.isFinite(n) || n <= 0) return "时长未知";
|
||
const minutes = Math.max(1, Math.round(n / 60));
|
||
if (minutes < 60) return minutes + " 分钟";
|
||
const h = Math.floor(minutes / 60);
|
||
const m = minutes % 60;
|
||
return h + " 小时" + (m ? " " + m + " 分钟" : "");
|
||
}
|
||
function listData(vm) {
|
||
return vm && vm.kngListData || [];
|
||
}
|
||
function listVm() {
|
||
const ul = document.querySelector("ul.kng-list-new");
|
||
let vm = ul && ul.__vue__;
|
||
while (vm && !Array.isArray(vm.kngListData)) vm = vm.$parent;
|
||
return vm && Array.isArray(vm.kngListData) ? vm : null;
|
||
}
|
||
function shortQuery(vm) {
|
||
return {
|
||
pageSize: Number(vm && vm.pageSize) || 16,
|
||
orderBy: vm && vm.sortColumn || "createTime",
|
||
orderType: vm && vm.sortType || "desc"
|
||
};
|
||
}
|
||
function shortCatalogKey(vm) {
|
||
const q = shortQuery(vm);
|
||
const key = {
|
||
collegeId: vm && vm.collegeId || "",
|
||
catalogId: vm && vm.currentCatalogId || "",
|
||
title: vm && vm.searchKey || "",
|
||
type: vm && vm.kngType != null ? vm.kngType : "",
|
||
allTag: vm && vm.tagIds && vm.tagIds.length ? 0 : 1,
|
||
tagIds: vm && vm.tagIds || [],
|
||
orderBy: q.orderBy,
|
||
orderType: q.orderType,
|
||
pageSize: q.pageSize
|
||
};
|
||
return JSON.stringify(key);
|
||
}
|
||
function readShortCatalog(key) {
|
||
try {
|
||
const raw = sessionStorage.getItem(SS_SHORT_CATALOG);
|
||
if (!raw) return null;
|
||
const obj = JSON.parse(raw);
|
||
if (!obj || obj.key !== key || !Array.isArray(obj.items)) return null;
|
||
if (Date.now() - (obj.at || 0) > SHORT_CATALOG_TTL) return null;
|
||
return obj;
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
function writeShortCatalog(cat) {
|
||
if (!cat) return;
|
||
try {
|
||
sessionStorage.setItem(SS_SHORT_CATALOG, JSON.stringify(cat));
|
||
} catch (e) {
|
||
}
|
||
}
|
||
function shortApiBase() {
|
||
try {
|
||
const hit = performance.getEntriesByType("resource").map(function(e) {
|
||
return e.name;
|
||
}).find(function(u) {
|
||
return /\/kng\/knowledge\/pagelist/.test(u);
|
||
});
|
||
if (hit) return new URL(hit).origin + "/kng/knowledge/pagelist";
|
||
} catch (e) {
|
||
}
|
||
return "https://api-phx-tc.yunxuetang.cn/kng/knowledge/pagelist";
|
||
}
|
||
function shortCatalogRequest(vm) {
|
||
const orgId = vm && vm.collegeId || localStorage.getItem("orgId") || "";
|
||
const token = localStorage.getItem("token") || "";
|
||
if (!token) throw new Error("未找到登录 token,可能尚未登录或登录已过期");
|
||
const body = JSON.stringify({
|
||
collegeId: orgId,
|
||
catalogId: vm && vm.currentCatalogId || "",
|
||
title: vm && vm.searchKey || "",
|
||
type: vm && vm.kngType != null ? vm.kngType : "",
|
||
allTag: vm && vm.tagIds && vm.tagIds.length ? 0 : 1,
|
||
tagIds: vm && vm.tagIds || []
|
||
});
|
||
const headers = {
|
||
"content-type": "application/json;charset=UTF-8",
|
||
accept: "application/json, text/plain, */*",
|
||
token,
|
||
"yxt-orgdomain": location.hostname,
|
||
source: "501"
|
||
};
|
||
return { body, headers };
|
||
}
|
||
async function fetchShortCatalog(vm) {
|
||
const key = shortCatalogKey(vm);
|
||
const q = shortQuery(vm);
|
||
const req = shortCatalogRequest(vm);
|
||
const api = shortApiBase();
|
||
const datas = [];
|
||
let offset = 0;
|
||
let total = Infinity;
|
||
let requests = 0;
|
||
while (offset < total && requests < 20) {
|
||
const limit = Number.isFinite(total) ? Math.min(6e3, total - offset) : 6e3;
|
||
if (limit <= 0) break;
|
||
const url = api + "?limit=" + limit + "&offset=" + offset + "&orderType=" + encodeURIComponent(q.orderType) + "&orderBy=" + encodeURIComponent(q.orderBy);
|
||
const res = await fetch(url, { method: "POST", headers: req.headers, body: req.body });
|
||
if (!res.ok) throw new Error("列表接口 HTTP " + res.status);
|
||
const json = await res.json();
|
||
const list = json && json.datas;
|
||
if (!Array.isArray(list)) throw new Error("列表接口返回格式异常");
|
||
if (!list.length) break;
|
||
datas.push.apply(datas, list);
|
||
total = Number(json.paging && json.paging.count);
|
||
if (!Number.isFinite(total)) total = datas.length;
|
||
offset += list.length;
|
||
requests++;
|
||
}
|
||
const items = [];
|
||
datas.forEach(function(item, index) {
|
||
if (!item || !item.title) return;
|
||
if (Number(item.status) === 2 || item.studyComplete === 1 || item.studyComplete === true) return;
|
||
const seconds = Number(item.studyHours);
|
||
items.push({
|
||
id: item.id ? String(item.id) : "",
|
||
title: norm(item.title),
|
||
duration: Number.isFinite(seconds) && seconds > 0 ? seconds : 0,
|
||
status: item.status,
|
||
page: Math.floor(index / q.pageSize) + 1
|
||
});
|
||
});
|
||
items.sort(function(a, b) {
|
||
const da = a.duration > 0 ? a.duration : Number.MAX_SAFE_INTEGER;
|
||
const db = b.duration > 0 ? b.duration : Number.MAX_SAFE_INTEGER;
|
||
return da === db ? a.page - b.page : da - db;
|
||
});
|
||
return { key, at: Date.now(), pageSize: q.pageSize, total: datas.length, items };
|
||
}
|
||
function ensureShortCatalog() {
|
||
if (!state.preferShort) return "off";
|
||
const vm = listVm();
|
||
if (!vm) {
|
||
if (!R.shortNoVmSince) R.shortNoVmSince = Date.now();
|
||
if (Date.now() - R.shortNoVmSince > 5e3) {
|
||
R.shortCatalogFailedAt = Date.now();
|
||
if (Date.now() - (R.shortWarnedAt || 0) > 1e4) {
|
||
R.shortWarnedAt = Date.now();
|
||
console.warn(TAG, "未找到课程列表组件,退回当前页时长排序");
|
||
setStatus("未读取到课程列表数据,先按当前页时长排序");
|
||
}
|
||
return "failed";
|
||
}
|
||
return "loading";
|
||
}
|
||
R.shortNoVmSince = 0;
|
||
const key = shortCatalogKey(vm);
|
||
if (R.shortCatalog && R.shortCatalog.key === key && Date.now() - (R.shortCatalog.at || 0) < SHORT_CATALOG_TTL) {
|
||
return "ready";
|
||
}
|
||
if (R.shortCatalogLoading) {
|
||
if (Date.now() - R.shortFetchStartedAt > 2e4) {
|
||
R.shortCatalogLoading = false;
|
||
R.shortCatalogFailedAt = Date.now();
|
||
return "failed";
|
||
}
|
||
return "loading";
|
||
}
|
||
if (R.shortCatalogFailedAt && Date.now() - R.shortCatalogFailedAt < 3e4) return "failed";
|
||
const cached = readShortCatalog(key);
|
||
if (cached) {
|
||
R.shortCatalog = cached;
|
||
return "ready";
|
||
}
|
||
R.shortCatalogLoading = true;
|
||
R.shortFetchStartedAt = Date.now();
|
||
setStatus("正在读取课程时长,优先安排短课程…");
|
||
fetchShortCatalog(vm).then(function(cat) {
|
||
R.shortCatalogLoading = false;
|
||
if (!state.preferShort) return;
|
||
R.shortCatalogFailedAt = 0;
|
||
R.shortCatalog = cat;
|
||
writeShortCatalog(cat);
|
||
const shortest = cat.items[0];
|
||
console.log(
|
||
TAG,
|
||
"短课程队列已就绪:待学",
|
||
cat.items.length,
|
||
"门;最短",
|
||
shortest ? formatDuration(shortest.duration) : "无"
|
||
);
|
||
setStatus(shortest ? "优先短课程:共 " + cat.items.length + " 门待学,最短 " + formatDuration(shortest.duration) : "时长队列为空,按当前页继续");
|
||
updatePanel();
|
||
}).catch(function(e) {
|
||
R.shortCatalogLoading = false;
|
||
R.shortCatalogFailedAt = Date.now();
|
||
if (!state.preferShort) return;
|
||
console.warn(
|
||
TAG,
|
||
"读取课程时长失败,退回当前页时长排序:",
|
||
e && e.message ? e.message : e
|
||
);
|
||
setStatus("未能读取全部课程时长,先按当前页时长排序");
|
||
updatePanel();
|
||
});
|
||
return "loading";
|
||
}
|
||
function shortFallback(reason) {
|
||
R.shortCatalog = null;
|
||
R.shortCatalogFailedAt = Date.now();
|
||
try {
|
||
sessionStorage.removeItem(SS_SHORT_CATALOG);
|
||
} catch (e) {
|
||
}
|
||
if (Date.now() - (R.shortWarnedAt || 0) > 1e4) {
|
||
R.shortWarnedAt = Date.now();
|
||
console.warn(TAG, "短课程优先暂不可用,退回当前页时长排序:", reason);
|
||
setStatus("短课程优先暂不可用,先按当前页时长排序");
|
||
}
|
||
}
|
||
function pendingShortItems() {
|
||
const cat = R.shortCatalog;
|
||
if (!cat) return [];
|
||
return cat.items.filter(function(item) {
|
||
if (!item || !item.title) return false;
|
||
if (S.done[item.title] || S.failed[item.title]) return false;
|
||
return true;
|
||
});
|
||
}
|
||
function courseIdOfCard(card) {
|
||
if (!card) return "";
|
||
const vm = listVm();
|
||
const data = listData(vm);
|
||
const byIndex = data[card.i];
|
||
const t = norm(card.title);
|
||
if (byIndex && String(byIndex.id || "") && norm(byIndex.title) === t) {
|
||
return String(byIndex.id);
|
||
}
|
||
const hit = data.find(function(x) {
|
||
return x && norm(x.title) === t;
|
||
});
|
||
return hit && hit.id ? String(hit.id) : "";
|
||
}
|
||
function durationOfCard(card, vm) {
|
||
if (!card) return 0;
|
||
const data = listData(vm);
|
||
const hit = data.find(function(item) {
|
||
return norm(item.title) === card.title;
|
||
});
|
||
const n = hit ? Number(hit.studyHours) : 0;
|
||
return Number.isFinite(n) && n > 0 ? n : 0;
|
||
}
|
||
function cardForShortItem(cards, item) {
|
||
if (!item) return null;
|
||
if (item.id) {
|
||
const vm = listVm();
|
||
const data = listData(vm);
|
||
const index = data.findIndex(function(x) {
|
||
return x && String(x.id) === item.id;
|
||
});
|
||
if (index >= 0 && cards[index] && cards[index].title === item.title) return cards[index];
|
||
}
|
||
return cards.find(function(c) {
|
||
return c.title === item.title;
|
||
}) || null;
|
||
}
|
||
function sortCardsByDuration(cards, vm) {
|
||
vm = vm || listVm();
|
||
return cards.slice().sort(function(a, b) {
|
||
const da = durationOfCard(a, vm) || Number.MAX_SAFE_INTEGER;
|
||
const db = durationOfCard(b, vm) || Number.MAX_SAFE_INTEGER;
|
||
return da - db;
|
||
});
|
||
}
|
||
function forgetShortCourse(title) {
|
||
const t = norm(title);
|
||
if (!t || !R.shortCatalog || !Array.isArray(R.shortCatalog.items)) return;
|
||
const before = R.shortCatalog.items.length;
|
||
R.shortCatalog.items = R.shortCatalog.items.filter(function(item) {
|
||
return item.title !== t;
|
||
});
|
||
if (R.shortCatalog.items.length !== before) writeShortCatalog(R.shortCatalog);
|
||
}
|
||
function chooseShortCourse(cards, pending) {
|
||
if (!state.preferShort) return null;
|
||
const status = ensureShortCatalog();
|
||
if (status === "loading") return { wait: true };
|
||
if (status === "ready") {
|
||
const items = pendingShortItems();
|
||
if (items.length) {
|
||
const target = items[0];
|
||
const page = currentPage() || S.listPage || 1;
|
||
if (target.page && target.page !== page) {
|
||
if (Date.now() - R.shortJumpAt < 6e3) return { wait: true };
|
||
R.shortJumpAt = Date.now();
|
||
if (jumpToPage(target.page)) {
|
||
S.listPage = target.page;
|
||
save();
|
||
setStatus("优先短课程:转到第 " + target.page + " 页(" + formatDuration(target.duration) + ")");
|
||
return { wait: true };
|
||
}
|
||
if (Date.now() - R.lastPageJumpAt < 3500) return { wait: true };
|
||
shortFallback("无法跳转到第 " + target.page + " 页");
|
||
} else {
|
||
const card = cardForShortItem(cards, target);
|
||
if (card) {
|
||
if (card.status === "已学完" || S.done[card.title] || S.failed[card.title]) {
|
||
forgetShortCourse(card.title);
|
||
return { wait: true };
|
||
}
|
||
card.duration = target.duration;
|
||
R.shortJumpAt = 0;
|
||
return { card };
|
||
}
|
||
if (Date.now() - R.shortJumpAt < 8e3) return { wait: true };
|
||
shortFallback("目标课程不在第 " + target.page + " 页");
|
||
}
|
||
}
|
||
}
|
||
if (pending && pending.length) {
|
||
const vm = listVm();
|
||
const sorted = sortCardsByDuration(pending, vm);
|
||
if (sorted.length) {
|
||
sorted[0].duration = durationOfCard(sorted[0], vm);
|
||
return { card: sorted[0] };
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
function listTick() {
|
||
const cards = scanCards();
|
||
if (!cards.length) return;
|
||
if (R.pageChangeSig) {
|
||
if (cardsSignature(cards) === R.pageChangeSig && Date.now() - R.pageChangeAt < 1e4) {
|
||
return;
|
||
}
|
||
R.pageChangeSig = "";
|
||
}
|
||
if (R.pageChangeAt && Date.now() - R.pageChangeAt < 1500) return;
|
||
if (S.listUrl !== location.href) {
|
||
S.listUrl = location.href;
|
||
save();
|
||
}
|
||
const page = currentPage();
|
||
if (S.returning) {
|
||
if (page != null && S.listPage && page !== S.listPage) {
|
||
R.restoreAttempts++;
|
||
if (R.restoreAttempts > 12) {
|
||
console.warn(TAG, "页码恢复失败,改用当前页继续");
|
||
S.listPage = page || 1;
|
||
S.returning = false;
|
||
save();
|
||
} else if (S.listPage > page) {
|
||
if (jumpToPage(S.listPage)) return;
|
||
nextPage();
|
||
return;
|
||
} else {
|
||
jumpToPage(S.listPage);
|
||
return;
|
||
}
|
||
} else {
|
||
S.returning = false;
|
||
R.restoreAttempts = 0;
|
||
save();
|
||
setStatus("回到列表第 " + (page || 1) + " 页,继续…");
|
||
}
|
||
}
|
||
if (!S.returning && page != null && page !== S.listPage) {
|
||
S.listPage = page;
|
||
save();
|
||
}
|
||
if (S.current && S.phase === "entering") {
|
||
const waited = Date.now() - (S.current.startedAt || 0);
|
||
if (waited > 15e3) {
|
||
failCurrent("未能进入课程页面");
|
||
return;
|
||
}
|
||
if (waited > 4e3 && R.entryRetries < 2 && Date.now() - R.lastCardClickAt > 4e3) {
|
||
const card = cards.find(function(c) {
|
||
return c.title === S.current.title;
|
||
});
|
||
if (card) {
|
||
R.entryRetries++;
|
||
R.lastCardClickAt = Date.now();
|
||
console.log(TAG, "进入课程超时,重试点击卡片(第", R.entryRetries, "次)");
|
||
setStatus("重新进入课程:" + truncate(S.current.title, 22));
|
||
try {
|
||
clickNode(card.li);
|
||
} catch (e) {
|
||
}
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
if (S.current && S.phase === "learning") {
|
||
if (!R.listWithCurrentSince) R.listWithCurrentSince = Date.now();
|
||
if (Date.now() - R.listWithCurrentSince < 5e3) return;
|
||
const card = cards.find(function(c) {
|
||
return c.title === S.current.title;
|
||
});
|
||
if (card && /已学完|已完成|已学习/.test(card.status)) {
|
||
S.done[S.current.title] = Date.now();
|
||
forgetShortCourse(S.current.title);
|
||
S.completedThisRun = (S.completedThisRun || 0) + 1;
|
||
}
|
||
S.current = null;
|
||
S.phase = "idle";
|
||
S.returning = false;
|
||
R.listWithCurrentSince = 0;
|
||
save();
|
||
setStatus("已重置中断的课程状态,继续…");
|
||
}
|
||
if (S.current) return;
|
||
const pending = cards.filter((c) => {
|
||
if (!c.title) return false;
|
||
if (/已学完|已完成|已学习/.test(c.status)) return false;
|
||
if (S.done[c.title]) return false;
|
||
if (S.failed[c.title]) return false;
|
||
return true;
|
||
});
|
||
if (state.preferShort) {
|
||
const choice = chooseShortCourse(cards, pending);
|
||
if (choice && choice.wait) return;
|
||
if (choice && choice.card) {
|
||
if (Date.now() - R.lastCardClickAt < 5e3) return;
|
||
startCourse(choice.card);
|
||
return;
|
||
}
|
||
}
|
||
if (pending.length) {
|
||
if (Date.now() - R.lastCardClickAt < 5e3) return;
|
||
startCourse(pending[0]);
|
||
return;
|
||
}
|
||
const max = maxPageNumber();
|
||
if (page != null && max && page < max) {
|
||
nextPage();
|
||
return;
|
||
}
|
||
stopBatch("所有页面的未学完课程都已处理完毕");
|
||
}
|
||
function startCourse(card) {
|
||
const page = currentPage() || S.listPage || 1;
|
||
S.current = {
|
||
title: card.title,
|
||
id: courseIdOfCard(card),
|
||
status: card.status,
|
||
startedAt: Date.now(),
|
||
page,
|
||
duration: card.duration || 0
|
||
};
|
||
S.listPage = page;
|
||
S.phase = "entering";
|
||
S.returning = false;
|
||
R.lastCardClickAt = Date.now();
|
||
R.shortJumpAt = 0;
|
||
resetCourseRuntime();
|
||
save();
|
||
const durationText = card.duration ? "(" + formatDuration(card.duration) + ")" : "";
|
||
setStatus("进入课程" + durationText + ":" + truncate(card.title, 28));
|
||
console.log(
|
||
TAG,
|
||
"进入课程",
|
||
card.title,
|
||
"状态",
|
||
card.status,
|
||
card.duration ? "时长 " + formatDuration(card.duration) : ""
|
||
);
|
||
try {
|
||
card.li.click();
|
||
} catch (e) {
|
||
console.error(TAG, e);
|
||
failCurrent("课程卡片点击失败");
|
||
}
|
||
}
|
||
|
||
// src/complete.js
|
||
function completeCurrent() {
|
||
const title = S.current ? S.current.title : courseTitle();
|
||
if (title) {
|
||
S.done[title] = Date.now();
|
||
forgetShortCourse(title);
|
||
}
|
||
S.completedThisRun = (S.completedThisRun || 0) + 1;
|
||
S.consecutiveFailures = 0;
|
||
S.current = null;
|
||
S.phase = "idle";
|
||
S.returning = true;
|
||
save();
|
||
R.doneLatchKey = "";
|
||
R.lastSummary = null;
|
||
pauseAllMedia();
|
||
console.log(TAG, "课程完成:", title, "第", S.completedThisRun, "门,返回列表选下一门");
|
||
const limit = CFG.maxCourses > 0 ? CFG.maxCourses : Infinity;
|
||
if (S.completedThisRun >= limit) {
|
||
S.running = false;
|
||
S.runFinished = true;
|
||
save();
|
||
setStatus("本次已完成 " + S.completedThisRun + " 门,已停止。可重新勾选开始新一轮。");
|
||
} else {
|
||
setStatus("课程完成:" + truncate(title, 26) + "(本次 " + S.completedThisRun + ")");
|
||
}
|
||
later(function() {
|
||
goToList(true);
|
||
}, 1200);
|
||
}
|
||
function failCurrent(reason) {
|
||
const title = S.current ? S.current.title : courseTitle();
|
||
if (title) {
|
||
S.failed[title] = { reason, at: Date.now() };
|
||
forgetShortCourse(title);
|
||
}
|
||
S.current = null;
|
||
S.phase = "idle";
|
||
S.returning = true;
|
||
S.consecutiveFailures = (S.consecutiveFailures || 0) + 1;
|
||
if (S.consecutiveFailures >= CFG.maxConsecutiveFailures) {
|
||
S.running = false;
|
||
S.runFinished = true;
|
||
setStatus("连续失败 " + S.consecutiveFailures + " 门,已暂停:" + reason);
|
||
save();
|
||
return;
|
||
}
|
||
save();
|
||
setStatus("跳过:" + truncate(title, 24) + "(" + reason + ")");
|
||
later(function() {
|
||
goToList(true);
|
||
}, 1e3);
|
||
}
|
||
function stopBatch(reason) {
|
||
S.running = false;
|
||
S.runFinished = true;
|
||
save();
|
||
setStatus(reason || "批量任务结束");
|
||
updatePanel();
|
||
}
|
||
|
||
// src/panel.js
|
||
var SESSION_START_AT = Date.now();
|
||
var statusEl = null;
|
||
var statsEl = null;
|
||
var footerEl = null;
|
||
var cbBatch = null;
|
||
var btnSkip = null;
|
||
var labelAuto = null;
|
||
var labelBatch = null;
|
||
var labelShort = null;
|
||
var cbPreferShort = null;
|
||
var cbAuto = null;
|
||
var loginBox = null;
|
||
var loginUserEl = null;
|
||
var loginPassEl = null;
|
||
var loginRememberEl = null;
|
||
var loginAutoEl = null;
|
||
var btnOne = null;
|
||
var btnClearCreds = null;
|
||
var LABEL_CSS = "display:flex;align-items:center;gap:6px;cursor:pointer;margin-top:4px;";
|
||
function smallBtnCss(display) {
|
||
return [
|
||
"display:" + display,
|
||
"margin-top:6px",
|
||
"padding:2px 8px",
|
||
"cursor:pointer",
|
||
"background:#444",
|
||
"color:#eee",
|
||
"border:1px solid #666",
|
||
"border-radius:4px",
|
||
"font-size:12px"
|
||
].join(";");
|
||
}
|
||
function formatElapsed(ms) {
|
||
const total = Math.max(0, Math.floor(ms / 1e3));
|
||
const h = Math.floor(total / 3600);
|
||
const m = Math.floor(total % 3600 / 60);
|
||
const s = total % 60;
|
||
const p = function(n) {
|
||
return n < 10 ? "0" + n : "" + n;
|
||
};
|
||
return h > 0 ? h + ":" + p(m) + ":" + p(s) : m + ":" + p(s);
|
||
}
|
||
function setStatus(text) {
|
||
console.log(TAG, text);
|
||
if (statusEl) statusEl.textContent = text;
|
||
}
|
||
function updatePanel() {
|
||
if (cbBatch) cbBatch.checked = S.running;
|
||
if (statsEl) {
|
||
const limit = CFG.maxCourses > 0 ? CFG.maxCourses : "∞";
|
||
statsEl.textContent = "本次 " + S.completedThisRun + "/" + limit + " · 失败 " + Object.keys(S.failed).length + (R.idlePassed ? " · 验证 " + R.idlePassed : "");
|
||
}
|
||
if (footerEl) {
|
||
footerEl.textContent = "会话时长 " + formatElapsed(Date.now() - SESSION_START_AT);
|
||
}
|
||
if (btnSkip) {
|
||
btnSkip.style.display = S.current && route() === "course" ? "block" : "none";
|
||
}
|
||
if (cbAuto) cbAuto.checked = state.enabled;
|
||
if (cbPreferShort) cbPreferShort.checked = state.preferShort;
|
||
const onLogin = isLoginPage();
|
||
if (loginBox) loginBox.style.display = onLogin ? "block" : "none";
|
||
if (labelAuto) labelAuto.style.display = onLogin ? "none" : "flex";
|
||
if (labelBatch) labelBatch.style.display = onLogin ? "none" : "flex";
|
||
if (labelShort) labelShort.style.display = onLogin ? "none" : "flex";
|
||
if (statsEl) statsEl.style.display = onLogin ? "none" : "block";
|
||
if (btnOne) {
|
||
const onCourse = route() === "course" && !!S.current;
|
||
btnOne.textContent = onLogin ? "一键登录并刷课" : onCourse && S.running ? "停止刷课" : "一键刷课";
|
||
btnOne.title = onCourse && S.running ? "停止批量刷课" : "开始 / 继续批量刷课";
|
||
}
|
||
}
|
||
function buildPanel() {
|
||
if (window.top !== window.self) return;
|
||
if (document.getElementById(PANEL_ID)) return;
|
||
const box = document.createElement("div");
|
||
box.id = PANEL_ID;
|
||
box.style.cssText = [
|
||
"position:fixed",
|
||
"right:16px",
|
||
"bottom:16px",
|
||
"z-index:2147483647",
|
||
"background:rgba(24,24,28,.88)",
|
||
"color:#eee",
|
||
"padding:10px 12px",
|
||
"border-radius:8px",
|
||
"box-shadow:0 4px 16px rgba(0,0,0,.35)",
|
||
'font:12px/1.5 system-ui,-apple-system,"Microsoft YaHei",sans-serif',
|
||
"user-select:none",
|
||
"max-width:280px",
|
||
"min-width:220px"
|
||
].join(";");
|
||
const title = document.createElement("div");
|
||
title.style.cssText = "display:flex;align-items:baseline;gap:6px;margin-bottom:6px;";
|
||
const titleText = document.createElement("span");
|
||
titleText.textContent = "云学堂助手";
|
||
titleText.style.cssText = "font-weight:600;";
|
||
const titleVer = document.createElement("span");
|
||
titleVer.id = "yt-version";
|
||
titleVer.textContent = "v" + VERSION;
|
||
titleVer.title = "脚本版本 v" + VERSION;
|
||
titleVer.style.cssText = "font-weight:400;font-size:10px;color:#8ee6c0;opacity:.85;";
|
||
title.appendChild(titleText);
|
||
title.appendChild(titleVer);
|
||
btnOne = document.createElement("button");
|
||
btnOne.type = "button";
|
||
btnOne.id = "yt-oneclick";
|
||
btnOne.textContent = "一键刷课";
|
||
btnOne.style.cssText = [
|
||
"width:100%",
|
||
"margin:2px 0 6px",
|
||
"padding:5px 8px",
|
||
"cursor:pointer",
|
||
"background:#2f7d5b",
|
||
"color:#fff",
|
||
"border:1px solid #3a9a70",
|
||
"border-radius:5px",
|
||
"font-size:12px",
|
||
"font-weight:600"
|
||
].join(";");
|
||
btnOne.addEventListener("click", function() {
|
||
if (isLoginPage()) {
|
||
doLoginAndStart();
|
||
return;
|
||
}
|
||
if (route() === "course" && S.current && S.running) {
|
||
S.running = false;
|
||
save();
|
||
setStatus("自动刷课已暂停");
|
||
} else {
|
||
startBatch();
|
||
}
|
||
updatePanel();
|
||
});
|
||
loginBox = document.createElement("div");
|
||
loginBox.id = "yt-login-box";
|
||
loginBox.style.cssText = "display:none;margin-bottom:6px;";
|
||
const mkInput = function(id, type, ph) {
|
||
const el = document.createElement("input");
|
||
el.id = id;
|
||
el.type = type;
|
||
el.placeholder = ph;
|
||
el.autocomplete = type === "password" ? "off" : "username";
|
||
el.style.cssText = [
|
||
"width:100%",
|
||
"box-sizing:border-box",
|
||
"margin:0 0 4px",
|
||
"padding:4px 6px",
|
||
"background:#2a2a30",
|
||
"color:#eee",
|
||
"border:1px solid #555",
|
||
"border-radius:4px",
|
||
"font-size:12px",
|
||
"user-select:text"
|
||
].join(";");
|
||
return el;
|
||
};
|
||
loginUserEl = mkInput("yt-login-user", "text", "员工邮箱/账号");
|
||
loginPassEl = mkInput("yt-login-pass", "password", "密码");
|
||
const mkCheck = function(id, text) {
|
||
const label = document.createElement("label");
|
||
label.style.cssText = "display:inline-flex;align-items:center;gap:4px;cursor:pointer;margin-right:10px;";
|
||
const cb = document.createElement("input");
|
||
cb.type = "checkbox";
|
||
cb.id = id;
|
||
label.appendChild(cb);
|
||
label.appendChild(document.createTextNode(text));
|
||
return { label, cb };
|
||
};
|
||
const rem = mkCheck("yt-login-remember", "记住密码");
|
||
loginRememberEl = rem.cb;
|
||
const auto = mkCheck("yt-login-auto", "自动登录");
|
||
loginAutoEl = auto.cb;
|
||
loginRememberEl.addEventListener("change", function() {
|
||
if (!loginRememberEl.checked) clearSavedCreds();
|
||
});
|
||
const remRow = document.createElement("div");
|
||
remRow.style.cssText = "margin:2px 0 4px;font-size:11px;color:#bbb;";
|
||
remRow.appendChild(rem.label);
|
||
remRow.appendChild(auto.label);
|
||
btnClearCreds = document.createElement("button");
|
||
btnClearCreds.type = "button";
|
||
btnClearCreds.textContent = "清除密码";
|
||
btnClearCreds.style.cssText = [
|
||
"padding:1px 6px",
|
||
"cursor:pointer",
|
||
"background:#444",
|
||
"color:#eee",
|
||
"border:1px solid #666",
|
||
"border-radius:4px",
|
||
"font-size:10px"
|
||
].join(";");
|
||
btnClearCreds.addEventListener("click", function() {
|
||
clearSavedCreds();
|
||
if (loginPassEl) loginPassEl.value = "";
|
||
if (loginUserEl) loginUserEl.value = "";
|
||
setStatus("已清除保存的密码");
|
||
});
|
||
const loginHint = document.createElement("div");
|
||
loginHint.textContent = "密码仅存本机(base64 混淆,非加密),公用电脑请勿勾选记住";
|
||
loginHint.style.cssText = "color:#d9a441;font-size:10px;line-height:1.35;margin-top:2px;";
|
||
loginBox.appendChild(loginUserEl);
|
||
loginBox.appendChild(loginPassEl);
|
||
loginBox.appendChild(remRow);
|
||
loginBox.appendChild(btnClearCreds);
|
||
loginBox.appendChild(loginHint);
|
||
labelAuto = document.createElement("label");
|
||
labelAuto.style.cssText = "display:flex;align-items:center;gap:6px;cursor:pointer;";
|
||
cbAuto = document.createElement("input");
|
||
cbAuto.type = "checkbox";
|
||
cbAuto.id = "yt-auto-enabled";
|
||
cbAuto.checked = state.enabled;
|
||
cbAuto.addEventListener("change", function() {
|
||
state.enabled = cbAuto.checked;
|
||
try {
|
||
localStorage.setItem(LS_ENABLED, cbAuto.checked ? "1" : "0");
|
||
} catch (e) {
|
||
}
|
||
setStatus(cbAuto.checked ? "自动连播已开启" : "自动连播已关闭");
|
||
});
|
||
labelAuto.appendChild(cbAuto);
|
||
labelAuto.appendChild(document.createTextNode("手动课程自动连播"));
|
||
labelBatch = document.createElement("label");
|
||
labelBatch.style.cssText = LABEL_CSS;
|
||
cbBatch = document.createElement("input");
|
||
cbBatch.type = "checkbox";
|
||
cbBatch.id = "yt-batch-enabled";
|
||
cbBatch.checked = S.running;
|
||
cbBatch.addEventListener("change", function() {
|
||
if (cbBatch.checked) {
|
||
startBatch();
|
||
} else {
|
||
S.running = false;
|
||
save();
|
||
setStatus("自动刷课已暂停");
|
||
}
|
||
updatePanel();
|
||
});
|
||
labelBatch.appendChild(cbBatch);
|
||
labelBatch.appendChild(document.createTextNode("自动刷课(批量)"));
|
||
labelShort = document.createElement("label");
|
||
labelShort.style.cssText = LABEL_CSS;
|
||
cbPreferShort = document.createElement("input");
|
||
cbPreferShort.type = "checkbox";
|
||
cbPreferShort.id = "yt-prefer-short";
|
||
cbPreferShort.checked = state.preferShort;
|
||
cbPreferShort.title = "按课程总时长从短到长选课";
|
||
cbPreferShort.addEventListener("change", function() {
|
||
state.preferShort = cbPreferShort.checked;
|
||
try {
|
||
localStorage.setItem(LS_PREFER_SHORT, state.preferShort ? "1" : "0");
|
||
} catch (e) {
|
||
}
|
||
if (!state.preferShort) {
|
||
R.shortCatalog = null;
|
||
R.shortCatalogLoading = false;
|
||
R.shortCatalogFailedAt = 0;
|
||
try {
|
||
sessionStorage.removeItem(SS_SHORT_CATALOG);
|
||
} catch (e) {
|
||
}
|
||
setStatus("已关闭短课程优先,按列表顺序选课");
|
||
} else {
|
||
R.shortCatalogFailedAt = 0;
|
||
setStatus("已开启短课程优先:时长正序");
|
||
}
|
||
updatePanel();
|
||
});
|
||
labelShort.appendChild(cbPreferShort);
|
||
labelShort.appendChild(document.createTextNode("优先短课程(时长正序)"));
|
||
statusEl = document.createElement("div");
|
||
statusEl.id = "yt-status";
|
||
statusEl.textContent = "待机";
|
||
statusEl.style.cssText = "margin-top:6px;color:#8ee6c0;word-break:break-all;";
|
||
statsEl = document.createElement("div");
|
||
statsEl.id = "yt-stats";
|
||
statsEl.style.cssText = "margin-top:2px;color:#aaa;";
|
||
btnSkip = document.createElement("button");
|
||
btnSkip.type = "button";
|
||
btnSkip.textContent = "跳过本课";
|
||
btnSkip.style.cssText = smallBtnCss("none");
|
||
btnSkip.addEventListener("click", function() {
|
||
if (S.current) failCurrent("手动跳过");
|
||
});
|
||
const clearFail = document.createElement("button");
|
||
clearFail.type = "button";
|
||
clearFail.textContent = "清除失败记录";
|
||
clearFail.style.cssText = smallBtnCss("inline-block");
|
||
clearFail.addEventListener("click", function() {
|
||
S.failed = {};
|
||
S.consecutiveFailures = 0;
|
||
S.runFinished = false;
|
||
save();
|
||
updatePanel();
|
||
setStatus("已清除失败记录");
|
||
});
|
||
const hint = document.createElement("div");
|
||
hint.textContent = "真实播放计时 · 保持窗口可见";
|
||
hint.style.cssText = "margin-top:6px;color:#777;font-size:11px;";
|
||
const footer = document.createElement("div");
|
||
footer.id = "yt-session-duration";
|
||
footer.textContent = "会话时长 " + formatElapsed(0);
|
||
footer.style.cssText = "margin-top:4px;color:#8ee6c0;font-size:11px;";
|
||
footerEl = footer;
|
||
box.appendChild(title);
|
||
box.appendChild(loginBox);
|
||
box.appendChild(btnOne);
|
||
box.appendChild(labelAuto);
|
||
box.appendChild(labelBatch);
|
||
box.appendChild(labelShort);
|
||
box.appendChild(statusEl);
|
||
box.appendChild(statsEl);
|
||
box.appendChild(btnSkip);
|
||
box.appendChild(clearFail);
|
||
box.appendChild(hint);
|
||
box.appendChild(footer);
|
||
document.documentElement.appendChild(box);
|
||
}
|
||
function ensurePanel() {
|
||
if (window.top !== window.self) return;
|
||
if (!document.getElementById(PANEL_ID)) buildPanel();
|
||
updatePanel();
|
||
}
|
||
|
||
// src/course.js
|
||
function courseTick() {
|
||
R.listWithCurrentSince = 0;
|
||
if (!S.current) {
|
||
goToList();
|
||
return;
|
||
}
|
||
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;
|
||
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();
|
||
}
|
||
if ((location.hash || "").indexOf("#/scan") === 0) return;
|
||
if (handleAntiIdle()) return;
|
||
if (detectQuiz()) {
|
||
failCurrent("检测到答题/考试,需要手动处理");
|
||
return;
|
||
}
|
||
if (!R.quizChecked && document.querySelector(".yxtulcdsdk-catalog ul > li .tag-type")) {
|
||
R.quizChecked = true;
|
||
if (courseHasQuizLesson()) {
|
||
failCurrent("课程包含考试/测验章节");
|
||
return;
|
||
}
|
||
}
|
||
const summary = readSummary();
|
||
const prevSummaryX = R.lastSummary ? R.lastSummary.x : -1;
|
||
if (summary) {
|
||
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;
|
||
}
|
||
if (!isMedia && alignToFirstIncomplete()) return;
|
||
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 ? "(起点未对齐)" : ""));
|
||
}
|
||
const v = findPlayableVideo();
|
||
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;
|
||
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) {
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (!localDone) {
|
||
R.waitStart = 0;
|
||
} else if (!R.waitStart) {
|
||
R.waitStart = Date.now();
|
||
R.waitSummaryX = prevSummaryX;
|
||
R.waitLessonKey = lessonKey;
|
||
setStatus(isMedia ? "课程已播完,等待平台记录…" : "本节结束,等待平台记录…");
|
||
}
|
||
if (R.waitStart) {
|
||
const newKey = readActiveLessonKey();
|
||
const s2 = readSummary();
|
||
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 > 3e4) {
|
||
if (isMedia) {
|
||
R.waitStart = Date.now();
|
||
setStatus("课程已播完,等待平台完成记录…");
|
||
} else {
|
||
R.waitStart = 0;
|
||
setStatus("平台未更新进度,尝试下一节…");
|
||
advanceLesson();
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
if (Date.now() - R.lastProgressAt > CFG.stuckMs) {
|
||
failCurrent("长时间没有计时进展");
|
||
}
|
||
}
|
||
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: 1e3, 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);
|
||
}
|
||
}
|
||
function advanceLesson() {
|
||
if (Date.now() - R.lastAdvanceAt < 5e3) 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("没有找到下一节,等待课程完成…");
|
||
}
|
||
}
|
||
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));
|
||
}
|
||
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 > 6e3) {
|
||
R.manualAdvanceAt = Date.now();
|
||
const next = findNextLessonElement();
|
||
if (next) {
|
||
switchLesson(next);
|
||
} else {
|
||
const btn = findNextButton();
|
||
if (btn) clickNode(btn);
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
ensurePlaying(v);
|
||
}
|
||
|
||
// src/index.js
|
||
function tick2() {
|
||
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);
|
||
}
|
||
}
|
||
setTick(tick2);
|
||
if (window.top !== window.self) {
|
||
every(function() {
|
||
try {
|
||
hookAllVideos();
|
||
} catch (e) {
|
||
}
|
||
}, CFG.tickMs);
|
||
} else {
|
||
ensurePanel();
|
||
hookAllVideos();
|
||
every(tick2, CFG.tickMs);
|
||
onWindow(window, "hashchange", function() {
|
||
later(tick2, 300);
|
||
});
|
||
}
|
||
try {
|
||
Object.defineProperty(window, "__ytAuto", {
|
||
value: {
|
||
S,
|
||
R,
|
||
state,
|
||
tick: tick2,
|
||
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) {
|
||
}
|
||
console.log("%c云学堂助手 v" + VERSION + " 已加载", "color:#3c9;font-weight:bold");
|
||
setStatus(S.runFinished ? "本次已完成 " + S.completedThisRun + " 门(已停止)" : S.running ? "自动刷课运行中" : "待机");
|
||
})();
|