新增无头运行能力,解决 Linux 无图形化服务器上无法常驻的问题;同时改掉 "遇到考试课/无进展课就跳过"的策略,改为在同门课上退避重试。 - runner/:自己起无头 Chromium,用 CDP 把构建产物注入页面主世界(等价 @grant none),不装插件、不需要桌面环境或 Xvfb;含浏览器探测与启动参数、 持久化 profile、陈旧 SingletonLock 清理、优雅退出、日志轮转、启动预检 - runner.config.mjs / start-headless.cmd:通用配置 + Windows 一键入口; 凭据只走 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD 环境变量 - src/exam.js:含考试章节的课程自动进考试 → 开始/继续 → 空卷提交 → 确定, 交卷后靠平台记账推进课程;maxExamTries 限制同门课交卷次数,防不记账时死循环 - 去掉跳过与黑名单:没学完就退避重试(30s 起翻倍,最长 5 分钟), 同门课超过 maxCourseRetries 次才停整轮并提示,替代原 maxConsecutiveFailures - 每完成一门都重新拉列表接口取平台实时状态再选下一门,避免用陈旧队列 - 学习进度改存 sessionStorage(yun-xue-tang-run-state-v3),整页跳转/刷新可续; 运行开关不跨会话,浏览器重启后必须显式开跑 - dev/:新增 real-cdp / real-run / real-test / real-e2e 实跑脚本与 check.mjs 统一静态检查(JS 22 + 批处理 2),替代逐文件 node --check - docs/evidence/headless-linux-evidence.txt + 实跑截图,README 补无头部署章节
295 lines
11 KiB
JavaScript
295 lines
11 KiB
JavaScript
// ======== 面板 ========
|
|
import { CFG, VERSION, LS_ENABLED, LS_PREFER_SHORT, SS_SHORT_CATALOG, PANEL_ID, TAG } from './config.js';
|
|
import { R, S, state, saveRunState } from './state.js';
|
|
import { route } from './route.js';
|
|
import { isLoginPage, startBatch, doLoginAndStart, clearSavedCreds } from './login.js';
|
|
|
|
// 会话时长:面板 footer 显示本次脚本会话自加载起已运行多久
|
|
const SESSION_START_AT = Date.now();
|
|
export let statusEl = null;
|
|
let statsEl = null;
|
|
let footerEl = null;
|
|
let cbBatch = null;
|
|
let labelAuto = null;
|
|
let labelBatch = null;
|
|
let labelShort = null;
|
|
let cbPreferShort = null;
|
|
let cbAuto = null;
|
|
let loginBox = null;
|
|
export let loginUserEl = null;
|
|
export let loginPassEl = null;
|
|
export let loginRememberEl = null;
|
|
export let loginAutoEl = null;
|
|
let btnOne = null;
|
|
let btnClearCreds = null;
|
|
|
|
// 面板里两个复选框行共用同一套 label 样式
|
|
const LABEL_CSS = 'display:flex;align-items:center;gap:6px;cursor:pointer;margin-top:4px;';
|
|
|
|
function formatElapsed(ms) {
|
|
const total = Math.max(0, Math.floor(ms / 1000));
|
|
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);
|
|
}
|
|
|
|
export function setStatus(text) {
|
|
console.log(TAG, text);
|
|
if (statusEl) statusEl.textContent = text;
|
|
}
|
|
|
|
export function updatePanel() {
|
|
if (cbBatch) cbBatch.checked = S.batchRunning;
|
|
if (statsEl) {
|
|
const limit = CFG.maxCourses > 0 ? CFG.maxCourses : '∞';
|
|
let text = '本次 ' + S.completedThisRun + '/' + limit;
|
|
const retryTitles = Object.keys(S.courseRetries || {}).length;
|
|
if (retryTitles) text += ' · 待重试 ' + retryTitles;
|
|
if (R.retryWaitUntil && Date.now() < R.retryWaitUntil) {
|
|
text += ' · ' + Math.ceil((R.retryWaitUntil - Date.now()) / 1000) + 's 后重试';
|
|
}
|
|
text += (R.idlePassed ? ' · 验证 ' + R.idlePassed : '');
|
|
statsEl.textContent = text;
|
|
}
|
|
if (footerEl) {
|
|
footerEl.textContent = '会话时长 ' + formatElapsed(Date.now() - SESSION_START_AT);
|
|
}
|
|
if (cbAuto) cbAuto.checked = state.autoPlayEnabled;
|
|
if (cbPreferShort) cbPreferShort.checked = state.preferShortCourses;
|
|
|
|
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.activeCourse;
|
|
btnOne.textContent = onLogin
|
|
? '一键登录并刷课'
|
|
: (onCourse && S.batchRunning ? '停止刷课' : '一键刷课');
|
|
btnOne.title = onCourse && S.batchRunning ? '停止列表刷课' : '开始 / 继续列表刷课';
|
|
}
|
|
}
|
|
|
|
export 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 = 'yun-xue-tang-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 = 'yun-xue-tang-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.activeCourse && S.batchRunning) {
|
|
S.batchRunning = false;
|
|
saveRunState();
|
|
setStatus('列表刷课已暂停');
|
|
} else {
|
|
startBatch();
|
|
}
|
|
updatePanel();
|
|
});
|
|
|
|
loginBox = document.createElement('div');
|
|
loginBox.id = 'yun-xue-tang-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('yun-xue-tang-login-user', 'text', '员工邮箱/账号');
|
|
loginPassEl = mkInput('yun-xue-tang-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: label, cb: cb };
|
|
};
|
|
const rem = mkCheck('yun-xue-tang-login-remember', '记住密码');
|
|
loginRememberEl = rem.cb;
|
|
const auto = mkCheck('yun-xue-tang-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 = 'yun-xue-tang-auto-enabled';
|
|
cbAuto.checked = state.autoPlayEnabled;
|
|
cbAuto.addEventListener('change', function () {
|
|
state.autoPlayEnabled = 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 = 'yun-xue-tang-batch-enabled';
|
|
cbBatch.checked = S.batchRunning;
|
|
cbBatch.addEventListener('change', function () {
|
|
if (cbBatch.checked) {
|
|
startBatch();
|
|
} else {
|
|
S.batchRunning = false;
|
|
saveRunState();
|
|
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 = 'yun-xue-tang-prefer-short';
|
|
cbPreferShort.checked = state.preferShortCourses;
|
|
cbPreferShort.title = '按课程总时长从短到长选课';
|
|
cbPreferShort.addEventListener('change', function () {
|
|
state.preferShortCourses = cbPreferShort.checked;
|
|
try { localStorage.setItem(LS_PREFER_SHORT, state.preferShortCourses ? '1' : '0'); } catch (e) {}
|
|
if (!state.preferShortCourses) {
|
|
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 = 'yun-xue-tang-status';
|
|
statusEl.textContent = '待机';
|
|
statusEl.style.cssText = 'margin-top:6px;color:#8ee6c0;word-break:break-all;';
|
|
|
|
statsEl = document.createElement('div');
|
|
statsEl.id = 'yun-xue-tang-stats';
|
|
statsEl.style.cssText = 'margin-top:2px;color:#aaa;';
|
|
|
|
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 = 'yun-xue-tang-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(hint);
|
|
box.appendChild(footer);
|
|
document.documentElement.appendChild(box);
|
|
}
|
|
|
|
export function ensurePanel() {
|
|
if (window.top !== window.self) return;
|
|
if (!document.getElementById(PANEL_ID)) buildPanel();
|
|
updatePanel();
|
|
}
|