// ======== 面板 ======== import { CFG, VERSION, LS_ENABLED, LS_PREFER_SHORT, SS_SHORT_CATALOG, PANEL_ID, TAG } from './config.js'; import { R, S, state, points, saveRunState } from './state.js'; import { LS_POINTS_MODE } from './points.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 labelPoints = null; let cbPoints = null; let pointsEl = 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; if (cbPoints) cbPoints.checked = state.pointsEnabled; if (pointsEl) { pointsEl.style.display = state.pointsEnabled ? 'block' : 'none'; pointsEl.textContent = state.pointsEnabled ? points.summary() : ''; } 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 (labelPoints) labelPoints.style.display = onLogin ? 'none' : 'flex'; if (statsEl) statsEl.style.display = onLogin ? 'none' : 'block'; if (pointsEl) pointsEl.style.display = onLogin ? 'none' : (state.pointsEnabled ? 'block' : 'none'); 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('优先短课程(时长正序)')); labelPoints = document.createElement('label'); labelPoints.style.cssText = LABEL_CSS; cbPoints = document.createElement('input'); cbPoints.type = 'checkbox'; cbPoints.id = 'yun-xue-tang-points-enabled'; cbPoints.checked = state.pointsEnabled; cbPoints.title = '按课时类型计分:文档1 / 视频3 / 音频2 / 微课2,每天最多 24 分'; cbPoints.addEventListener('change', function () { state.pointsEnabled = cbPoints.checked; points.setEnabled(state.pointsEnabled); try { localStorage.setItem(LS_POINTS_MODE, state.pointsEnabled ? '1' : '0'); } catch (e) {} R.pointsInventory = null; R.pointsInventoryFailedAt = 0; setStatus(state.pointsEnabled ? '已开启每日积分模式:' + points.summary() : '已关闭每日积分模式,恢复普通刷课'); updatePanel(); }); labelPoints.appendChild(cbPoints); labelPoints.appendChild(document.createTextNode('每日积分模式(凑满 24 分就停)')); 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;'; pointsEl = document.createElement('div'); pointsEl.id = 'yun-xue-tang-points'; pointsEl.style.cssText = 'margin-top:2px;color:#8ee6c0;'; 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(labelPoints); box.appendChild(statusEl); box.appendChild(statsEl); box.appendChild(pointsEl); 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(); }