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
+327
@@ -0,0 +1,327 @@
|
||||
// ======== 面板 ========
|
||||
import { CFG, VERSION, LS_ENABLED, LS_PREFER_SHORT, SS_SHORT_CATALOG, PANEL_ID, TAG } from './config.js';
|
||||
import { R, S, state, save } from './state.js';
|
||||
import { route } from './route.js';
|
||||
import { isLoginPage, startBatch, doLoginAndStart, clearSavedCreds } from './login.js';
|
||||
import { failCurrent } from './complete.js';
|
||||
|
||||
// 会话时长:面板 footer 显示本次脚本会话自加载起已运行多久
|
||||
const SESSION_START_AT = Date.now();
|
||||
export let statusEl = null;
|
||||
let statsEl = null;
|
||||
let footerEl = null;
|
||||
let cbBatch = null;
|
||||
let btnSkip = 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;';
|
||||
|
||||
// 「跳过本课」/「清除失败记录」共用一套按钮样式,只有 display 不同
|
||||
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 / 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.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 ? '停止批量刷课' : '开始 / 继续批量刷课';
|
||||
}
|
||||
}
|
||||
|
||||
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 = '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: label, cb: 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);
|
||||
}
|
||||
|
||||
export function ensurePanel() {
|
||||
if (window.top !== window.self) return;
|
||||
if (!document.getElementById(PANEL_ID)) buildPanel();
|
||||
updatePanel();
|
||||
}
|
||||
Reference in new issue
Block a user