// ==UserScript== // @name 学习通课程任务助手 // @namespace local.chaoxing.course.helper // @version 3.0.0 // @description 本地待办任务助手,支持附件身份、断点恢复、单媒体调度、任务处理器和本地题库 // @author JXY // @license MIT // @homepage https://scriptcat.org/ // @supportURL https://scriptcat.org/ // @match *://chaoxing.com/* // @match *://*.chaoxing.com/* // @match *://xueyinonline.com/* // @match *://*.xueyinonline.com/* // @grant GM_getValue // @grant GM_setValue // @grant GM_addValueChangeListener // @grant GM_registerMenuCommand // @grant unsafeWindow // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const SCRIPT_VERSION = '3.0.0'; const BANK_SCHEMA_VERSION = 1; const TODO_EXPORT_SCHEMA_VERSION = 2; const SETTINGS_SCHEMA_VERSION = 1; const STORAGE_SCHEMA_VERSION = 4; const MESSAGE_TYPE = 'cx-course-helper-worker-v1'; const PAGE_WINDOW = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window; const IS_TOP = PAGE_WINDOW.top === PAGE_WINDOW; const IS_DIRECT_CHILD = !IS_TOP && PAGE_WINDOW.parent === PAGE_WINDOW.top; const FRAME_PATH = (() => { const path = []; let current = PAGE_WINDOW; try { while (current !== current.top) { const parent = current.parent; let frameIndex = -1; for (let index = 0; index < parent.frames.length; index += 1) { if (parent.frames[index] === current) { frameIndex = index; break; } } path.unshift(frameIndex < 0 ? Number.MAX_SAFE_INTEGER : frameIndex); current = parent; } } catch (_) { path.unshift(Number.MAX_SAFE_INTEGER); } return path; })(); let controllerActive = IS_TOP; let parentControllerSeen = IS_TOP; let controllerMenuRegistered = false; const WORKER_ID = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`; const TAB_ID = `tab-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`; const STORE = { enabled: 'cx_helper_enabled', speed: 'cx_helper_speed', volume: 'cx_helper_volume', autoJump: 'cx_helper_auto_jump', bank: 'cx_helper_bank', unanswered: 'cx_helper_unanswered', logs: 'cx_helper_logs', uiState: 'cx_helper_ui_state', adapterHealth: 'cx_helper_adapter_health', storageBackup: 'cx_helper_storage_backup', schema: 'cx_helper_schema', tabLease: 'cx_helper_tab_lease', todos: 'cx_helper_todos', todoScopes: 'cx_helper_todo_scopes', scanSession: 'cx_helper_scan_session', queuePaused: 'cx_helper_queue_paused', navigationMode: 'cx_helper_navigation_mode', executionJournal: 'cx_helper_execution_journal' }; const CONFIG = { defaultSpeed: 1, interval: 1000, workerTtl: 6500, mediaStallThreshold: Number(PAGE_WINDOW.__XXT_TEST_MEDIA_STALL_MS__) || 30000, mediaRetryLimit: Number(PAGE_WINDOW.__XXT_TEST_MEDIA_RETRY_LIMIT__) || 3, navigationCooldown: 8000, completionStableTime: Number(PAGE_WINDOW.__XXT_TEST_COMPLETION_STABLE_MS__) || 2500, taskWarmupTime: Number(PAGE_WINDOW.__XXT_TEST_TASK_WARMUP_MS__) || 4000, serverCompletionTimeout: Number(PAGE_WINDOW.__XXT_TEST_SERVER_CONFIRM_MS__) || 30000, tabLeaseTtl: Number(PAGE_WINDOW.__XXT_TEST_TAB_LEASE_MS__) || 5000, storageWarningBytes: Number(PAGE_WINDOW.__XXT_TEST_STORAGE_WARNING_BYTES__) || 4 * 1024 * 1024, selectorGraceTime: Number(PAGE_WINDOW.__XXT_TEST_SELECTOR_GRACE_MS__) || 12000, detailPageDelay: 1800, selectors: { questions: [ '.TiMu', '.questionLi', '.quesItem', '.ans-ques', '.judge-question', '.combination-question', '.ans-videoquiz', '.line-question', '.cloze-question', '.reading-question' ], optionGroups: [ '.Zy_ulTop > li', '.answerList > li', '.answerOption', '.option-item', '.ans-videoquiz-opt label', 'label:has(input[type="radio"])', 'label:has(input[type="checkbox"])' ], textInputs: ['input[type="text"]', 'textarea', 'select'], submit: [ 'button.submitBtn', '.submitBtn', '.answer-submit', 'button[data-action="submit"]', 'button[type="submit"]', '#videoquiz-submit' ], next: [ 'button.nextChapter', 'a.nextChapter', '.next-btn', 'button[title*="下一节"]', 'a[title*="下一节"]', 'button[aria-label*="下一"]', 'a[aria-label*="下一"]' ], currentCatalog: [ '.posCatalog_select', '.catalog_points .curr', '.chapter_unit .curr', '.catalog_level .curr', '.ncells .curr', '[aria-current="true"]' ], completed: [ '.completed', '.finish', '.finished', '.jobFinish', '.icon_Completed', '.ans-job-finished', '[data-status="completed"]', '[title*="已完成"]' ], incomplete: [ '.jobUnfinish', '.unfinished', '.icon-unfinish', '[data-status="unfinished"]', '[title*="未完成"]' ] } }; const COURSE_TEMPLATES = { 'chaoxing-new': { detect: ['#courseChapter', '.course-card', '.courseItem', '.course-item'], courseLinks: ['.course-card a[href]', '.courseItem a[href]', '.course-item a[href]'], courseCards: ['.course-card', '.courseItem', '.course-item', '.course_item'], catalog: ['#courseChapter .chapter_item a[href]', '#courseChapter [onclick*="chapter"]', '.chapter_item .catalog_name'] }, 'chaoxing-legacy': { detect: ['.chapter_unit', '.catalog_level', '.ncells', '.Mconright'], courseLinks: ['.Mconright a[href]', 'li a[href*="courseId="]'], courseCards: ['.Mconright', 'li', 'article'], catalog: ['.chapter_unit a[href*="studentstudy"]', '.catalog_level a[href*="studentstudy"]', '.ncells a[href]'] }, 'portal-iframe': { detect: [], courseLinks: ['a[href*="courseId="]', 'a[href*="clazzid="]', 'a[href*="/mycourse/"]'], courseCards: ['.course-card', '.courseItem', '.Mconright', 'li', 'article'], catalog: ['#courseChapter .chapter_item a[href]', '.chapter_unit a[href*="studentstudy"]', '.catalog_level a[href*="studentstudy"]', '.ncells a[href]'] }, xueyin: { detect: ['.catalog_points', '.posCatalog_name'], courseLinks: ['a[href*="courseId="]', 'a[href*="clazzid="]', 'a[href*="/mycourse/"]'], courseCards: ['.course-card', '.courseItem', 'li', 'article'], catalog: ['.catalog_points a[href]', '.posCatalog_name', 'a[href*="chapterId"][href*="courseId"]'] }, generic: { detect: [], courseLinks: ['a[href*="courseId="]', 'a[href*="courseid="]', 'a[href*="clazzid="]', 'a[href*="/mycourse/"]'], courseCards: ['.courseItem', '.course-item', '.course_item', '.course-card', '.Mconright', 'li', 'article'], catalog: [ '.chapter_unit a[href*="studentstudy"]', '.catalog_level a[href*="studentstudy"]', '.catalog_points a[href]', '.ncells a[href]', '#courseChapter .chapter_item a[href]', '#courseChapter [onclick*="chapter"]', '.chapter_item .catalog_name', 'a[href*="chapterId"][href*="courseId"]', '.posCatalog_name' ] } }; const PLATFORM_ADAPTER_API_VERSION = 1; const TASK_HANDLER_API_VERSION = 1; const PLATFORM_ADAPTERS = Object.fromEntries(Object.entries(COURSE_TEMPLATES).map(([id, selectors]) => [id, Object.freeze({ id, apiVersion: PLATFORM_ADAPTER_API_VERSION, selectors, detect: () => id === 'generic' || selectors.detect.some((selector) => document.querySelector(selector)), catalog: () => scanCatalogEntries(), media: () => Array.from(document.querySelectorAll('video, audio')).filter(isVisible), completion: (node) => hasCompletionMarker(node), question: () => getQuestionBlocks() })])); const FALLBACK_PREFIX = 'cx_helper_fallback:'; const FALLBACK_META_PREFIX = 'cx_helper_fallback_meta:'; const storageApi = { get(key, fallback) { try { const marker = localStorage.getItem(`${FALLBACK_META_PREFIX}${key}`); if (marker) { const envelope = JSON.parse(marker); if (envelope && Number(envelope.revision) > 0) return envelope.value; } } catch (_) { // Ignore malformed fallback metadata and continue with the primary backend. } try { if (typeof GM_getValue === 'function') return GM_getValue(key, fallback); } catch (error) { console.warn('[XXT] GM_getValue failed, using local storage', error); } try { const raw = localStorage.getItem(`${FALLBACK_PREFIX}${key}`); return raw === null ? fallback : JSON.parse(raw); } catch (_) { return fallback; } }, set(key, value) { const revision = Date.now() * 1000 + Math.floor(Math.random() * 1000); try { if (typeof GM_setValue === 'function') { GM_setValue(key, value); try { localStorage.removeItem(`${FALLBACK_META_PREFIX}${key}`); localStorage.removeItem(`${FALLBACK_PREFIX}${key}`); } catch (_) { /* Ignore fallback cleanup failures. */ } return true; } } catch (error) { console.warn('[XXT] GM_setValue failed, using local storage', error); } try { const envelope = { revision, value }; localStorage.setItem(`${FALLBACK_PREFIX}${key}`, JSON.stringify(value)); localStorage.setItem(`${FALLBACK_META_PREFIX}${key}`, JSON.stringify(envelope)); return true; } catch (_) { PAGE_WINDOW.__XXT_STORAGE_WARNING__ = '本地存储空间不足或不可用'; return false; } }, listen(key, callback) { try { if (typeof GM_addValueChangeListener === 'function') { return GM_addValueChangeListener(key, callback); } } catch (error) { console.warn('[XXT] GM_addValueChangeListener failed', error); } return null; }, registerMenu(label, callback) { try { if (typeof GM_registerMenuCommand === 'function') { return GM_registerMenuCommand(label, callback); } } catch (error) { console.warn('[XXT] GM_registerMenuCommand failed', error); } return null; } }; const state = { enabled: toBoolean(storageApi.get(STORE.enabled, true)), speed: clampSpeed(storageApi.get(STORE.speed, CONFIG.defaultSpeed)), volume: Math.max(0, Math.min(1, Number(storageApi.get(STORE.volume, 0)) || 0)), autoJump: toBoolean(storageApi.get(STORE.autoJump, true)), bank: loadObject(STORE.bank), bankIndex: new Map(), unanswered: loadObject(STORE.unanswered), todos: loadScopedTodos(), scanSession: loadObject(STORE.scanSession), queuePaused: toBoolean(storageApi.get(STORE.queuePaused, false)), queueNavigationAt: 0, queueFinishedLogged: false, navigationMode: ['next', 'incomplete', 'manual'].includes(storageApi.get(STORE.navigationMode, 'next')) ? storageApi.get(STORE.navigationMode, 'next') : 'next', finalState: null, intervention: null, executionJournal: loadArray(STORE.executionJournal), executionRecovered: false, operationRevision: 0, operationTimers: new Set(), auxiliaryRuntime: new Map(), domObserver: null, observedBody: null, domRevision: 0, lastMutationAt: Date.now(), domCycleScheduled: false, media: new WeakMap(), knownMedia: new Set(), answeredQuestions: new WeakSet(), submittedQuestions: new WeakSet(), pageSubmitted: false, workers: new Map(), mediaPermit: { workerId: '', index: -1, revision: 0 }, mediaPermitRevision: 0, lastMediaFailureKey: '', panel: null, progressWindow: null, progressOpen: false, todoWindow: null, todoOpen: false, logWindow: null, logOpen: false, logAutoScroll: true, todoSearch: '', todoStatusFilter: 'all', todoTypeFilter: 'all', logFilter: 'all', uiState: loadObject(STORE.uiState), adapterHealth: loadObject(STORE.adapterHealth), tabPrimary: !IS_TOP, leaseClaimId: '', leaseConfirmedAt: 0, selectorMisses: 0, storageMigrated: false, timer: null, running: false, seenWork: false, stableSince: 0, completionAwaitStartedAt: 0, completionAwaitTaskKey: '', lastNavigation: 0, detailJumpAttempted: false, detailExpandAttempted: false, detailPageEnteredAt: Date.now(), currentTaskKey: '', taskEnteredAt: Date.now(), ignoreWorkerUntil: 0, lastLog: '', lastLogAt: 0, lastError: '' }; function invalidateOperations() { state.operationRevision += 1; state.operationTimers.forEach((timer) => window.clearTimeout(timer)); state.operationTimers.clear(); state.domCycleScheduled = false; return state.operationRevision; } function scheduleOperation(callback, delay) { const revision = state.operationRevision; const timer = window.setTimeout(() => { state.operationTimers.delete(timer); if (revision !== state.operationRevision) return; callback(); }, delay); state.operationTimers.add(timer); return timer; } function ensureDomObserver() { if (!document.body || state.observedBody === document.body) return; state.domObserver?.disconnect(); state.observedBody = document.body; state.lastMutationAt = Date.now(); state.domObserver = new MutationObserver((mutations) => { const externalMutation = mutations.some((mutation) => { const target = mutation.target instanceof Element ? mutation.target : mutation.target.parentElement; return !target?.closest?.('#cx-helper-panel,#cx-helper-progress-window,#cx-helper-todo-window,#cx-helper-log-window,#cx-helper-style'); }); if (!externalMutation) return; state.domRevision += 1; state.lastMutationAt = Date.now(); if (state.domCycleScheduled) return; state.domCycleScheduled = true; scheduleOperation(() => { state.domCycleScheduled = false; runCycle(); }, 120); }); state.domObserver.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['data', 'data-attachment', 'src', 'class', 'style', 'data-status'] }); } function discoveryStatus() { const registry = pageAttachmentDescriptors().filter((item) => item.job); const descriptor = frameAttachmentDescriptor(); const identities = new Set(); if (descriptor.identity) identities.add(descriptor.identity); document.querySelectorAll('iframe[data],iframe[data-attachment]').forEach((frame) => { const raw = frame.getAttribute('data') || frame.getAttribute('data-attachment'); if (!raw) return; try { const data = JSON.parse(raw); const jobId = compactText(data.jobid || data._jobid); const match = registry.find((item) => item.jobId === jobId); if (match?.identity) identities.add(match.identity); else if (jobId) identities.add(`job:${jobId}`); } catch (_) { /* Ignore malformed iframe metadata. */ } }); return { revision: state.domRevision, stableForMs: Date.now() - state.lastMutationAt, expected: registry.length, identities: Array.from(identities) }; } function isController() { return controllerActive; } function ensureTabLease() { if (!IS_TOP) return true; const now = Date.now(); const lease = loadObject(STORE.tabLease); if (lease.owner === TAB_ID && lease.claimId && lease.claimId === state.leaseClaimId) { const active = { ...lease, phase: 'active', expiresAt: now + CONFIG.tabLeaseTtl }; saveObject(STORE.tabLease, active); const verified = loadObject(STORE.tabLease); state.tabPrimary = verified.owner === TAB_ID && verified.claimId === state.leaseClaimId; if (state.tabPrimary) state.leaseConfirmedAt = now; return state.tabPrimary; } if (!lease.owner || Number(lease.expiresAt || 0) <= now) { const claimId = `${TAB_ID}:${now}:${Math.random().toString(36).slice(2)}`; state.leaseClaimId = claimId; const candidate = { owner: TAB_ID, claimId, phase: 'candidate', claimedAt: now, expiresAt: now + CONFIG.tabLeaseTtl, account: detectAccountKey(), term: currentTermKey() }; saveObject(STORE.tabLease, candidate); const verified = loadObject(STORE.tabLease); state.tabPrimary = verified.owner === TAB_ID && verified.claimId === claimId; state.leaseConfirmedAt = state.tabPrimary ? now : 0; return state.tabPrimary; } if (lease.owner === TAB_ID && !state.leaseClaimId) { state.leaseClaimId = lease.claimId || `${TAB_ID}:legacy`; state.tabPrimary = true; state.leaseConfirmedAt = now; return true; } if (state.tabPrimary) { invalidateOperations(); cancelTaskHandlers('lease-lost'); revokeMediaPermit(); pauseLocalMedia(); } state.tabPrimary = false; return false; } function handleTabLeaseChange() { if (!IS_TOP) return; const lease = loadObject(STORE.tabLease); const ownsLease = lease.owner === TAB_ID && lease.claimId && lease.claimId === state.leaseClaimId; if (ownsLease || !lease.owner || Number(lease.expiresAt || 0) <= Date.now()) return; state.tabPrimary = false; state.leaseConfirmedAt = 0; invalidateOperations(); cancelTaskHandlers('lease-lost'); revokeMediaPermit(); pauseLocalMedia(); updatePanel(activeWorkerSummary()); } function releaseTabLease() { invalidateOperations(); cancelTaskHandlers('pagehide'); state.domObserver?.disconnect(); state.domObserver = null; state.observedBody = null; if (!IS_TOP) return; const lease = loadObject(STORE.tabLease); if (lease.owner === TAB_ID && (!state.leaseClaimId || lease.claimId === state.leaseClaimId)) { saveObject(STORE.tabLease, { owner: '', claimId: '', phase: 'released', expiresAt: 0 }); } } function broadcastToChildren(message) { try { for (let index = 0; index < PAGE_WINDOW.frames.length; index += 1) { PAGE_WINDOW.frames[index].postMessage(message, '*'); } } catch (_) { // Cross-origin frames still accept postMessage when referenced directly. } } function announceController() { if (!isController()) return; broadcastToChildren({ type: MESSAGE_TYPE, kind: 'controller-present' }); } function activateController() { controllerActive = true; parentControllerSeen = true; if (!state.storageMigrated) migrateStorage(); recoverExecutionState(); if (document.body) createPanel(); if (!controllerMenuRegistered) { storageApi.registerMenu('启动/暂停助手', toggleEnabled); storageApi.registerMenu('导入本地题库', importBank); storageApi.registerMenu('导出未匹配题目', exportUnanswered); controllerMenuRegistered = true; } announceController(); } function deactivateController() { if (IS_TOP) return; controllerActive = false; invalidateOperations(); cancelTaskHandlers('controller-deactivated'); state.panel?.remove(); state.progressWindow?.remove(); state.todoWindow?.remove(); state.logWindow?.remove(); state.panel = null; state.progressWindow = null; state.todoWindow = null; state.logWindow = null; state.progressOpen = false; state.todoOpen = false; state.logOpen = false; state.workers.clear(); } function probeParentController() { if (!IS_DIRECT_CHILD || window.parent === window) return; window.parent.postMessage({ type: MESSAGE_TYPE, kind: 'controller-probe' }, '*'); } rebuildBankIndex(); migrateLegacyUnanswered(); function toBoolean(value) { if (typeof value === 'string') return value === 'true' || value === '1'; return Boolean(value); } function clampSpeed(value) { const number = Number(value); if (!Number.isFinite(number)) return CONFIG.defaultSpeed; return Math.max(0.5, Math.min(16, number)); } function loadObject(key) { const raw = storageApi.get(key, '{}'); try { const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw; return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}; } catch (_) { backupCorruptValue(key, raw); return {}; } } function loadArray(key) { const raw = storageApi.get(key, '[]'); try { const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw; return Array.isArray(parsed) ? parsed : []; } catch (_) { backupCorruptValue(key, raw); return []; } } function backupCorruptValue(key, raw) { if (key === STORE.storageBackup) return; const existing = storageApi.get(STORE.storageBackup, {}); const backups = existing && typeof existing === 'object' && !Array.isArray(existing) ? existing : {}; backups[`${key}:${Date.now()}`] = String(raw).slice(0, 200000); storageApi.set(STORE.storageBackup, backups); } function todoScopeKey() { return `${detectAccountKey() || 'anonymous'}|${currentTermKey()}|${scanPlatformKey()}`; } function loadScopedTodos() { const scopes = loadObject(STORE.todoScopes); const key = todoScopeKey(); if (Array.isArray(scopes[key])) return scopes[key]; if (Object.keys(scopes).length > 0 && detectAccountKey()) return []; return loadArray(STORE.todos); } function saveObject(key, value) { storageApi.set(key, JSON.stringify(value)); } function normalize(value) { return String(value ?? '') .replace(/[\u00a0\r\n\t ]+/g, ' ') .replace(/[“”"'「」『』()()【】\[\]]/g, '') .replace(/^\s*(?:第\s*)?\d+\s*(?:题)?[.、::]?\s*/, '') .replace(/^\s*(?:单选题|多选题|判断题|填空题|简答题|名词解释|论述题|计算题|连线题|完形填空|阅读理解)\s*[::]?\s*/i, '') .replace(/\s*[((]?\s*\d+(?:\.\d+)?\s*分\s*[))]?\s*$/i, '') .trim() .toLowerCase(); } function rebuildBankIndex() { state.bankIndex = new Map(); Object.entries(state.bank).forEach(([question, answer]) => { if (!question.startsWith('_unanswered:')) { const key = normalize(question); if (key) state.bankIndex.set(key, answer); } }); } function migrateLegacyUnanswered() { let changed = false; Object.keys(state.bank).forEach((key) => { if (!key.startsWith('_unanswered:')) return; const question = key.slice('_unanswered:'.length); if (question && !Object.prototype.hasOwnProperty.call(state.unanswered, question)) { state.unanswered[question] = ''; } delete state.bank[key]; changed = true; }); if (changed) { saveObject(STORE.bank, state.bank); saveObject(STORE.unanswered, state.unanswered); rebuildBankIndex(); } } function isVisible(node) { if (!(node instanceof Element)) return false; const style = getComputedStyle(node); if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return false; const rect = node.getBoundingClientRect(); return rect.width > 0 && rect.height > 0; } function queryAllInOrder(selectors, root = document) { if (!selectors.length) return []; try { return Array.from(root.querySelectorAll(selectors.join(','))); } catch (_) { const nodes = []; selectors.forEach((selector) => { try { root.querySelectorAll(selector).forEach((node) => { if (!nodes.includes(node)) nodes.push(node); }); } catch (_) { // Ignore selectors unsupported by an older browser engine. } }); return nodes; } } function firstVisible(selectors, root = document) { return queryAllInOrder(selectors, root).find(isVisible) || null; } function clickElement(node) { if (!(node instanceof Element) || !isVisible(node)) return false; node.scrollIntoView({ block: 'center', behavior: 'auto' }); ['mouseover', 'mousedown', 'mouseup'].forEach((type) => { node.dispatchEvent(new MouseEvent(type, { bubbles: true, cancelable: true })); }); node.click(); return true; } function setNativeValue(input, value) { const prototype = input instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : input instanceof HTMLSelectElement ? HTMLSelectElement.prototype : HTMLInputElement.prototype; const descriptor = Object.getOwnPropertyDescriptor(prototype, 'value'); if (descriptor && descriptor.set) descriptor.set.call(input, String(value ?? '')); else input.value = String(value ?? ''); input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true })); } function pushLog(message, force = false, category = '') { const now = Date.now(); if (!force && message === state.lastLog && now - state.lastLogAt < 4000) return; state.lastLog = message; state.lastLogAt = now; if (!isController()) { window.parent.postMessage({ type: MESSAGE_TYPE, kind: 'log', message, hops: 0 }, '*'); return; } const inferred = category || (/异常|失败|错误|超时/.test(message) ? 'error' : /媒体|播放|视频|音频/.test(message) ? 'media' : /题目|题库|答案/.test(message) ? 'question' : /进入|跳转|打开/.test(message) ? 'navigation' : 'system'); const line = `${new Date().toLocaleTimeString()} [${inferred}] ${message}`; const existing = storageApi.get(STORE.logs, []); const lines = Array.isArray(existing) ? existing : []; storageApi.set(STORE.logs, [line, ...lines].slice(0, 100)); updatePanel(); } function reportError(error) { const message = error instanceof Error ? error.message : String(error); state.lastError = message; pushLog(`运行异常:${message}`, true); } function recordExecutionEvent(type, details = {}) { const todo = currentRunningTodo(); const entry = { at: Date.now(), type, todoId: todo?.id || details.todoId || '', title: todo?.title || details.title || '', stage: todo?.stage || details.stage || '', attachmentId: todo?.attachmentId || details.attachmentId || '', reason: compactText(details.reason || '').slice(0, 240) }; const previous = state.executionJournal[0]; if (previous && previous.type === entry.type && previous.todoId === entry.todoId && previous.stage === entry.stage && previous.reason === entry.reason && entry.at - previous.at < 2000) return; state.executionJournal = [entry, ...state.executionJournal].slice(0, 120); storageApi.set(STORE.executionJournal, state.executionJournal); } function recoverExecutionState() { if (state.executionRecovered) return; state.executionRecovered = true; const todo = currentRunningTodo(); if (!todo) return; if (!todo.stage) todo.stage = 'running'; if (todo.stage === 'waiting-intervention') { state.queuePaused = true; state.enabled = false; storageApi.set(STORE.queuePaused, true); storageApi.set(STORE.enabled, false); state.lastError = todo.error || '任务等待人工处理'; } else if (todo.stage === 'navigating' && todoMatchesLocation(todo)) { todo.stage = 'running'; } saveTodos(); recordExecutionEvent('recovered', { reason: `恢复阶段 ${todo.stage}` }); } function bindStoreListeners() { storageApi.listen(STORE.enabled, (_name, _old, value) => { state.enabled = toBoolean(value); updatePanel(); }); storageApi.listen(STORE.speed, (_name, _old, value) => { state.speed = clampSpeed(value); updatePanel(); }); storageApi.listen(STORE.volume, (_name, _old, value) => { state.volume = Math.max(0, Math.min(1, Number(value) || 0)); document.querySelectorAll('video,audio').forEach((media) => { media.volume = state.volume; media.muted = state.volume === 0; }); updatePanel(); }); storageApi.listen(STORE.autoJump, (_name, _old, value) => { state.autoJump = toBoolean(value); updatePanel(); }); storageApi.listen(STORE.navigationMode, (_name, _old, value) => { if (['next', 'incomplete', 'manual'].includes(value)) state.navigationMode = value; updatePanel(); }); storageApi.listen(STORE.bank, () => { state.bank = loadObject(STORE.bank); rebuildBankIndex(); }); storageApi.listen(STORE.unanswered, () => { state.unanswered = loadObject(STORE.unanswered); }); storageApi.listen(STORE.todos, () => { state.todos = loadScopedTodos(); updatePanel(); }); storageApi.listen(STORE.todoScopes, () => { state.todos = loadScopedTodos(); updatePanel(); }); storageApi.listen(STORE.scanSession, () => { state.scanSession = loadObject(STORE.scanSession); updatePanel(); }); storageApi.listen(STORE.queuePaused, (_name, _old, value) => { state.queuePaused = toBoolean(value); updatePanel(); }); storageApi.listen(STORE.tabLease, handleTabLeaseChange); } function getQuestionBlocks() { const candidates = queryAllInOrder(CONFIG.selectors.questions).filter(isVisible); const blocks = candidates.map((node) => { if (!node.matches('.TiMu')) return node; const ownsAnswerControls = node.querySelector('.Zy_ulTop, .answerList, input, textarea, select'); return ownsAnswerControls ? node : node.closest('.questionLi, .quesItem, .ans-ques') || node; }); const unique = blocks.filter((node, index) => blocks.indexOf(node) === index && isVisible(node)); const hasControls = (node) => Boolean(node.querySelector('input, textarea, select, button.submitBtn, .submitBtn')); return unique.filter((node) => !unique.some((other) => other !== node && node.contains(other) && hasControls(other))); } function getQuestionText(block) { const title = block.querySelector('.Zy_TItle, .quesTitle, .question-title, .mark_name, .ans-videoquiz-title, .ans-videoquiz-question, [data-question-title]'); const imageHints = Array.from((title || block).querySelectorAll('img')).map((image) => { const alt = compactText(image.getAttribute('alt')); if (alt) return `图片 ${alt}`; try { return `图片 ${new URL(image.src, location.href).pathname.split('/').filter(Boolean).at(-1) || ''}`; } catch (_) { return ''; } }).filter(Boolean); if (title) return normalize([title.textContent, ...imageHints].join(' ')); const clone = block.cloneNode(true); queryAllInOrder(CONFIG.selectors.optionGroups, clone).forEach((node) => node.remove()); queryAllInOrder(CONFIG.selectors.textInputs, clone).forEach((node) => node.remove()); return normalize([clone.textContent, ...imageHints].join(' ')).slice(0, 500); } function findLocalBankAnswer(question) { const key = normalize(question); if (!key) return null; if (state.bankIndex.has(key)) return state.bankIndex.get(key); if (key.length < 6) return null; for (const [candidate, answer] of state.bankIndex.entries()) { if (candidate.length >= 6 && (key.includes(candidate) || candidate.includes(key))) return answer; } return null; } const LOCAL_BANK_PROVIDER = Object.freeze({ id: 'local-json', schemaVersion: BANK_SCHEMA_VERSION, readOnly: true, find: findLocalBankAnswer, export: (records = state.bank) => ({ schemaVersion: BANK_SCHEMA_VERSION, provider: 'local-json', exportedAt: new Date().toISOString(), records }) }); function activeBankProvider() { return LOCAL_BANK_PROVIDER; } function findAnswer(question) { return activeBankProvider().find(question); } function rememberUnanswered(question) { if (!question || Object.prototype.hasOwnProperty.call(state.unanswered, question)) return; state.unanswered[question] = ''; saveObject(STORE.unanswered, state.unanswered); pushLog(`未匹配题目:${question.slice(0, 36)}`); } function findOptionNodes(block) { for (const selector of CONFIG.selectors.optionGroups) { let nodes = []; try { nodes = Array.from(block.querySelectorAll(selector)).filter(isVisible); } catch (_) { nodes = []; } if (nodes.length) return nodes; } return []; } function optionText(node) { const preferred = node.querySelector('.after, .option-content, .answerText, .clearfix'); const text = normalize((preferred || node).textContent); return text.replace(/^[a-z][.、.]\s*/i, '').trim(); } function optionLetter(node) { const text = normalize(node.textContent); const match = text.match(/^([a-z])[.、.\s]/i); return match ? match[1].toLowerCase() : ''; } function isOptionSelected(node) { const input = node.matches('input') ? node : node.querySelector('input[type="radio"], input[type="checkbox"]'); if (input && input.checked) return true; if (node.getAttribute('aria-checked') === 'true' || node.getAttribute('aria-selected') === 'true') return true; return /(^|\s)(selected|checked|active|on)(\s|$)/i.test(node.className || ''); } function isQuestionCompleted(block) { if (block.matches('.completed, .finished, [data-status="completed"]')) return true; return Boolean(firstVisible(['.answerAnalysis', '.analysis', '.Py_answer', '.rightAnswer', '.result-right'], block)); } function answerQuestion(block) { const question = getQuestionText(block); if (isQuestionCompleted(block)) { return { completed: true, pending: false, matched: true, stage: 'confirmed' }; } if (state.answeredQuestions.has(block)) { const stage = state.submittedQuestions.has(block) ? 'submitted' : 'filled'; return { completed: true, pending: false, matched: true, stage }; } const answer = findAnswer(question); if (answer === null || answer === undefined || answer === '') { rememberUnanswered(question); return { completed: false, pending: true, matched: false, stage: 'discovered', reason: '本地题库未匹配' }; } const answers = (Array.isArray(answer) ? answer : [answer]).map(normalize).filter(Boolean); const options = findOptionNodes(block); let changed = false; let matchedCount = 0; if (options.length) { answers.forEach((wanted) => { const target = wanted.replace(/^[a-z][.、.]\s*/i, '').trim(); const node = options.find((option) => { const text = optionText(option); const letter = optionLetter(option); return Boolean(text) && (text === target || text.startsWith(target) || target.startsWith(text)) || Boolean(letter) && letter === wanted; }); if (!node) return; matchedCount += 1; if (!isOptionSelected(node)) changed = clickElement(node) || changed; }); } else { const inputs = queryAllInOrder(CONFIG.selectors.textInputs, block).filter(isVisible); const richFrames = Array.from(block.querySelectorAll('iframe')).filter((frame) => { try { return Boolean(frame.contentDocument?.body); } catch (_) { return false; } }); inputs.forEach((input, index) => { const value = answers[index] ?? answers[0]; if (value !== undefined && normalize(input.value) !== value) { setNativeValue(input, Array.isArray(answer) ? answer[index] ?? answer[0] : answer); changed = true; } }); richFrames.forEach((frame, index) => { const value = answers[inputs.length + index] ?? answers[index] ?? answers[0]; const body = frame.contentDocument.body; if (value !== undefined && normalize(body.innerText || body.textContent) !== normalize(value)) { body.textContent = String(value); body.dispatchEvent(new Event('input', { bubbles: true })); body.dispatchEvent(new Event('change', { bubbles: true })); changed = true; } }); const controlCount = inputs.length + richFrames.length; matchedCount = controlCount ? Math.min(controlCount, answers.length || 1) : 0; } const fullyMatched = matchedCount >= answers.length && answers.length > 0; if (!fullyMatched) { rememberUnanswered(question); return { completed: false, pending: true, matched: true, stage: 'matched', reason: '答案与页面控件不匹配' }; } const submit = firstVisible(CONFIG.selectors.submit, block); if (submit && !state.submittedQuestions.has(block)) { clickElement(submit); state.submittedQuestions.add(block); } state.answeredQuestions.add(block); if (changed) pushLog(`已填写题目:${question.slice(0, 36)}`); return { completed: true, pending: false, matched: true, stage: submit ? 'submitted' : 'filled' }; } function processQuestions() { const blocks = activeCourseTemplate().question(); let completed = 0; let pending = 0; const stages = { discovered: 0, matched: 0, filled: 0, submitted: 0, confirmed: 0 }; const items = []; blocks.forEach((block) => { const result = answerQuestion(block); if (result.completed) completed += 1; if (result.pending) pending += 1; if (Object.prototype.hasOwnProperty.call(stages, result.stage)) stages[result.stage] += 1; items.push({ title: getQuestionText(block), stage: result.stage, matched: result.matched, reason: result.reason || '' }); }); if (blocks.length && pending === 0 && !state.pageSubmitted) { const pageSubmit = firstVisible(CONFIG.selectors.submit); if (pageSubmit && !blocks.some((block) => block.contains(pageSubmit))) { clickElement(pageSubmit); state.pageSubmitted = true; pushLog('已提交当前页面题目'); } } return { total: blocks.length, completed, pending, stages, items }; } function mediaCompleted(media) { const info = state.media.get(media); if (info?.completed) return true; const duration = Number(media.duration); const complete = media.ended || (Number.isFinite(duration) && duration > 0 && media.currentTime >= duration - 0.35); if (complete) { state.media.set(media, { ...info, completed: true, started: info?.started || false }); } return complete; } function mediaSource(media) { return String(media.currentSrc || media.src || media.querySelector?.('source[src]')?.src || ''); } function cleanTaskDisplayName(value) { let text = compactText(value); if (!text) return ''; if (/^https?:\/\//i.test(text)) { try { text = new URL(text).pathname.split('/').filter(Boolean).at(-1) || ''; } catch (_) { /* Keep raw text. */ } } else if (/[\\/]/.test(text)) { text = text.split(/[\\/]/).filter(Boolean).at(-1) || text; } text = text.replace(/[?#].*$/, ''); try { text = decodeURIComponent(text); } catch (_) { /* Keep undecoded name. */ } text = compactText(text) .replace(/^(?:视频|音频|文件)(?:名称)?\s*[::]\s*/i, '') .replace(/\.(?:mp4|m4v|mov|avi|mkv|flv|wmv|webm|mp3|m4a|aac|wav|ogg)$/i, '') .trim(); const token = text.replace(/[\s_\-—]+/g, ''); if (!token || /^(?:(?:文本|text)(?:[一二三四五六七八九十百\d]+)?)+$/i.test(token)) return ''; if (/^(?:视频|音频|任务点|课程视频|当前任务|未命名|无标题)\d*$/i.test(token)) return ''; return text.slice(0, 180); } function mediaTaskTitleFromDocument() { const selectors = [ '[data-video-name]', '[data-filename]', '.ans-video-title', '.video-title', '.ans-attach-title', '.file-name', '.fileName', '.attachment-name' ]; for (const node of queryAllInOrder(selectors).filter(isVisible)) { const name = cleanTaskDisplayName( node.getAttribute?.('data-video-name') || node.getAttribute?.('data-filename') || node.getAttribute?.('title') || node.textContent ); if (name) return name; } for (const media of document.querySelectorAll('video, audio')) { const name = mediaElementTitle(media); if (name) return name; } return ''; } function mediaElementTitle(media) { return cleanTaskDisplayName( media?.getAttribute?.('data-video-name') || media?.getAttribute?.('data-filename') || media?.getAttribute?.('data-title') || media?.getAttribute?.('title') || media?.getAttribute?.('aria-label') || mediaSource(media) ); } function prepareMedia(media, index) { let info = state.media.get(media); const source = mediaSource(media); if (info && info.source !== source) { Object.assign(info, { source, sourceRevision: Number(info.sourceRevision || 0) + 1, started: false, completed: false, startFailures: 0, playAttemptAt: 0, uiAttemptAt: 0, lastCurrentTime: Number(media.currentTime) || 0, lastProgressAt: Date.now(), stallRetries: 0, failed: false, failureReason: '' }); pushLog(`媒体 ${index + 1} 已切换资源,重新接管`, true, 'media'); } if (!info) { info = { source, sourceRevision: 1, listenerToken: `media-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`, started: false, completed: false, startFailures: 0, playAttemptAt: 0, uiAttemptAt: 0, lastCurrentTime: Number(media.currentTime) || 0, lastProgressAt: Date.now(), stallRetries: 0, failed: false, failureReason: '' }; state.media.set(media, info); info.endedHandler = () => { const current = state.media.get(media); if (!current || current.listenerToken !== info.listenerToken) return; current.completed = true; pushLog(`媒体 ${index + 1} 已完成`); }; media.addEventListener('ended', info.endedHandler); } media.playbackRate = state.speed; media.volume = state.volume; media.muted = state.volume === 0; return info; } function updateMediaHealth(media, info, permitted, index) { const now = Date.now(); const currentTime = Number(media.currentTime) || 0; if (currentTime > Number(info.lastCurrentTime || 0) + 0.15) { info.lastCurrentTime = currentTime; info.lastProgressAt = now; return; } if (!info.lastProgressAt) info.lastProgressAt = now; if (!permitted || !info.started || media.paused || info.failed) return; if (now - info.lastProgressAt < CONFIG.mediaStallThreshold) return; info.stallRetries = Number(info.stallRetries || 0) + 1; info.lastProgressAt = now; pauseMediaElement(media); if (info.stallRetries >= CONFIG.mediaRetryLimit) { info.failed = true; info.failureReason = `媒体 ${index + 1} 播放无进度,已重试 ${info.stallRetries} 次`; pushLog(info.failureReason, true); return; } info.started = false; info.playAttemptAt = 0; info.uiAttemptAt = 0; pushLog(`媒体 ${index + 1} 播放卡住,自动重试 ${info.stallRetries}/${CONFIG.mediaRetryLimit}`, true); } function resetFailedMedia() { document.querySelectorAll('video, audio').forEach((media) => { const info = state.media.get(media); if (!info?.failed) return; info.failed = false; info.failureReason = ''; info.stallRetries = 0; info.startFailures = 0; info.started = false; info.lastCurrentTime = Number(media.currentTime) || 0; info.lastProgressAt = Date.now(); info.playAttemptAt = 0; info.uiAttemptAt = 0; }); } function findMediaPlayButton(media) { const selectors = [ '.vjs-big-play-button', '.vjs-play-control', '.ans-insertvideo-play', '.playButton', '.xgplayer-start', '.dplayer-play-icon', 'button[aria-label*="播放"]', '[title="播放"]' ]; const container = media.closest( '.ans-attach-ct, .video-js, .ans-video, .video-container, .xgplayer, .dplayer' ) || media.parentElement; return firstVisible(selectors, container || document) || firstVisible(selectors, document); } function markMediaStarted(info, index, total) { if (info.started) return; info.started = true; info.startFailures = 0; pushLog(`开始媒体 ${index + 1}/${total},${state.speed}x`); } function recordMediaStartFailure(info, index) { info.startFailures = Number(info.startFailures || 0) + 1; if (info.startFailures < CONFIG.mediaRetryLimit) return; info.failed = true; info.failureReason = `媒体 ${index + 1} 启动失败,已重试 ${info.startFailures} 次`; pushLog(info.failureReason, true); } function attemptMediaStart(media, info, index, total) { const now = Date.now(); const needsPlayerButton = Number(media.readyState || 0) < 2 || Number(info.startFailures || 0) > 0; if (needsPlayerButton && now - info.uiAttemptAt >= 1200) { info.uiAttemptAt = now; const playButton = findMediaPlayButton(media); if (playButton && clickElement(playButton)) { info.playAttemptAt = now; scheduleOperation(() => { if (state.media.get(media) !== info || !media.isConnected || info.completed || info.failed) return; if (!media.paused) { markMediaStarted(info, index, total); return; } try { const result = media.play(); if (result && typeof result.then === 'function') { result.then(() => markMediaStarted(info, index, total)).catch(() => recordMediaStartFailure(info, index)); } else { markMediaStarted(info, index, total); } } catch (_) { recordMediaStartFailure(info, index); } }, 150); return; } } if (now - info.playAttemptAt < 1200) return; info.playAttemptAt = now; try { const result = media.play(); if (result && typeof result.then === 'function') { result.then(() => markMediaStarted(info, index, total)).catch(() => recordMediaStartFailure(info, index)); } else { markMediaStarted(info, index, total); } } catch (_) { recordMediaStartFailure(info, index); } } function processMedia() { const mediaList = activeCourseTemplate().media(); for (const oldMedia of state.knownMedia) { if (oldMedia.isConnected && mediaList.includes(oldMedia)) continue; pauseMediaElement(oldMedia); state.knownMedia.delete(oldMedia); } mediaList.forEach((media) => state.knownMedia.add(media)); let completed = 0; const items = []; mediaList.forEach((media, index) => { const info = prepareMedia(media, index); const complete = mediaCompleted(media); const permitted = state.mediaPermit.workerId === WORKER_ID && state.mediaPermit.index === index; if (complete) { completed += 1; items.push({ index, type: media.tagName.toLowerCase(), completed: true, title: mediaElementTitle(media), sourceRevision: info.sourceRevision, listenerToken: info.listenerToken, currentTime: Number(media.currentTime) || 0, duration: Number(media.duration) || 0, speed: Number(media.playbackRate) || state.speed }); return; } updateMediaHealth(media, info, permitted, index); if (info.failed) { items.push({ index, title: mediaElementTitle(media), completed: false, failed: true, failureReason: info.failureReason, sourceRevision: info.sourceRevision, listenerToken: info.listenerToken, retries: info.stallRetries }); return; } if (!permitted && !media.paused) { pauseMediaElement(media); } if (permitted && media.paused) attemptMediaStart(media, info, index, mediaList.length); items.push({ index, type: media.tagName.toLowerCase(), completed: false, failed: false, title: mediaElementTitle(media), sourceRevision: info.sourceRevision, listenerToken: info.listenerToken, currentTime: Number(media.currentTime) || 0, duration: Number(media.duration) || 0, speed: Number(media.playbackRate) || state.speed }); }); const failedItems = items.filter((item) => item.failed); return { total: mediaList.length, completed, failed: failedItems.length, failureReason: failedItems[0]?.failureReason || '', items }; } function pauseLocalMedia() { document.querySelectorAll('video, audio').forEach((media) => { if (!media.paused) pauseMediaElement(media); }); } function cancelMediaOperations(reason = 'cancelled') { for (const media of state.knownMedia) { const info = state.media.get(media); if (info?.endedHandler) media.removeEventListener('ended', info.endedHandler); pauseMediaElement(media); } if (reason !== 'not-detected') state.knownMedia.clear(); return true; } function cancelQuestionOperations(reason = 'cancelled') { if (reason === 'not-detected') return true; state.answeredQuestions = new WeakSet(); state.submittedQuestions = new WeakSet(); state.pageSubmitted = false; return true; } function emptyAuxiliaryStatus() { return { total: 0, completed: 0, pending: 0, items: [], issue: null }; } function currentAttachmentModule() { return normalize(frameAttachmentDescriptor().module); } function auxiliaryRuntime(type) { const descriptor = frameAttachmentDescriptor(); const key = `${type}:${descriptor.identity || location.href}`; if (!state.auxiliaryRuntime.has(key)) state.auxiliaryRuntime.set(key, { key, type, startedAt: Date.now(), lastActionAt: 0, index: 0 }); return state.auxiliaryRuntime.get(key); } function cancelAuxiliaryOperations(type, reason) { if (reason === 'not-detected') return true; for (const [key, runtime] of state.auxiliaryRuntime.entries()) { if (!type || runtime.type === type || runtime.type === 'finish') state.auxiliaryRuntime.delete(key); } return true; } function invokeFinishJob() { const runtime = auxiliaryRuntime('finish'); if (runtime.finishedInvoked) return true; let finish = null; try { finish = PAGE_WINDOW.finishJob; } catch (_) { finish = null; } if (typeof finish !== 'function') return false; finish.call(PAGE_WINDOW); runtime.finishedInvoked = true; return true; } function detectTimedReadingTask() { return /timereader|timing/.test(location.href) || Boolean(document.querySelector('iframe[name="bookifame"][src*="timing"]')); } function detectReadingTask() { if (detectTimedReadingTask()) return false; const module = currentAttachmentModule(); return /insertbook|insertdoc|document|read/.test(module) || Boolean(document.querySelector('#img.imglook,.ans-book,.ans-document,[data-module="insertdoc"],[data-module="insertbook"]')); } function processReadingTask() { const descriptor = frameAttachmentDescriptor(); if (descriptor.passed) return { total: 1, completed: 1, pending: 0, items: [{ type: 'reading', stage: 'confirmed', title: descriptor.title }], issue: null }; const runtime = auxiliaryRuntime('reading'); document.scrollingElement?.scrollTo?.(0, document.scrollingElement.scrollHeight); const invoked = invokeFinishJob(); runtime.stage = invoked ? 'waiting-confirm' : 'running'; return { total: 1, completed: invoked ? 1 : 0, pending: invoked ? 0 : 1, items: [{ type: 'reading', stage: runtime.stage, title: descriptor.title || '阅读任务' }], issue: invoked ? null : { type: 'reading-wait', label: '阅读任务处理中', advice: '等待页面提供完成接口', blocking: false } }; } function processTimedReadingTask() { const descriptor = frameAttachmentDescriptor(); if (descriptor.passed) return { total: 1, completed: 1, pending: 0, items: [{ type: 'timed-reading', stage: 'confirmed', title: descriptor.title }], issue: null }; const runtime = auxiliaryRuntime('timed-reading'); const frame = document.querySelector('iframe[name="bookifame"][src*="timing"]'); let timing = 60; try { timing = Number(new URL(frame?.src || location.href).searchParams.get('timing')) || 60; } catch (_) { timing = 60; } const requiredMs = Number(PAGE_WINDOW.__XXT_TEST_TIMED_READING_MS__) || Math.max(3000, (timing + 3) * 3000); const elapsed = Date.now() - runtime.startedAt; const completed = elapsed >= requiredMs; if (completed) invokeFinishJob(); return { total: 1, completed: completed ? 1 : 0, pending: completed ? 0 : 1, items: [{ type: 'timed-reading', stage: completed ? 'waiting-confirm' : 'running', elapsed, requiredMs, title: descriptor.title || '计时阅读' }], issue: null }; } function detectPptTask() { const module = currentAttachmentModule(); return /insertflash|ppt|presentation/.test(module) || Boolean(document.querySelector('.swiper-container,.swiper-slide,.ppt-container')); } function processPptTask() { const descriptor = frameAttachmentDescriptor(); const slides = Array.from(document.querySelectorAll('.swiper-container .swiper-slide,.ppt-container .slide')); const total = Math.max(1, slides.length); if (descriptor.passed) return { total: 1, completed: 1, pending: 0, items: [{ type: 'ppt', stage: 'confirmed', index: total, total, title: descriptor.title }], issue: null }; const runtime = auxiliaryRuntime('ppt'); const mediaBusy = Array.from(document.querySelectorAll('video,audio')).some((media) => !media.ended); if (!mediaBusy && runtime.index < total - 1 && Date.now() - runtime.lastActionAt >= 900) { let advanced = false; try { if (typeof PAGE_WINDOW.swiperNext === 'function') { PAGE_WINDOW.swiperNext(); advanced = true; } } catch (_) { /* Fall through to a visible next button. */ } if (!advanced) advanced = clickElement(firstVisible(['.swiper-button-next','.ppt-next','button[title*="下一页"]'])); if (advanced) { runtime.index += 1; runtime.lastActionAt = Date.now(); } } const completed = runtime.index >= total - 1 && !mediaBusy; if (completed) invokeFinishJob(); return { total: 1, completed: completed ? 1 : 0, pending: completed ? 0 : 1, items: [{ type: 'ppt', stage: completed ? 'waiting-confirm' : 'running', index: runtime.index + 1, total, title: descriptor.title || 'PPT' }], issue: null }; } function detectHyperlinkTask() { const module = currentAttachmentModule(); return /hyperlink|link/.test(module) || Boolean(document.querySelector('#hyperlink,[data-module="hyperlink"]')); } function processHyperlinkTask() { const descriptor = frameAttachmentDescriptor(); if (descriptor.passed) return { total: 1, completed: 1, pending: 0, items: [{ type: 'hyperlink', stage: 'confirmed', title: descriptor.title }], issue: null }; return { total: 1, completed: 0, pending: 1, items: [{ type: 'hyperlink', stage: 'waiting-intervention', title: descriptor.title || '链接任务' }], issue: { type: 'hyperlink', label: '检测到链接任务', advice: '请确认链接内容后手动打开', blocking: true } }; } function combineAuxiliaryStatuses(results) { const statuses = ['reading', 'timed-reading', 'ppt', 'hyperlink'].map((id) => results[id]).filter(Boolean); return statuses.reduce((combined, status) => { combined.total += status.total; combined.completed += status.completed; combined.pending += status.pending; combined.items.push(...status.items); if (!combined.issue && status.issue) combined.issue = status.issue; return combined; }, emptyAuxiliaryStatus()); } function pauseMediaElement(media) { try { media?.pause?.(); return true; } catch (_) { return false; } } const TASK_HANDLERS = Object.freeze([ Object.freeze({ id: 'media', apiVersion: TASK_HANDLER_API_VERSION, detect: () => activeCourseTemplate().media().length > 0, run: () => processMedia(), status: (result) => result || { total: 0, completed: 0, failed: 0, failureReason: '', items: [] }, cancel: (reason) => cancelMediaOperations(reason) }), Object.freeze({ id: 'questions', apiVersion: TASK_HANDLER_API_VERSION, detect: () => activeCourseTemplate().question().length > 0, run: () => processQuestions(), status: (result) => result || { total: 0, completed: 0, pending: 0, stages: { discovered: 0, matched: 0, filled: 0, submitted: 0, confirmed: 0 }, items: [] }, cancel: (reason) => cancelQuestionOperations(reason) }), Object.freeze({ id: 'reading', apiVersion: TASK_HANDLER_API_VERSION, detect: detectReadingTask, run: processReadingTask, status: (result) => result || emptyAuxiliaryStatus(), cancel: (reason) => cancelAuxiliaryOperations('reading', reason) }), Object.freeze({ id: 'timed-reading', apiVersion: TASK_HANDLER_API_VERSION, detect: detectTimedReadingTask, run: processTimedReadingTask, status: (result) => result || emptyAuxiliaryStatus(), cancel: (reason) => cancelAuxiliaryOperations('timed-reading', reason) }), Object.freeze({ id: 'ppt', apiVersion: TASK_HANDLER_API_VERSION, detect: detectPptTask, run: processPptTask, status: (result) => result || emptyAuxiliaryStatus(), cancel: (reason) => cancelAuxiliaryOperations('ppt', reason) }), Object.freeze({ id: 'hyperlink', apiVersion: TASK_HANDLER_API_VERSION, detect: detectHyperlinkTask, run: processHyperlinkTask, status: (result) => result || emptyAuxiliaryStatus(), cancel: (reason) => cancelAuxiliaryOperations('hyperlink', reason) }) ]); function runTaskHandlers() { return Object.fromEntries(TASK_HANDLERS.map((handler) => { if (!handler.detect()) { handler.cancel('not-detected'); return [handler.id, handler.status(null)]; } return [handler.id, handler.status(handler.run())]; })); } function cancelTaskHandlers(reason = 'cancelled') { TASK_HANDLERS.forEach((handler) => handler.cancel(reason)); } function frameAttachmentDescriptor() { const frames = []; try { let current = PAGE_WINDOW; for (let depth = 0; depth < 4 && current && current !== current.top; depth += 1) { const frame = current.frameElement; if (frame) frames.push(frame); current = current.parent; } } catch (_) { if (!frames.length) return correlateAttachmentDescriptor({ source: 'cross-origin-frame', jobId: '', module: '', title: '', status: '', passed: false }); } if (!frames.length) return correlateAttachmentDescriptor({ source: 'document', jobId: '', module: '', title: mediaTaskTitleFromDocument(), status: '', passed: false }); const frame = frames.find((item) => item.getAttribute('data') || item.getAttribute('data-attachment')) || frames[0]; let payload = {}; const raw = frame.getAttribute('data') || frame.getAttribute('data-attachment') || ''; if (raw) { try { payload = JSON.parse(raw); } catch (_) { try { payload = JSON.parse(decodeURIComponent(raw)); } catch (_) { payload = {}; } } } const parseNestedObject = (value) => { if (value && typeof value === 'object') return value; if (typeof value !== 'string' || !value.trim()) return {}; try { return JSON.parse(value); } catch (_) { return {}; } }; const nested = [payload, ...['property', 'defaults', 'attachment', 'params', 'data'] .map((key) => parseNestedObject(payload[key]))]; const value = (keys) => { for (const object of nested) { const found = keys.map((key) => object?.[key]) .find((item) => item !== null && item !== undefined && item !== ''); if (found !== undefined) return found; } return keys.map((key) => frame.getAttribute(`data-${key}`)) .find((item) => item !== null && item !== undefined) ?? ''; }; const status = compactText(value(['status', 'jobstatus', 'job-status'])); const passedValue = value(['passed', 'pass', 'isPassed', 'ispassed']); const payloadTitle = cleanTaskDisplayName(value([ 'name', 'title', 'filename', 'fileName', 'videoName', 'videoTitle', 'objectName', 'displayName' ])); return correlateAttachmentDescriptor({ source: raw ? (frame === frames[0] ? 'frame-data' : 'ancestor-frame-data') : 'frame-attributes', jobId: compactText(value(['jobid', 'jobId', 'job-id'])).slice(0, 160), module: compactText(value(['module', 'type'])).slice(0, 80), title: payloadTitle || mediaTaskTitleFromDocument(), status: status.slice(0, 80), passed: passedValue === true || passedValue === 1 || /^(true|1|passed|completed|finish(?:ed)?)$/i.test(compactText(passedValue || status)) }); } function attachmentProperty(attachment) { if (!attachment || typeof attachment !== 'object') return {}; if (attachment.property && typeof attachment.property === 'object') return attachment.property; if (typeof attachment.property === 'string') { try { return JSON.parse(attachment.property); } catch (_) { return {}; } } return {}; } function attachmentDescriptor(attachment) { const property = attachmentProperty(attachment); const jobId = compactText(attachment?.jobid || attachment?._jobid || property._jobid || property.jobid).slice(0, 160); const mid = compactText(attachment?.mid || property.mid).slice(0, 160); const objectId = compactText(attachment?.objectid || attachment?.objectId || property.objectid || property.objectId).slice(0, 160); const title = cleanTaskDisplayName( property.name || property.title || property.filename || property.fileName || attachment?.name || attachment?.title ); const module = compactText(attachment?.module || property.module || property.type).slice(0, 80); const passedRaw = attachment?.isPassed ?? attachment?.passed ?? property.isPassed ?? property.passed; const jobRaw = attachment?.job ?? property.job; const passed = passedRaw === true || passedRaw === 1 || /^(true|1|passed|completed|finish(?:ed)?)$/i.test(compactText(passedRaw)); const job = jobRaw === true || jobRaw === 1 || /^(true|1|job)$/i.test(compactText(jobRaw)); return { jobId, mid, objectId, title, module, job, passed, identity: mid ? `mid:${mid}` : objectId ? `object:${objectId}` : jobId ? `job:${jobId}` : '' }; } function pageAttachmentDescriptors() { const candidates = []; const add = (value) => { if (!Array.isArray(value)) return; value.forEach((item) => { const descriptor = attachmentDescriptor(item); if (descriptor.identity && !candidates.some((existing) => existing.identity === descriptor.identity)) candidates.push(descriptor); }); }; try { add(PAGE_WINDOW.attachments); } catch (_) { /* Ignore inaccessible attachment registry. */ } try { add(PAGE_WINDOW.parent?.attachments); } catch (_) { /* Ignore cross-origin parent. */ } try { add(PAGE_WINDOW.top?.attachments); } catch (_) { /* Ignore cross-origin top window. */ } return candidates; } function correlateAttachmentDescriptor(descriptor = {}) { const registry = pageAttachmentDescriptors(); const match = registry.find((item) => descriptor.jobId && item.jobId === descriptor.jobId) || registry.find((item) => descriptor.mid && item.mid === descriptor.mid) || registry.find((item) => descriptor.objectId && item.objectId === descriptor.objectId); const merged = { ...descriptor, jobId: match?.jobId || descriptor.jobId || '', mid: match?.mid || descriptor.mid || '', objectId: match?.objectId || descriptor.objectId || '', title: match?.title || descriptor.title || '', module: descriptor.module || match?.module || '', job: match ? match.job : Boolean(descriptor.job), passed: Boolean(descriptor.passed || match?.passed), registryMatched: Boolean(match) }; merged.identity = merged.mid ? `mid:${merged.mid}` : merged.objectId ? `object:${merged.objectId}` : merged.jobId ? `job:${merged.jobId}` : ''; return merged; } function uniqueWorkersByAttachment(workers) { const result = []; const identities = new Map(); [...workers].sort((left, right) => compareFramePaths(left.framePath, right.framePath)).forEach((worker) => { worker.attachment = correlateAttachmentDescriptor(worker.attachment || {}); const identity = worker.attachment.identity; if (!identity) { result.push(worker); return; } const existingIndex = identities.get(identity); if (existingIndex === undefined) { identities.set(identity, result.length); result.push(worker); return; } const existing = result[existingIndex]; const existingScore = Number(existing.media?.total || 0) + Number(existing.questions?.total || 0); const workerScore = Number(worker.media?.total || 0) + Number(worker.questions?.total || 0); if (workerScore > existingScore) result[existingIndex] = worker; }); return result; } function emptyWorkerStatus() { return { id: WORKER_ID, url: location.href, framePath: FRAME_PATH, attachment: frameAttachmentDescriptor(), discovery: discoveryStatus(), at: Date.now(), media: { total: 0, completed: 0, items: [] }, questions: { total: 0, completed: 0, pending: 0, stages: { discovered: 0, matched: 0, filled: 0, submitted: 0, confirmed: 0 }, items: [] }, auxiliary: emptyAuxiliaryStatus(), hasWork: false, busy: false }; } function buildWorkerStatus() { const results = runTaskHandlers(); const media = results.media; const questions = results.questions; const auxiliary = combineAuxiliaryStatuses(results); recordAdapterSuccess(media.total, questions.total); const hasWork = media.total > 0 || questions.total > 0 || auxiliary.total > 0; const issue = auxiliary.issue || classifyPageIssue(media); return { id: WORKER_ID, url: location.href, framePath: FRAME_PATH, attachment: frameAttachmentDescriptor(), discovery: discoveryStatus(), at: Date.now(), media, questions, auxiliary, issue, hasWork, busy: media.completed < media.total || questions.pending > 0 || auxiliary.pending > 0 }; } function classifyPageIssue(mediaStatus) { const text = compactText(document.body?.innerText || ''); const mediaError = Array.from(document.querySelectorAll('video, audio')).find((item) => item.error)?.error; const visible = (selectors) => Boolean(firstVisible(selectors)); if (visible(['#fcqrimg[src]', '.chapterVideoFaceMaskDiv', '.face-recognition', '[class*="faceMask"]']) || /人脸识别|人脸验证/.test(text)) { return { type: 'face', label: '需要人脸验证', advice: '完成人脸验证后继续队列', blocking: true }; } const videoQuiz = firstVisible(['.ans-videoquiz', '.ans-videoquiz-opt']) || firstVisible(['#videoquiz-submit'])?.closest('.ans-videoquiz'); if ((videoQuiz && !state.answeredQuestions.has(videoQuiz)) || (!videoQuiz && /视频内题目|视频弹题/.test(text))) { return { type: 'video-quiz', label: '检测到视频内题目', advice: '完成题目后继续播放', blocking: true }; } if (visible(['.posCatalog_active .chapter-lock', '#prev_tab .prev_ul .curr .flag']) || /闯关模式/.test(text)) { return { type: 'breaking', label: '课程处于闯关模式', advice: '确认当前章节任务后再继续', blocking: true }; } if (/登录|重新登录|login|会话.*失效/i.test(text) && /登录|login/i.test(location.href + text)) { return { type: 'login', label: '登录状态失效', advice: '重新登录后继续队列' }; } if (/验证码|滑块验证|安全验证|captcha/i.test(text)) { return { type: 'captcha', label: '需要人工验证', advice: '完成页面验证后继续队列' }; } if (/试看|仅可试看|购买后观看|无权观看/i.test(text)) { return { type: 'preview', label: '播放权限受限', advice: '检查课程权限或联系课程管理员' }; } if (mediaError) { const labels = { 1: '播放被中止', 2: '媒体网络错误', 3: '媒体解码错误', 4: '媒体格式不支持' }; return { type: 'player', label: labels[mediaError.code] || '播放器错误', advice: '刷新播放器或切换清晰度后继续' }; } if (Number(mediaStatus?.failed || 0) > 0) { return { type: 'loading', label: mediaStatus.failureReason || '媒体加载超时', advice: '检查网络后继续队列' }; } if (/网络异常|加载失败|network error|请求超时/i.test(text)) { return { type: 'network', label: '页面网络异常', advice: '检查网络并刷新当前任务页' }; } return null; } function handleBlockingIssue(summary) { const issue = summary?.issue; if (!isController() || !issue?.blocking || state.intervention?.type === issue.type) return false; state.intervention = { ...issue, at: Date.now(), taskKey: state.currentTaskKey || taskKey() }; const todo = currentRunningTodo(); if (todo) { todo.stage = 'waiting-intervention'; todo.error = issue.label; todo.updatedAt = Date.now(); saveTodos(); } pauseQueueForError(`${issue.label}:${issue.advice}`); return true; } function selectorSelfCheck(status) { const relevant = /studentstudy|chapterstudy|knowledge/i.test(location.pathname) && /课程|任务|视频|音频|题目/.test(compactText(document.body?.innerText || '')); if (status.discoveryPending) { state.selectorMisses = 0; if (state.lastError === '页面模板待适配') state.lastError = ''; return; } if (!relevant || status.hasWork || currentTaskMarkedComplete()) { state.selectorMisses = 0; if (state.lastError === '页面模板待适配') state.lastError = ''; return; } if (Date.now() - state.taskEnteredAt < CONFIG.selectorGraceTime) return; state.selectorMisses += 1; if (state.selectorMisses >= 3) state.lastError = '页面模板待适配'; } function publishWorkerStatus(status) { if (isController()) { if (Date.now() >= state.ignoreWorkerUntil) state.workers.set(WORKER_ID, status); return; } window.parent.postMessage({ type: MESSAGE_TYPE, kind: 'status', status, hops: 0 }, '*'); } function compareFramePaths(left = [], right = []) { const length = Math.max(left.length, right.length); for (let index = 0; index < length; index += 1) { const a = left[index] ?? -1; const b = right[index] ?? -1; if (a !== b) return a - b; } return 0; } function selectMediaPermit() { if (!isController()) return; const workers = uniqueWorkersByAttachment(Array.from(state.workers.values())) .sort((a, b) => compareFramePaths(a.framePath, b.framePath)); const candidates = []; let hasFailure = false; workers.forEach((worker) => { (worker.media.items || []).forEach((item) => { if (item.failed) hasFailure = true; else if (!item.completed) candidates.push({ workerId: worker.id, index: item.index }); }); }); const selected = !hasFailure && candidates[0] ? candidates[0] : { workerId: '', index: -1 }; if (selected.workerId === state.mediaPermit.workerId && selected.index === state.mediaPermit.index) return; state.mediaPermitRevision += 1; state.mediaPermit = { workerId: selected.workerId, index: selected.index, revision: state.mediaPermitRevision }; enforceMediaPermit(state.mediaPermit); broadcastToChildren({ type: MESSAGE_TYPE, kind: 'media-permit', permit: state.mediaPermit }); } function enforceMediaPermit(permit = state.mediaPermit) { const visibleMedia = Array.from(document.querySelectorAll('video, audio')).filter(isVisible); const target = permit.workerId === WORKER_ID ? visibleMedia[permit.index] : null; document.querySelectorAll('video, audio').forEach((media) => { if (media === target || media.paused) return; pauseMediaElement(media); }); } function revokeMediaPermit() { if (!state.mediaPermit.workerId && state.mediaPermit.index === -1) return; state.mediaPermitRevision += 1; state.mediaPermit = { workerId: '', index: -1, revision: state.mediaPermitRevision }; enforceMediaPermit(state.mediaPermit); broadcastToChildren({ type: MESSAGE_TYPE, kind: 'media-permit', permit: state.mediaPermit }); } function isScanBlockingStatus(status) { return ['requested', 'scanning', 'paused', 'scan_error'].includes(status); } function disableLegacyScannerState() { if (!isScanBlockingStatus(state.scanSession.status)) return; state.scanSession = { status: 'manual', context: state.scanSession.context || null, sourceUrl: state.scanSession.sourceUrl || '', scannerRemovedAt: Date.now() }; saveScanSession(); revokeMediaPermit(); } function activeWorkerSummary() { const cutoff = Date.now() - CONFIG.workerTtl; for (const [id, worker] of state.workers.entries()) { if (worker.at < cutoff) state.workers.delete(id); } const workers = uniqueWorkersByAttachment(Array.from(state.workers.values())); const summary = workers.reduce((result, worker) => { result.mediaTotal += worker.media.total; result.mediaCompleted += worker.media.completed; result.mediaFailed += Number(worker.media.failed || 0); if (!result.mediaFailureReason && worker.media.failureReason) { result.mediaFailureReason = worker.media.failureReason; } result.questionTotal += worker.questions.total; result.questionCompleted += worker.questions.completed; result.questionPending += worker.questions.pending; result.auxiliaryTotal += Number(worker.auxiliary?.total || 0); result.auxiliaryCompleted += Number(worker.auxiliary?.completed || 0); result.auxiliaryPending += Number(worker.auxiliary?.pending || 0); if (!result.issue && worker.issue) result.issue = worker.issue; result.hasWork = result.hasWork || worker.hasWork; result.busy = result.busy || worker.busy; return result; }, { mediaTotal: 0, mediaCompleted: 0, mediaFailed: 0, mediaFailureReason: '', questionTotal: 0, questionCompleted: 0, questionPending: 0, auxiliaryTotal: 0, auxiliaryCompleted: 0, auxiliaryPending: 0, issue: null, hasWork: false, busy: false }); const discoveredIdentities = new Set(workers.flatMap((worker) => worker.discovery?.identities || [])); summary.discoveryExpected = Math.max(0, ...workers.map((worker) => Number(worker.discovery?.expected || 0))); summary.discoverySeen = discoveredIdentities.size; summary.discoveryStableForMs = workers.length ? Math.min(...workers.map((worker) => Number(worker.discovery?.stableForMs || 0))) : 0; summary.discoveryPending = summary.discoveryExpected > summary.discoverySeen && summary.discoveryStableForMs < CONFIG.selectorGraceTime; if (summary.hasWork) state.seenWork = true; syncRunningTodoDisplayName(workers); return summary; } function handleMediaFailure(summary) { if (!isController() || Number(summary.mediaFailed || 0) < 1) return false; const reason = summary.mediaFailureReason || '媒体播放长时间没有进度'; const key = `${state.currentTaskKey}|${reason}`; if (state.lastMediaFailureKey === key && state.queuePaused) return true; state.lastMediaFailureKey = key; pauseQueueForError(reason); updatePanel(summary); return true; } function pauseQueueForError(reason) { invalidateOperations(); cancelTaskHandlers('queue-error'); state.queuePaused = true; state.enabled = false; storageApi.set(STORE.queuePaused, true); storageApi.set(STORE.enabled, false); revokeMediaPermit(); pauseLocalMedia(); state.lastError = reason; const todo = currentRunningTodo(); if (todo && todo.stage !== 'waiting-intervention') { todo.stage = 'failed'; todo.error = reason; todo.updatedAt = Date.now(); saveTodos(); } recordExecutionEvent('paused', { reason }); pushLog(`待办已暂停:${reason}`, true); } function hasCompletionMarker(node) { if (!node) return false; const className = node.className || ''; const title = node.getAttribute?.('title') || ''; if (/unfinished|unfinish/i.test(className) || /未完成/.test(title)) return false; if (CONFIG.selectors.incomplete.some((selector) => node.querySelector?.(selector))) return false; if (/completed|(^|[-_\s])finished($|[-_\s])|jobfinish|icon_completed/i.test(className)) return true; if (/已完成/.test(title)) return true; return CONFIG.selectors.completed.some((selector) => Boolean(node.querySelector?.(selector))); } function hasIncompleteMarker(node) { if (!node) return false; if (/unfinished|unfinish/i.test(node.className || '')) return true; if (/未完成/.test(node.getAttribute?.('title') || '')) return true; return CONFIG.selectors.incomplete.some((selector) => Boolean(node.querySelector?.(selector))); } function catalogEntries() { return activeCourseTemplate().catalog().filter((entry) => isVisible(entry.node)).map((entry) => entry.node); } function simpleHash(value) { let hash = 2166136261; const text = String(value || ''); for (let index = 0; index < text.length; index += 1) { hash ^= text.charCodeAt(index); hash = Math.imul(hash, 16777619); } return (hash >>> 0).toString(36); } function currentAcademicYear() { const now = new Date(); const year = now.getFullYear(); const start = now.getMonth() >= 7 ? year : year - 1; return { start, end: start + 1 }; } function currentTermNumber() { const month = new Date().getMonth(); return month >= 7 || month <= 0 ? 1 : 2; } function currentTermKey() { const year = currentAcademicYear(); return `${year.start}-${year.end}-${currentTermNumber()}`; } function detectAccountKey() { try { const url = new URL(location.href); for (const key of ['userId', 'userid', 'uid', 'puid', 'pUid']) { const value = compactText(url.searchParams.get(key)); if (value) return value.slice(0, 120); } } catch (_) { // Continue with DOM account hints. } const node = document.querySelector( '[data-userid], [data-user-id], [data-uid], input[name="userId"], input[name="uid"], meta[name="user-id"]' ); return compactText( node?.getAttribute?.('data-userid') || node?.getAttribute?.('data-user-id') || node?.getAttribute?.('data-uid') || node?.getAttribute?.('value') || node?.getAttribute?.('content') ).slice(0, 120); } function scanPlatformKey(hostname = location.hostname) { const host = normalize(hostname); if (host.includes('xueyinonline')) return 'xueyinonline'; if (host.includes('chaoxing')) return 'chaoxing'; return host; } function activeCourseTemplate() { if (scanPlatformKey() === 'xueyinonline') return PLATFORM_ADAPTERS.xueyin; if (FRAME_PATH.length > 0) return PLATFORM_ADAPTERS['portal-iframe']; for (const id of ['chaoxing-new', 'chaoxing-legacy']) { const adapter = PLATFORM_ADAPTERS[id]; if (adapter.detect()) return adapter; } return PLATFORM_ADAPTERS.generic; } function templateSelectors(kind) { return Array.from(new Set(activeCourseTemplate().selectors[kind] || [])); } function templateNodes(kind) { const adapter = activeCourseTemplate(); const primary = queryAllInOrder(adapter.selectors[kind] || []); if (primary.length || adapter.id === 'generic') return primary; return queryAllInOrder(COURSE_TEMPLATES.generic[kind] || []); } function adapterDomSignature(adapter = activeCourseTemplate()) { const nodes = ['catalog', 'courseLinks'].flatMap((kind) => queryAllInOrder(adapter.selectors[kind] || []).slice(0, 8)); return simpleHash(nodes.map((node) => [ node.tagName, node.id, String(node.className || '').slice(0, 120), node.getAttribute?.('data-status') || '' ].join('|')).join('||')); } function recordAdapterSuccess(mediaCount = 0, questionCount = 0) { const adapter = activeCourseTemplate(); const catalogCount = adapter.catalog().length; if (mediaCount + questionCount + catalogCount < 1) return; const previous = state.adapterHealth[adapter.id] || {}; const signature = adapterDomSignature(adapter); const now = Date.now(); if (previous.domSignature === signature && now - Number(previous.lastSuccessAt || 0) < 60000) return; state.adapterHealth[adapter.id] = { apiVersion: adapter.apiVersion, domSignature: signature, lastSuccessAt: now, counts: { catalog: catalogCount, media: mediaCount, questions: questionCount } }; saveObject(STORE.adapterHealth, state.adapterHealth); } function isCourseLink(link) { const rawHref = link?.getAttribute?.('href') || ''; if (!rawHref || /^javascript:/i.test(rawHref)) return false; try { const url = new URL(rawHref, location.href); const hasIdentity = url.searchParams.has('courseId') || url.searchParams.has('courseid') || url.searchParams.has('clazzid') || url.searchParams.has('clazzId'); const knownPath = /\/mycourse\/|studentcourse|studentstudy|coursehome/i.test(url.pathname); return hasIdentity && knownPath; } catch (_) { return false; } } function courseListFingerprint(courses) { return simpleHash((Array.isArray(courses) ? courses : []) .map((course) => `${course.id}|${compactText(course.name)}`) .join('||')); } function buildScanContext(courses) { return { accountKey: detectAccountKey(), termKey: currentTermKey(), platformKey: scanPlatformKey(), courseFingerprint: courseListFingerprint(courses) }; } function scanContextKey(context) { if (!context || typeof context !== 'object') return ''; return simpleHash([ context.accountKey || '', context.termKey || '', context.platformKey || '', context.courseFingerprint || '' ].join('|')); } function scanContextMismatch(context, currentCourses = null) { if (!context || typeof context !== 'object') return ''; if (context.termKey && context.termKey !== currentTermKey()) return '学期已变化'; if (context.platformKey && context.platformKey !== scanPlatformKey()) return '课程平台已变化'; const accountKey = detectAccountKey(); if (context.accountKey && accountKey && context.accountKey !== accountKey) return '登录账号已变化'; if (Array.isArray(currentCourses) && context.courseFingerprint && context.courseFingerprint !== courseListFingerprint(currentCourses)) return '课程列表已变化'; return ''; } function courseIdentity(urlValue) { try { const url = new URL(urlValue, location.href); const courseId = url.searchParams.get('courseId') || url.searchParams.get('courseid') || ''; const clazzId = url.searchParams.get('clazzid') || url.searchParams.get('clazzId') || ''; return courseId || clazzId ? `${courseId}:${clazzId}` : `${url.origin}${url.pathname}`; } catch (_) { return normalize(urlValue); } } function isCurrentSemesterCourse(cardText) { const text = compactText(cardText); if (/已结课|已结束|课程结束|已归档|历史课程|已过期/.test(text)) return false; const year = currentAcademicYear(); const ranges = Array.from(text.matchAll(/(20\d{2})\s*[-—至/]\s*(20\d{2})/g)); if (ranges.length && !ranges.some((match) => Number(match[1]) === year.start && Number(match[2]) === year.end)) { return false; } const firstTerm = /第\s*[一1]\s*学期|秋季|秋学期/.test(text); const secondTerm = /第\s*[二2]\s*学期|春季|春学期/.test(text); if (firstTerm !== secondTerm) { if (firstTerm && currentTermNumber() !== 1) return false; if (secondTerm && currentTermNumber() !== 2) return false; } return true; } function courseNameFromCard(card, link) { const titleNode = card.querySelector( '.course-name, .courseName, .course_name, .name, .Mconright h3, h3, h4, [title]' ); return compactText( titleNode?.getAttribute?.('title') || titleNode?.textContent || link.getAttribute('title') || link.textContent ).slice(0, 160); } function discoverCurrentSemesterCourses() { const links = templateNodes('courseLinks') .filter((link) => isVisible(link) && isCourseLink(link) && !link.closest('#cx-helper-panel, #cx-helper-progress-window, #cx-helper-todo-window')); const courses = []; const seen = new Set(); links.forEach((link) => { const rawHref = link.getAttribute('href') || ''; if (!rawHref || /^javascript:/i.test(rawHref)) return; let url; try { url = new URL(rawHref, location.href).href; } catch (_) { return; } const card = link.closest(templateSelectors('courseCards').join(',')) || link; const cardText = compactText(card.textContent); if (!isCurrentSemesterCourse(cardText)) return; const name = courseNameFromCard(card, link); if (!name) return; const id = courseIdentity(url); if (!id || seen.has(id)) return; seen.add(id); courses.push({ id, name, url, order: courses.length }); }); return courses; } function isCourseHomePage() { const path = location.pathname.toLowerCase(); if (/studentstudy|studentcourse|\/mycourse\/tch|knowledge|chapterstudy/.test(path)) return false; return discoverCurrentSemesterCourses().length > 0; } function scanCatalogEntries() { const candidates = templateNodes('catalog'); const entries = []; candidates.forEach((node) => { const clickable = node.closest('a[href]') || node.querySelector?.('a[href]') || node; const title = compactText(clickable.textContent || node.textContent); if (!title || entries.some((entry) => entry.node === clickable)) return; const href = clickable.getAttribute?.('href') || ''; let url = ''; if (href && !/^javascript:/i.test(href)) { try { url = new URL(href, location.href).href; } catch (_) { url = ''; } } entries.push({ node: clickable, title, url, order: entries.length }); }); return entries; } function inferTaskType(entry) { const text = normalize(`${entry.title} ${entry.node.className || ''} ${entry.url}`); if (/视频|video/.test(text)) return '视频'; if (/音频|audio/.test(text)) return '音频'; if (/测验|作业|考试|quiz|work/.test(text)) return '题目'; if (/文档|阅读|ppt|pdf|document/.test(text)) return '文档'; return '任务点'; } function createTaskDescriptor(course, entry, index, contextKey) { return { schemaVersion: 3, id: `todo-${simpleHash(`${course.id}|${entry.url}|${entry.title}`)}`, courseId: course.id, courseName: course.name, courseOrder: course.order, taskOrder: index, title: entry.title.slice(0, 180), type: inferTaskType(entry), stage: 'detected', deadline: detectDeadline(entry.node?.textContent || ''), url: entry.url || course.url, status: 'pending', source: '目录标记', sourceRevision: simpleHash(`${entry.url}|${entry.title}|${entry.node?.className || ''}`), scannedAt: Date.now(), completedAt: 0, contextKey }; } function extractPendingTodos(course, catalog = scanCatalogEntries()) { const contextKey = scanContextKey(state.scanSession.context); return catalog .filter((entry) => hasIncompleteMarker(entry.node) || !hasCompletionMarker(entry.node)) .filter((entry) => entry.url || hasIncompleteMarker(entry.node)) .map((entry, index) => createTaskDescriptor(course, entry, index, contextKey)); } function saveTodos() { const scopes = loadObject(STORE.todoScopes); scopes[todoScopeKey()] = state.todos; saveObject(STORE.todoScopes, scopes); storageApi.set(STORE.todos, JSON.stringify(state.todos)); } function detectDeadline(value = document.body?.innerText || '') { const text = compactText(value); const match = text.match(/(?:截止(?:时间)?|deadline)\s*[::]?\s*(20\d{2}[年\-/.]\d{1,2}[月\-/.]\d{1,2}(?:日)?(?:\s+\d{1,2}:\d{2})?)/i); if (!match) return ''; return match[1].replace(/[年/.]/g, '-').replace('月', '-').replace('日', '').trim(); } function syncRunningTodoDeadline() { const todo = currentRunningTodo(); if (!todo || todo.deadline) return; const deadline = detectDeadline(); if (!deadline) return; todo.deadline = deadline; saveTodos(); } function saveScanSession() { storageApi.set(STORE.scanSession, JSON.stringify(state.scanSession)); } function currentRunningTodo() { return state.todos.find((todo) => todo.status === 'running') || null; } function nextPendingTodo() { return state.todos .filter((todo) => todo.status === 'pending') .sort((a, b) => a.courseOrder - b.courseOrder || a.taskOrder - b.taskOrder)[0] || null; } function todoMatchesLocation(todo) { if (!todo?.url) return false; try { const target = new URL(todo.url, location.href); const current = new URL(location.href); if (target.href === current.href) return true; const targetCourse = target.searchParams.get('courseId') || target.searchParams.get('courseid'); const currentCourse = current.searchParams.get('courseId') || current.searchParams.get('courseid'); const targetChapter = target.searchParams.get('chapterId') || target.searchParams.get('knowledgeId'); const currentChapter = current.searchParams.get('chapterId') || current.searchParams.get('knowledgeId'); if (targetCourse && currentCourse && targetCourse === currentCourse && targetChapter && currentChapter && targetChapter === currentChapter) { return true; } } catch (_) { // Fall through to title matching for onclick-only task entries. } const currentTitle = normalize(currentTaskTitle()); const todoTitle = normalize(todo.title); return Boolean(todoTitle && currentTitle && (currentTitle.includes(todoTitle) || todoTitle.includes(currentTitle))); } function invalidateTodoContext(reason) { state.todos = []; saveTodos(); state.scanSession = { ...state.scanSession, status: 'context_changed', contextChangedReason: reason, contextChangedAt: Date.now() }; saveScanSession(); pauseQueueForError(reason); pushLog(`待办已清空:${reason}`, true); } function validateTodoContext() { if (!state.todos.length) return true; const context = state.scanSession.context; if (!context) { invalidateTodoContext('旧待办缺少账号和学期信息,请重新扫描'); return false; } const homeCourses = isCourseHomePage() ? discoverCurrentSemesterCourses() : null; const reason = scanContextMismatch(context, homeCourses); const expectedKey = scanContextKey(context); const storedMismatch = state.todos.some((todo) => todo.contextKey && todo.contextKey !== expectedKey); if (reason || storedMismatch) { invalidateTodoContext(reason || '待办所属账号或学期与当前环境不一致'); return false; } if (state.todos.some((todo) => !todo.contextKey)) { state.todos = state.todos.map((todo) => ({ ...todo, contextKey: expectedKey })); saveTodos(); } return true; } function navigateForQueue(todo) { if (!todo?.url || Date.now() - state.queueNavigationAt < 2000) return false; state.queueNavigationAt = Date.now(); pushLog(`进入待办:${todo.courseName} / ${todo.title}`); scheduleOperation(() => { const running = currentRunningTodo(); if (!state.enabled || state.queuePaused || running?.id !== todo.id) return; location.assign(todo.url); }, 150); return true; } function activateNextTodo() { if (state.queuePaused || !state.enabled) return null; let todo = currentRunningTodo(); if (!todo) { todo = nextPendingTodo(); if (todo) { todo.status = 'running'; todo.stage = 'running'; todo.startedAt = Date.now(); saveTodos(); recordExecutionEvent('started'); } } if (!todo) { if (state.todos.length && !state.queueFinishedLogged) { state.queueFinishedLogged = true; pushLog('本地待办队列已全部完成', true); } return null; } state.queueFinishedLogged = false; if (!todoMatchesLocation(todo)) navigateForQueue(todo); return todo; } function markRunningTodoComplete(completionSource = '服务端确认') { const todo = currentRunningTodo(); if (!todo) return false; todo.status = 'completed'; todo.stage = 'completed'; todo.completedAt = Date.now(); todo.completionSource = completionSource; const summary = activeWorkerSummary(); todo.completionSummary = { durationMs: todo.startedAt ? Math.max(0, todo.completedAt - todo.startedAt) : 0, media: { completed: summary.mediaCompleted, total: summary.mediaTotal }, questions: { completed: summary.questionCompleted, total: summary.questionTotal }, auxiliary: { completed: summary.auxiliaryCompleted, total: summary.auxiliaryTotal } }; saveTodos(); recordExecutionEvent('completed', { todoId: todo.id, title: todo.title, stage: todo.stage, attachmentId: todo.attachmentId, reason: completionSource }); pushLog(`完成待办:${todo.courseName} / ${todo.title}`, true); state.queueNavigationAt = 0; state.stableSince = 0; state.completionAwaitStartedAt = 0; state.completionAwaitTaskKey = ''; if (/平台尚未确认任务完成/.test(state.lastError)) state.lastError = ''; return true; } function completeRunningTodoAndAdvance(completionSource) { if (!markRunningTodoComplete(completionSource)) return false; const nextTodo = activateNextTodo(); if (!nextTodo) goToNextTask(); return true; } function processTodoQueue() { if (!isController()) return null; if (!validateTodoContext()) return null; if (state.queuePaused || !state.enabled) { pauseLocalMedia(); return currentRunningTodo(); } return activateNextTodo(); } function currentCatalogEntry(entries) { const current = firstVisible(CONFIG.selectors.currentCatalog); if (!current) return null; return entries.find((entry) => entry === current || entry.contains(current) || current.contains(entry)) || current; } function currentTaskCompletionEvidence(todo = null) { const current = firstVisible(CONFIG.selectors.currentCatalog); let catalogComplete = Boolean(current && activeCourseTemplate().completion(current)); const catalogIncomplete = Boolean(current && hasIncompleteMarker(current)); const statusNode = firstVisible([ '.ans-job-finished', '.jobFinish', '.job-finished', '[data-status="completed"]', '[data-job-status="completed"]', '.courseTaskStatus', '.task-status-completed' ]); const bannerComplete = Boolean(statusNode) || Array.from(document.querySelectorAll('body *')).some((node) => node.children.length === 0 && isVisible(node) && /^(任务点|任务)已完成$/.test(compactText(node.textContent)) ); if (todo && !catalogComplete) { const titles = [todo.title, todo.originalTitle].map(normalize).filter(Boolean); const targetUrl = todo.url || ''; const entry = scanCatalogEntries().find((candidate) => (targetUrl && candidate.url === targetUrl) || titles.some((title) => normalize(candidate.title) === title) ); catalogComplete = Boolean(entry && activeCourseTemplate().completion(entry.node)); } const workers = uniqueWorkersByAttachment(Array.from(state.workers.values())); const attachments = workers.map((worker) => correlateAttachmentDescriptor(worker.attachment || {})) .filter((item) => item.identity && (item.job || item.passed)); const scoped = todo?.attachmentId ? attachments.filter((item) => item.identity === todo.attachmentId) : attachments; const attachmentComplete = scoped.length > 0 && scoped.every((item) => item.passed) && Date.now() - state.taskEnteredAt >= CONFIG.taskWarmupTime; const sources = []; if (attachmentComplete) sources.push('附件 isPassed'); if (catalogComplete) sources.push('目录标记'); if (bannerComplete) sources.push('页面提示'); const conflict = attachmentComplete && catalogIncomplete ? '附件已完成,但当前目录仍显示未完成' : ''; return { complete: attachmentComplete || catalogComplete || bannerComplete, source: sources.join(' + ') || '', conflict, attachmentComplete, catalogComplete, bannerComplete }; } function currentTaskMarkedComplete() { return currentTaskCompletionEvidence().complete; } function currentTodoMarkedComplete(todo) { if (!todo || !todoMatchesLocation(todo)) return false; return currentTaskCompletionEvidence(todo).complete; } function completeTodoFromEvidence(todo, fallbackSource) { const evidence = currentTaskCompletionEvidence(todo); if (!evidence.complete) return false; if (evidence.conflict && todo) { todo.completionConflict = evidence.conflict; todo.completionEvidence = { attachment: evidence.attachmentComplete, catalog: evidence.catalogComplete, banner: evidence.bannerComplete }; saveTodos(); pushLog(`完成状态冲突:${evidence.conflict}`, true, 'error'); } return completeRunningTodoAndAdvance(evidence.source || fallbackSource); } function taskKey() { const current = firstVisible(CONFIG.selectors.currentCatalog); if (!current) return ''; const clickable = current.closest('a[href]') || current.querySelector('a[href]') || current; return clickable.getAttribute?.('href') || clickable.getAttribute?.('data-id') || normalize(clickable.textContent); } function resetTaskProgress(ignoreMilliseconds = 0) { invalidateOperations(); cancelTaskHandlers('task-reset'); state.workers.clear(); state.mediaPermitRevision += 1; state.mediaPermit = { workerId: '', index: -1, revision: state.mediaPermitRevision }; enforceMediaPermit(state.mediaPermit); if (isController()) { broadcastToChildren({ type: MESSAGE_TYPE, kind: 'media-permit', permit: state.mediaPermit }); } state.seenWork = false; state.stableSince = 0; state.completionAwaitStartedAt = 0; state.completionAwaitTaskKey = ''; state.pageSubmitted = false; state.answeredQuestions = new WeakSet(); state.submittedQuestions = new WeakSet(); state.media = new WeakMap(); state.ignoreWorkerUntil = Date.now() + ignoreMilliseconds; state.taskEnteredAt = Date.now(); state.finalState = null; state.intervention = null; } function syncTaskContext() { const key = taskKey(); if (!key) return; if (!state.currentTaskKey) { state.currentTaskKey = key; return; } if (key !== state.currentTaskKey) { state.currentTaskKey = key; resetTaskProgress(500); pushLog(`已切换任务点:${normalize(key).slice(0, 30)}`); } } function isCourseDetailPage() { const path = location.pathname.toLowerCase(); if (/studentstudy|knowledge|chapterstudy/.test(path)) return false; if (/\/mycourse\/(studentcourse|tch)|\/course\/(detail|courseinfo)/.test(path)) return true; return Boolean(document.querySelector('.chapter_unit, .catalog_points, .catalog_level')) && catalogEntries().length > 0; } function jumpFromCourseDetail() { if (!isController() || !state.enabled || !state.autoJump || state.detailJumpAttempted) return false; if (!isCourseDetailPage() || Date.now() - state.detailPageEnteredAt < CONFIG.detailPageDelay) return false; const entries = catalogEntries(); const target = entries.find(hasIncompleteMarker) || entries.find((entry) => !hasCompletionMarker(entry)); if (!target) { if (!state.detailExpandAttempted) { const expander = firstVisible([ '#courseChapter .chapter_unit', '#courseChapter .chapterText', '.chapter_item .chapterText', '.catalog_level .chapterText' ]); if (expander) { state.detailExpandAttempted = true; state.detailPageEnteredAt = Date.now(); clickElement(expander); pushLog('已展开课程目录,正在查找任务点'); } } return false; } state.detailJumpAttempted = true; resetTaskProgress(1200); state.currentTaskKey = ''; state.lastNavigation = Date.now(); pushLog(`进入任务点:${normalize(target.textContent).slice(0, 30)}`); const clicked = clickElement(target); if (!clicked) state.detailJumpAttempted = false; return clicked; } function goToNextTask() { if (Date.now() - state.lastNavigation < CONFIG.navigationCooldown) return false; const finalKey = taskKey() || location.href; if (state.finalState?.taskKey === finalKey) return false; if (state.navigationMode === 'manual') { state.finalState = { status: 'manual', taskKey: finalKey, at: Date.now() }; pushLog('当前任务已完成,按设置停留在本页', true, 'navigation'); return false; } const entries = catalogEntries(); const current = currentCatalogEntry(entries); const index = entries.indexOf(current); const following = index >= 0 ? entries.slice(index + 1) : []; const catalogTarget = following.find(hasIncompleteMarker) || following.find((entry) => !hasCompletionMarker(entry)); const next = firstVisible(CONFIG.selectors.next); const target = state.navigationMode === 'incomplete' ? catalogTarget : next && !next.matches(':disabled, [aria-disabled="true"]') ? next : catalogTarget; if (!target) { state.finalState = { status: 'complete', taskKey: finalKey, at: Date.now() }; pushLog('已到达最后一个可执行任务点', true, 'navigation'); return false; } const clicked = clickElement(target); if (!clicked) return false; const usedNextButton = target === next; resetTaskProgress(1200); state.currentTaskKey = ''; state.lastNavigation = Date.now(); if (usedNextButton) { pushLog('进入下一个任务点'); } else { pushLog(`按目录进入:${normalize(target.textContent).slice(0, 30)}`); } return true; } function maybeAdvance(summary) { if (!isController() || !state.enabled || isCourseDetailPage()) return; if (Date.now() - state.taskEnteredAt < CONFIG.taskWarmupTime) return; const completedWork = state.seenWork && !summary.busy && summary.questionPending === 0 && summary.mediaCompleted >= summary.mediaTotal; const ready = completedWork || (!state.seenWork && currentTaskMarkedComplete()); if (!ready) { state.stableSince = 0; state.completionAwaitStartedAt = 0; state.completionAwaitTaskKey = ''; return; } if (!state.stableSince) state.stableSince = Date.now(); if (Date.now() - state.stableSince < CONFIG.completionStableTime) return; const runningTodo = currentRunningTodo(); if (runningTodo) { if (currentTodoMarkedComplete(runningTodo)) { completeTodoFromEvidence(runningTodo, '服务端确认'); return; } if (completedWork) { const confirmationKey = runningTodo.id || runningTodo.url || runningTodo.title; if (state.completionAwaitTaskKey !== confirmationKey) { state.completionAwaitTaskKey = confirmationKey; state.completionAwaitStartedAt = Date.now(); runningTodo.stage = 'waiting-confirm'; runningTodo.updatedAt = Date.now(); saveTodos(); recordExecutionEvent('waiting-confirm'); pushLog('本地处理完成,等待平台确认', true); } if (Date.now() - state.completionAwaitStartedAt >= CONFIG.serverCompletionTimeout) { const reason = '平台尚未确认任务完成'; pauseQueueForError(reason); } return; } } if (goToNextTask()) state.stableSince = 0; } function toggleEnabled() { state.enabled = !state.enabled; state.queuePaused = !state.enabled; resetTaskProgress(state.enabled ? 500 : 0); state.detailJumpAttempted = false; state.detailExpandAttempted = false; state.detailPageEnteredAt = Date.now(); storageApi.set(STORE.enabled, state.enabled); storageApi.set(STORE.queuePaused, state.queuePaused); pushLog(state.enabled ? '已启动自动运行' : '已暂停自动运行', true); updatePanel(); } function importBank() { const raw = prompt('粘贴题库 JSON:{"题目":"答案"},多选题答案使用数组'); if (!raw) return; try { importBankObject(JSON.parse(raw)); } catch (error) { alert(`题库 JSON 解析失败:${error.message}`); } } function importBankObject(parsed) { if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('顶层必须是 JSON 对象'); const envelope = parsed.records && typeof parsed.records === 'object' && !Array.isArray(parsed.records) ? parsed : { schemaVersion: 0, provider: 'legacy-json', records: parsed }; const records = envelope.records; const entries = Object.entries(records); if (!entries.length) throw new Error('题库文件没有记录'); const invalid = entries.filter(([question, answer]) => !normalize(question) || !(typeof answer === 'string' || typeof answer === 'number' || Array.isArray(answer))); if (invalid.length) throw new Error(`有 ${invalid.length} 条记录格式错误`); const conflicts = entries.filter(([question, answer]) => Object.prototype.hasOwnProperty.call(state.bank, question) && JSON.stringify(state.bank[question]) !== JSON.stringify(answer) ).length; state.bank = { ...state.bank, ...records }; entries.forEach(([question]) => { const normalized = normalize(question); Object.keys(state.unanswered).forEach((storedQuestion) => { if (storedQuestion === question || normalize(storedQuestion) === normalized) delete state.unanswered[storedQuestion]; }); }); saveObject(STORE.bank, state.bank); saveObject(STORE.unanswered, state.unanswered); rebuildBankIndex(); const result = { schemaVersion: BANK_SCHEMA_VERSION, sourceSchemaVersion: Number(envelope.schemaVersion || 0), provider: activeBankProvider().id, imported: entries.length, conflicts, total: Object.keys(state.bank).length }; pushLog(`题库导入 ${result.imported} 条,覆盖冲突 ${result.conflicts} 条,现有 ${result.total} 条`, true, 'question'); return result; } async function importBankFile(file) { if (!file) return null; const text = await file.text(); return importBankObject(JSON.parse(text)); } function exportUnanswered() { const payload = JSON.stringify({ schemaVersion: BANK_SCHEMA_VERSION, provider: activeBankProvider().id, exportedAt: new Date().toISOString(), records: state.unanswered }, null, 2); prompt('复制未匹配题目,补充答案后重新导入:', payload); } function exportBankPayload() { return JSON.stringify(activeBankProvider().export(), null, 2); } function settingsPayload() { return { schemaVersion: SETTINGS_SCHEMA_VERSION, exportedAt: new Date().toISOString(), settings: { speed: state.speed, volume: state.volume, autoJump: state.autoJump, navigationMode: state.navigationMode, uiState: state.uiState } }; } function importSettingsObject(payload) { if (!payload || typeof payload !== 'object') throw new Error('设置文件必须是 JSON 对象'); const source = payload.settings && typeof payload.settings === 'object' ? payload.settings : payload; const navigationMode = ['next', 'incomplete', 'manual'].includes(source.navigationMode) ? source.navigationMode : state.navigationMode; state.speed = clampSpeed(source.speed ?? state.speed); state.volume = Math.max(0, Math.min(1, Number(source.volume ?? state.volume) || 0)); state.autoJump = source.autoJump === undefined ? state.autoJump : toBoolean(source.autoJump); state.navigationMode = navigationMode; if (source.uiState && typeof source.uiState === 'object' && !Array.isArray(source.uiState)) state.uiState = source.uiState; storageApi.set(STORE.speed, state.speed); storageApi.set(STORE.volume, state.volume); storageApi.set(STORE.autoJump, state.autoJump); storageApi.set(STORE.navigationMode, state.navigationMode); saveUiState(); updatePanel(); recordExecutionEvent('settings-imported'); return settingsPayload(); } function exportSettings(download = true) { const text = JSON.stringify(settingsPayload(), null, 2); if (download) downloadText('chaoxing-helper-settings.json', text); return text; } async function importSettingsFile(file) { if (!file) return null; return importSettingsObject(JSON.parse(await file.text())); } function diagnosticPayload() { const workers = Array.from(state.workers.values()).map((worker) => ({ framePath: worker.framePath, attachment: redactDiagnosticValue(worker.attachment), media: worker.media, questions: worker.questions, auxiliary: worker.auxiliary, discovery: worker.discovery, issue: redactDiagnosticValue(worker.issue), location: redactDiagnosticText(workerLocation(worker)) })); return { version: SCRIPT_VERSION, at: new Date().toISOString(), platform: scanPlatformKey(), template: activeCourseTemplate().id, enabled: state.enabled, queuePaused: state.queuePaused, schemaVersions: { bank: BANK_SCHEMA_VERSION, todoExport: TODO_EXPORT_SCHEMA_VERSION, settings: SETTINGS_SCHEMA_VERSION, storage: STORAGE_SCHEMA_VERSION }, executionJournal: state.executionJournal.slice(0, 30).map((entry) => redactDiagnosticValue(entry)), bankProvider: activeBankProvider().id, adapterHealth: redactDiagnosticValue(state.adapterHealth), primaryTab: state.tabPrimary, todoCounts: state.todos.reduce((result, todo) => { result[todo.status] = (result[todo.status] || 0) + 1; return result; }, {}), lastError: redactDiagnosticText(state.lastError), workers, logs: (storageApi.get(STORE.logs, []) || []).slice(0, 30).map(redactDiagnosticText) }; } function redactDiagnosticText(value) { return String(value || '') .replace(/https?:\/\/[^\s]+/gi, (raw) => { try { const url = new URL(raw); return `${url.origin}${url.pathname}`; } catch (_) { return '[URL]'; } }) .replace(/([?&](?:token|ticket|uid|userid|account|phone|mobile|session|enc)\s*=)[^\s]+/gi, '$1[REDACTED]') .replace(/\b1[3-9]\d{9}\b/g, '[PHONE]') .replace(/\b(?:\d{15,18}[\dXx]|[A-Za-z0-9_-]{24,})\b/g, '[ID]'); } function redactDiagnosticValue(value) { if (Array.isArray(value)) return value.map(redactDiagnosticValue); if (value && typeof value === 'object') { return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactDiagnosticValue(item)])); } return typeof value === 'string' ? redactDiagnosticText(value) : value; } function diagnosticPreview() { const raw = [state.lastError, ...(storageApi.get(STORE.logs, []) || []).slice(0, 30)].join('\n'); return { logLines: Math.min(30, (storageApi.get(STORE.logs, []) || []).length), workerCount: state.workers.size, todoCount: state.todos.length, sensitiveMatches: { url: (raw.match(/https?:\/\/[^\s]+/gi) || []).length, phone: (raw.match(/\b1[3-9]\d{9}\b/g) || []).length, longId: (raw.match(/\b[A-Za-z0-9_-]{24,}\b/g) || []).length } }; } function exportDiagnostic(download = true) { if (download) { const preview = diagnosticPreview(); const accepted = typeof confirm !== 'function' || confirm( `诊断包包含 ${preview.logLines} 条日志、${preview.workerCount} 个页面状态和 ${preview.todoCount} 条待办统计。\n` + `检测到 URL ${preview.sensitiveMatches.url} 处、手机号 ${preview.sensitiveMatches.phone} 处、长标识 ${preview.sensitiveMatches.longId} 处,导出时会自动隐藏。\n\n继续导出?` ); if (!accepted) return ''; } const text = JSON.stringify(diagnosticPayload(), null, 2); if (download) downloadText('chaoxing-diagnostic.json', text); return text; } function compactText(value) { return String(value || '').replace(/[\r\n\t ]+/g, ' ').trim(); } function detectedMediaTask(workers = Array.from(state.workers.values())) { const ordered = [...workers].sort((left, right) => { const leftActive = left.id === state.mediaPermit.workerId ? 1 : 0; const rightActive = right.id === state.mediaPermit.workerId ? 1 : 0; if (leftActive !== rightActive) return rightActive - leftActive; const leftMedia = Number(left.media?.total || 0) > 0 ? 1 : 0; const rightMedia = Number(right.media?.total || 0) > 0 ? 1 : 0; if (leftMedia !== rightMedia) return rightMedia - leftMedia; return compareFramePaths(left.framePath, right.framePath); }); for (const worker of ordered) { const items = [...(worker.media?.items || [])].sort((left, right) => { const leftActive = worker.id === state.mediaPermit.workerId && left.index === state.mediaPermit.index ? 1 : 0; const rightActive = worker.id === state.mediaPermit.workerId && right.index === state.mediaPermit.index ? 1 : 0; return rightActive - leftActive || left.index - right.index; }); for (const item of items) { const title = cleanTaskDisplayName(item.title); if (title) return { title, type: item.type === 'audio' ? '音频' : '视频', workerId: worker.id, attachment: worker.attachment || {} }; } const title = cleanTaskDisplayName(worker.attachment?.title); const module = normalize(worker.attachment?.module); if (title && /video|audio|media|视频|音频/.test(module)) { return { title, type: /audio|音频/.test(module) ? '音频' : '视频', workerId: worker.id, attachment: worker.attachment || {} }; } } const localTitle = mediaTaskTitleFromDocument(); return localTitle ? { title: localTitle, type: document.querySelector('audio') ? '音频' : '视频', workerId: WORKER_ID, attachment: frameAttachmentDescriptor() } : null; } function syncRunningTodoDisplayName(workers) { if (!isController()) return false; const todo = currentRunningTodo(); const detected = detectedMediaTask(workers); if (!todo || !detected) return false; let changed = false; const titleChanged = detected.title && normalize(todo.title) !== normalize(detected.title); if (titleChanged) { if (!todo.originalTitle) todo.originalTitle = todo.title || ''; todo.title = detected.title; todo.type = detected.type; todo.titleSource = '视频附件'; todo.titleUpdatedAt = Date.now(); changed = true; } const attachment = correlateAttachmentDescriptor(detected.attachment || {}); for (const [key, value] of Object.entries({ attachmentId: attachment.identity, jobId: attachment.jobId, mid: attachment.mid, objectId: attachment.objectId })) { if (value && todo[key] !== value) { todo[key] = value; changed = true; } } if (!changed) return false; saveTodos(); if (titleChanged) pushLog(`识别${detected.type}名称:${detected.title}`, true, 'media'); return true; } function currentTaskTitle() { const mediaTask = detectedMediaTask(); if (mediaTask?.title) return mediaTask.title; const current = firstVisible(CONFIG.selectors.currentCatalog); const text = cleanTaskDisplayName(current?.getAttribute?.('title') || current?.textContent); const todoTitle = cleanTaskDisplayName(currentRunningTodo()?.title); return text || todoTitle || cleanTaskDisplayName(document.title) || '当前课程页面'; } function workerLocation(worker) { try { const url = new URL(worker.url); return `${url.hostname}${url.pathname}`; } catch (_) { return compactText(worker.url) || '页面地址未知'; } } function setMeter(root, completed, total) { const percent = total > 0 ? Math.round((completed / total) * 100) : 0; root.querySelector('.cxh-meter-value').textContent = `${completed}/${total}`; root.querySelector('.cxh-meter-fill').style.width = `${Math.max(0, Math.min(100, percent))}%`; } function courseProgress() { const entries = catalogEntries(); return { total: entries.length, completed: entries.filter(hasCompletionMarker).length }; } function renderProgressWindow(summary) { const root = state.progressWindow; if (!root || !state.progressOpen) return; const course = courseProgress(); root.querySelector('.cxh-current-task').textContent = currentTaskTitle(); const allWorkers = uniqueWorkersByAttachment(Array.from(state.workers.values())); const task = detectedMediaTask(allWorkers); const attachment = correlateAttachmentDescriptor(task?.attachment || allWorkers.find((worker) => worker.attachment?.identity)?.attachment || {}); root.querySelector('.cxh-current-meta').textContent = attachment.identity ? `${task?.type || attachment.module || '任务'} · ${attachment.identity} · ${attachment.registryMatched ? '附件已关联' : '页面识别'}` : ''; setMeter(root.querySelector('[data-meter="tasks"]'), course.completed, course.total); setMeter(root.querySelector('[data-meter="media"]'), summary.mediaCompleted, summary.mediaTotal); setMeter(root.querySelector('[data-meter="questions"]'), summary.questionCompleted, summary.questionTotal); root.querySelector('.cxh-pending-value').textContent = String(summary.questionPending); root.querySelector('.cxh-unanswered-value').textContent = String(Object.keys(state.unanswered).length); root.querySelector('.cxh-auxiliary-value').textContent = `${summary.auxiliaryCompleted}/${summary.auxiliaryTotal}`; const issue = root.querySelector('.cxh-issue'); issue.hidden = !summary.issue; issue.textContent = summary.issue ? `${summary.issue.label}:${summary.issue.advice}` : ''; const activeMedia = Array.from(state.workers.values()) .flatMap((worker) => worker.media.items || []) .find((item) => !item.completed && !item.failed) || Array.from(state.workers.values()).flatMap((worker) => worker.media.items || []).at(-1); const formatTime = (seconds) => { const value = Math.max(0, Math.floor(Number(seconds) || 0)); return `${Math.floor(value / 60)}:${String(value % 60).padStart(2, '0')}`; }; root.querySelector('.cxh-media-time').textContent = activeMedia ? `${activeMedia.type === 'audio' ? '音频' : '视频'} ${formatTime(activeMedia.currentTime)} / ${formatTime(activeMedia.duration)} · 剩余 ${formatTime(Math.max(0, activeMedia.duration - activeMedia.currentTime))} · ${activeMedia.speed || state.speed}x` : '当前没有媒体'; const workerList = root.querySelector('.cxh-worker-list'); workerList.replaceChildren(); const workers = allWorkers.sort((a, b) => a.url.localeCompare(b.url)); if (!workers.length) { const empty = document.createElement('div'); empty.className = 'cxh-empty'; empty.textContent = state.enabled ? '等待课程页面或播放器状态' : '启动后显示页面与播放器状态'; workerList.appendChild(empty); } else { workers.forEach((worker, index) => { const row = document.createElement('div'); row.className = 'cxh-worker-row'; const header = document.createElement('div'); header.className = 'cxh-worker-header'; const name = document.createElement('strong'); name.textContent = worker.id === WORKER_ID ? '主页面' : `子页面 ${index + 1}`; const status = document.createElement('span'); status.className = worker.busy ? 'cxh-state is-busy' : worker.hasWork ? 'cxh-state is-ready' : 'cxh-state is-idle'; status.textContent = worker.busy ? '处理中' : worker.hasWork ? '已就绪' : '等待'; header.append(name, status); const location = document.createElement('div'); location.className = 'cxh-worker-location'; location.textContent = workerLocation(worker); location.title = workerLocation(worker); const details = document.createElement('div'); details.className = 'cxh-worker-details'; const types = (worker.media.items || []).map((item) => item.type === 'audio' ? '音频' : '视频'); details.textContent = `媒体 ${worker.media.completed}/${worker.media.total}${types.length ? `(${types.join('、')})` : ''} · 题目 ${worker.questions.completed}/${worker.questions.total} · 其他 ${worker.auxiliary?.completed || 0}/${worker.auxiliary?.total || 0} · 待处理 ${worker.questions.pending + Number(worker.auxiliary?.pending || 0)}`; row.append(header, location, details); workerList.appendChild(row); }); } const questionList = root.querySelector('.cxh-question-list'); questionList.replaceChildren(); const stageLabels = { discovered: '未匹配', matched: '已匹配', filled: '已填写', submitted: '已提交', confirmed: '已确认' }; const questionItems = workers.flatMap((worker) => worker.questions?.items || []); if (!questionItems.length) { const empty = document.createElement('div'); empty.className = 'cxh-empty'; empty.textContent = '当前没有题目'; questionList.appendChild(empty); } else { questionItems.forEach((item) => { const row = document.createElement('div'); row.className = 'cxh-question-row'; const title = document.createElement('div'); title.className = 'cxh-question-title'; title.textContent = item.title || '未命名题目'; const stage = document.createElement('span'); stage.className = 'cxh-question-stage'; stage.textContent = stageLabels[item.stage] || item.stage || '等待'; row.append(title, stage); if (item.reason) { const reason = document.createElement('div'); reason.className = 'cxh-question-reason'; reason.textContent = item.reason; row.appendChild(reason); } questionList.appendChild(row); }); } const logList = root.querySelector('.cxh-log-list'); logList.replaceChildren(); const storedLogs = storageApi.get(STORE.logs, []); const logs = (Array.isArray(storedLogs) ? storedLogs : []) .filter((line) => state.logFilter === 'all' || String(line).includes(`[${state.logFilter}]`)) .slice(0, 20); if (!logs.length) { const empty = document.createElement('div'); empty.className = 'cxh-empty'; empty.textContent = '暂无运行记录'; logList.appendChild(empty); } else { logs.forEach((line) => { const item = document.createElement('div'); item.className = 'cxh-log-item'; item.textContent = String(line); logList.appendChild(item); }); } root.querySelector('.cxh-updated-at').textContent = `更新于 ${new Date().toLocaleTimeString()}`; } function toggleProgressWindow(force) { if (!state.progressWindow) return; state.progressOpen = typeof force === 'boolean' ? force : !state.progressOpen; if (state.progressOpen && state.todoOpen) toggleTodoWindow(false); if (state.progressOpen && state.logOpen) toggleLogWindow(false); state.progressWindow.hidden = !state.progressOpen; state.panel?.querySelector('.cxh-open-progress')?.setAttribute('aria-expanded', String(state.progressOpen)); if (state.progressOpen) renderProgressWindow(activeWorkerSummary()); } function renderLogWindow() { const root = state.logWindow; if (!root || !state.logOpen) return; const output = root.querySelector('.cxh-log-output'); const stored = storageApi.get(STORE.logs, []); const lines = (Array.isArray(stored) ? stored : []).slice(0, 200).reverse(); output.replaceChildren(); const labelMap = { error: '错误', media: '日志', question: '信息', navigation: '信息', system: '信息' }; lines.forEach((line) => { const text = String(line); const match = text.match(/^(\S+)\s+\[([^\]]+)\]\s*(.*)$/); const row = document.createElement('div'); row.className = `cxh-live-log is-${match?.[2] || 'system'}`; const time = document.createElement('span'); time.className = 'cxh-live-time'; time.textContent = match?.[1] || ''; const label = document.createElement('span'); label.className = 'cxh-live-label'; label.textContent = `[${labelMap[match?.[2]] || '日志'}]`; const message = document.createElement('span'); message.textContent = match?.[3] || text; row.append(time, label, message); output.appendChild(row); }); if (!lines.length) { const empty = document.createElement('div'); empty.className = 'cxh-empty'; empty.textContent = '暂无日志输出'; output.appendChild(empty); } root.querySelector('.cxh-log-follow').textContent = state.logAutoScroll ? '暂停滚动' : '继续滚动'; if (state.logAutoScroll) output.scrollTop = output.scrollHeight; } function toggleLogWindow(force) { if (!state.logWindow) return; state.logOpen = typeof force === 'boolean' ? force : !state.logOpen; if (state.logOpen && state.progressOpen) toggleProgressWindow(false); if (state.logOpen && state.todoOpen) toggleTodoWindow(false); state.logWindow.hidden = !state.logOpen; state.panel?.querySelector('.cxh-open-log')?.setAttribute('aria-expanded', String(state.logOpen)); if (state.logOpen) renderLogWindow(); } function exportLogs() { const stored = storageApi.get(STORE.logs, []); const text = (Array.isArray(stored) ? stored : []).slice().reverse().join('\n'); downloadText('chaoxing-helper.log.txt', text, 'text/plain'); } function todoStatusText(status) { if (status === 'running') return '进行中'; if (status === 'completed') return '已完成'; if (status === 'failed') return '失败'; if (status === 'skipped') return '已跳过'; return '排队'; } function todoStageText(stage) { return ({ detected: '已识别', navigating: '正在打开', running: '处理中', 'waiting-confirm': '等待平台确认', 'waiting-intervention': '等待人工处理', completed: '已完成', failed: '失败', skipped: '已跳过' })[stage] || ''; } function summarizeCourseTodos(todos) { const sortedFailures = todos.filter((todo) => todo.error).sort((a, b) => Number(b.failedAt || b.updatedAt || 0) - Number(a.failedAt || a.updatedAt || 0) ); return { total: todos.length, completed: todos.filter((todo) => todo.status === 'completed').length, pending: todos.filter((todo) => todo.status === 'pending' || todo.status === 'running').length, failed: todos.filter((todo) => todo.status === 'failed').length, lastFailure: sortedFailures[0]?.error || '' }; } function saveUiState() { saveObject(STORE.uiState, state.uiState); } function retryTodo(todo) { todo.status = 'pending'; todo.stage = 'detected'; todo.error = ''; todo.failedAt = 0; saveTodos(); recordExecutionEvent('retried', { todoId: todo.id, title: todo.title, stage: todo.stage }); if (!state.queuePaused && state.enabled) activateNextTodo(); pushLog(`重新排队:${todo.courseName} / ${todo.title}`, true); } function skipTodo(todo) { const wasRunning = currentRunningTodo()?.id === todo.id; todo.status = 'skipped'; todo.stage = 'skipped'; todo.skippedAt = Date.now(); todo.completionSource = '用户手动跳过'; if (wasRunning) { state.queueNavigationAt = 0; state.currentTaskKey = ''; resetTaskProgress(0); } saveTodos(); recordExecutionEvent('skipped', { todoId: todo.id, title: todo.title, stage: todo.stage }); activateNextTodo(); pushLog(`已跳过:${todo.courseName} / ${todo.title}`, true); } function todoExportRows() { return [...state.todos] .sort((a, b) => a.courseOrder - b.courseOrder || a.taskOrder - b.taskOrder) .map((todo) => ({ course: todo.courseName || '', chapter: todo.title || '', type: todo.type || '任务点', status: todoStatusText(todo.status), deadline: todo.deadline || '', source: todo.source || '', completionSource: todo.completionSource || '', durationMs: todo.completedAt && todo.startedAt ? todo.completedAt - todo.startedAt : '', mediaProgress: todo.completionSummary ? `${todo.completionSummary.media.completed}/${todo.completionSummary.media.total}` : '', questionProgress: todo.completionSummary ? `${todo.completionSummary.questions.completed}/${todo.completionSummary.questions.total}` : '', auxiliaryProgress: todo.completionSummary ? `${todo.completionSummary.auxiliary.completed}/${todo.completionSummary.auxiliary.total}` : '', error: todo.error || '' })); } function downloadText(filename, text, type = 'application/json') { const blob = new Blob([text], { type: `${type};charset=utf-8` }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; link.click(); window.setTimeout(() => URL.revokeObjectURL(url), 1000); } function exportTodos(format = 'json', download = true) { const rows = todoExportRows(); let text; if (format === 'csv') { const quote = (value) => `"${String(value ?? '').replace(/"/g, '""')}"`; const columns = ['course', 'chapter', 'type', 'status', 'deadline', 'source', 'completionSource', 'durationMs', 'mediaProgress', 'questionProgress', 'auxiliaryProgress', 'error']; text = [columns.join(','), ...rows.map((row) => columns.map((key) => quote(row[key])).join(','))].join('\n'); } else { text = JSON.stringify({ schemaVersion: TODO_EXPORT_SCHEMA_VERSION, exportedAt: new Date().toISOString(), tasks: rows }, null, 2); } if (download) downloadText(`chaoxing-todos.${format}`, text, format === 'csv' ? 'text/csv' : 'application/json'); return text; } function renderTodoWindow() { const root = state.todoWindow; if (!root || !state.todoOpen) return; const query = normalize(state.todoSearch); const sorted = [...state.todos].sort((a, b) => a.courseOrder - b.courseOrder || a.taskOrder - b.taskOrder ).filter((todo) => { if (query && !normalize(`${todo.courseName} ${todo.title}`).includes(query)) return false; if (state.todoStatusFilter !== 'all' && todo.status !== state.todoStatusFilter) return false; if (state.todoTypeFilter !== 'all' && (todo.type || '任务点') !== state.todoTypeFilter) return false; return true; }); const counts = state.todos.reduce((result, todo) => { result[todo.status] = (result[todo.status] || 0) + 1; return result; }, { pending: 0, running: 0, completed: 0 }); root.querySelector('.cxh-todo-pending').textContent = String(counts.pending || 0); root.querySelector('.cxh-todo-running').textContent = String(counts.running || 0); root.querySelector('.cxh-todo-completed').textContent = String(counts.completed || 0); const queueButton = root.querySelector('.cxh-queue-toggle'); queueButton.textContent = state.queuePaused || !state.enabled ? '继续队列' : '暂停队列'; const list = root.querySelector('.cxh-todo-list'); list.replaceChildren(); if (!sorted.length) { const empty = document.createElement('div'); empty.className = 'cxh-empty'; empty.textContent = '当前没有待办任务'; list.appendChild(empty); return; } const groups = new Map(); sorted.forEach((todo) => { const key = `${todo.courseOrder}:${todo.courseName}`; if (!groups.has(key)) groups.set(key, []); groups.get(key).push(todo); }); groups.forEach((todos, key) => { const group = document.createElement('section'); group.className = 'cxh-todo-course'; const collapsed = Boolean(state.uiState.collapsedCourses?.[key]); group.classList.toggle('is-collapsed', collapsed); const title = document.createElement('div'); title.className = 'cxh-todo-course-title'; const courseStats = summarizeCourseTodos(todos); title.textContent = `${key.slice(key.indexOf(':') + 1)} · 完成 ${courseStats.completed}/${courseStats.total}`; title.tabIndex = 0; title.addEventListener('click', () => { state.uiState.collapsedCourses = state.uiState.collapsedCourses || {}; state.uiState.collapsedCourses[key] = !state.uiState.collapsedCourses[key]; saveUiState(); renderTodoWindow(); }); group.appendChild(title); const courseSummary = document.createElement('div'); courseSummary.className = 'cxh-todo-course-summary'; courseSummary.textContent = courseStats.lastFailure ? `待处理 ${courseStats.pending} · 失败 ${courseStats.failed} · 最近失败:${courseStats.lastFailure}` : `待处理 ${courseStats.pending} · 失败 ${courseStats.failed}`; group.appendChild(courseSummary); todos.forEach((todo) => { const row = document.createElement('div'); row.className = `cxh-todo-item is-${todo.status}`; const content = document.createElement('div'); content.className = 'cxh-todo-content'; const taskTitle = document.createElement('div'); taskTitle.className = 'cxh-todo-title'; taskTitle.textContent = todo.title; const meta = document.createElement('div'); meta.className = 'cxh-todo-meta'; const deadline = todo.deadline ? ` · 截止 ${todo.deadline}` : ''; const source = todo.completionSource || todo.source || '本地待办'; const stage = todoStageText(todo.stage); const completion = todo.completionSummary; const summaryText = completion ? ` · 媒体 ${completion.media.completed}/${completion.media.total} · 题目 ${completion.questions.completed}/${completion.questions.total} · 其他 ${completion.auxiliary.completed}/${completion.auxiliary.total}` : ''; meta.textContent = `${todo.type || '任务点'} · ${todoStatusText(todo.status)}${stage ? ` · ${stage}` : ''} · 来源 ${source}${summaryText}${deadline}${todo.error ? ` · ${todo.error}` : ''}`; if (todo.deadline && Date.parse(todo.deadline) - Date.now() < 3 * 86400000) row.classList.add('is-urgent'); content.append(taskTitle, meta); const status = document.createElement('span'); status.className = `cxh-todo-status is-${todo.status}`; status.textContent = todoStatusText(todo.status); const open = document.createElement('button'); open.type = 'button'; open.className = 'cxh-todo-open'; open.textContent = '打开'; open.disabled = !todo.url; open.addEventListener('click', () => { if (todo.url) location.assign(todo.url); }); row.append(content, status, open); if (todo.status === 'failed') { const retry = document.createElement('button'); retry.type = 'button'; retry.className = 'cxh-todo-retry'; retry.textContent = '重试'; retry.addEventListener('click', () => retryTodo(todo)); const skip = document.createElement('button'); skip.type = 'button'; skip.className = 'cxh-todo-skip'; skip.textContent = '跳过'; skip.addEventListener('click', () => skipTodo(todo)); row.append(retry, skip); } group.appendChild(row); }); list.appendChild(group); }); } function toggleTodoWindow(force) { if (!state.todoWindow) return; state.todoOpen = typeof force === 'boolean' ? force : !state.todoOpen; if (state.todoOpen && state.progressOpen) toggleProgressWindow(false); if (state.todoOpen && state.logOpen) toggleLogWindow(false); state.todoWindow.hidden = !state.todoOpen; state.panel?.querySelector('.cxh-open-todos')?.setAttribute('aria-expanded', String(state.todoOpen)); if (state.todoOpen) renderTodoWindow(); } function toggleTodoQueue() { state.queuePaused = !(state.queuePaused || !state.enabled); state.enabled = !state.queuePaused; storageApi.set(STORE.queuePaused, state.queuePaused); storageApi.set(STORE.enabled, state.enabled); if (state.queuePaused) { invalidateOperations(); cancelTaskHandlers('queue-paused'); pauseLocalMedia(); } else { resetFailedMedia(); broadcastToChildren({ type: MESSAGE_TYPE, kind: 'reset-media-failures' }); state.lastMediaFailureKey = ''; state.completionAwaitStartedAt = 0; state.completionAwaitTaskKey = ''; if (/媒体.*(无进度|卡住)|平台尚未确认任务完成/.test(state.lastError)) state.lastError = ''; activateNextTodo(); } pushLog(state.queuePaused ? '已暂停待办队列' : '已继续待办队列', true); updatePanel(); } function togglePanelMinimized(force) { if (!state.panel) return; const minimized = typeof force === 'boolean' ? force : !state.panel.classList.contains('is-minimized'); state.panel.classList.toggle('is-minimized', minimized); const button = state.panel.querySelector('.cxh-minimize'); if (button) { button.textContent = minimized ? '+' : '\u2212'; button.title = minimized ? '展开控制面板' : '最小化控制面板'; button.setAttribute('aria-label', button.title); button.setAttribute('aria-expanded', String(!minimized)); } state.uiState.panelMinimized = minimized; saveUiState(); } function createPanel() { if (!isController() || !document.body) return; if (state.panel?.isConnected && state.progressWindow?.isConnected && state.todoWindow?.isConnected && state.logWindow?.isConnected) return; state.panel?.remove(); state.progressWindow?.remove(); state.todoWindow?.remove(); state.logWindow?.remove(); state.panel = null; state.progressWindow = null; state.todoWindow = null; state.logWindow = null; let style = document.querySelector('#cx-helper-style'); if (!style) { style = document.createElement('style'); style.id = 'cx-helper-style'; style.textContent = ` #cx-helper-panel,#cx-helper-panel *{box-sizing:border-box;letter-spacing:0} #cx-helper-panel{position:fixed;z-index:2147483647;right:16px;bottom:16px;width:312px;background:#fff;color:#273142;font:13px/1.5 Arial,'Microsoft YaHei',sans-serif;border:1px solid #cbd3df;border-radius:8px;box-shadow:0 12px 32px #1f29372b;overflow:hidden} #cx-helper-panel .cxh-panel-header{height:54px;display:flex;align-items:center;gap:10px;padding:9px 10px;background:#fff;border-bottom:1px solid #e5e9f0;user-select:none} #cx-helper-panel .cxh-brand-mark{display:grid;place-items:center;width:32px;height:32px;flex:0 0 32px;border-radius:6px;background:#1677ff;color:#fff;font-size:15px;font-weight:700} #cx-helper-panel .cxh-brand-copy{min-width:0;flex:1} #cx-helper-panel .cxh-brand-line{display:flex;align-items:center;gap:6px;min-width:0} #cx-helper-panel .cxh-title{font-size:14px;font-weight:700;color:#1f2937;white-space:nowrap} #cx-helper-panel .cxh-version{font-size:10px;font-weight:600;color:#64748b;background:#eef2f7;border-radius:3px;padding:1px 4px} #cx-helper-panel .cxh-subtitle{font-size:11px;color:#7b8798;margin-top:1px} #cx-helper-panel .cxh-minimize{display:grid;place-items:center;width:28px;height:28px;min-width:28px;padding:0;border:0;border-radius:4px;background:transparent;color:#667085;font:18px/1 Arial,sans-serif;cursor:pointer} #cx-helper-panel .cxh-minimize:hover{background:#f0f3f7;color:#1f2937} #cx-helper-panel .cxh-panel-body{padding:10px} #cx-helper-panel.is-minimized{width:218px} #cx-helper-panel.is-minimized .cxh-panel-body{display:none} #cx-helper-panel .cxh-status-band{display:grid;grid-template-columns:8px minmax(0,1fr);align-items:start;gap:8px;padding:8px 9px;background:#f6f8fb;border-left:3px solid #1677ff} #cx-helper-panel .cxh-status-dot{width:8px;height:8px;margin-top:6px;border-radius:50%;background:#98a2b3} #cx-helper-panel[data-run-state="running"] .cxh-status-dot{background:#16a34a;box-shadow:0 0 0 3px #dcfce7} #cx-helper-panel[data-run-state="error"] .cxh-status-dot{background:#dc2626;box-shadow:0 0 0 3px #fee2e2} #cx-helper-panel[data-run-state="readonly"] .cxh-status-dot{background:#d97706;box-shadow:0 0 0 3px #fef3c7} #cx-helper-panel .cxh-status{min-height:20px;color:#344054;word-break:break-word} #cx-helper-panel .cxh-progress{padding:7px 9px;color:#667085;border-bottom:1px solid #edf0f4;white-space:normal} #cx-helper-panel .cxh-nav{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:4px;padding:9px 0;border-bottom:1px solid #edf0f4} #cx-helper-panel .cxh-nav button{display:block;width:100%;height:30px;min-width:0;min-height:0;margin:0;padding:4px 6px;border:1px solid transparent;border-radius:4px;background:transparent;color:#526075;font:12px/1 Arial,'Microsoft YaHei',sans-serif;cursor:pointer} #cx-helper-panel .cxh-nav button:hover{background:#f1f5f9;color:#1677ff} #cx-helper-panel .cxh-nav button[aria-expanded="true"]{background:#eaf3ff;border-color:#b9d7ff;color:#0969da;font-weight:700} #cx-helper-panel .cxh-section-label{margin:9px 0 5px;color:#98a2b3;font-size:11px;font-weight:700;text-transform:uppercase} #cx-helper-panel .cxh-row{display:flex;align-items:center;justify-content:space-between;gap:12px;min-height:34px;padding:4px 2px;color:#344054} #cx-helper-panel .cxh-speed,#cx-helper-panel .cxh-navigation-mode{width:118px;height:28px;background:#f7f9fc;color:#273142;border:1px solid #cbd3df;border-radius:4px;padding:3px 7px;outline:none} #cx-helper-panel .cxh-speed{width:72px} #cx-helper-panel .cxh-speed:focus,#cx-helper-panel .cxh-navigation-mode:focus{border-color:#1677ff;box-shadow:0 0 0 2px #dbeafe} #cx-helper-panel .cxh-volume-control{display:flex;align-items:center;gap:7px;width:138px} #cx-helper-panel .cxh-volume{width:96px;accent-color:#1677ff} #cx-helper-panel .cxh-volume-value{width:34px;color:#667085;font-size:11px;text-align:right} #cx-helper-panel .cxh-auto-jump{appearance:none;width:36px;height:20px;flex:0 0 36px;margin:0;padding:2px;border:0;border-radius:10px;background:#cbd5e1;cursor:pointer;transition:background .15s ease} #cx-helper-panel .cxh-auto-jump::before{display:block;width:16px;height:16px;border-radius:50%;background:#fff;box-shadow:0 1px 3px #0003;content:'';transition:transform .15s ease} #cx-helper-panel .cxh-auto-jump:checked{background:#1677ff} #cx-helper-panel .cxh-auto-jump:checked::before{transform:translateX(16px)} #cx-helper-panel .cxh-actions{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:6px} #cx-helper-panel .cxh-actions button{display:inline-flex;align-items:center;justify-content:center;width:100%;height:30px;min-width:0;min-height:0;margin:0;padding:4px 7px;border:1px solid #cbd3df;border-radius:4px;background:#fff;color:#475467;font:12px/1 Arial,'Microsoft YaHei',sans-serif;cursor:pointer} #cx-helper-panel .cxh-actions button:hover{border-color:#79b3ff;background:#f3f8ff;color:#0969da} #cx-helper-panel .cxh-actions .cxh-toggle{grid-column:1/-1;height:34px;border-color:#1677ff;background:#1677ff;color:#fff;font-weight:700} #cx-helper-panel .cxh-actions .cxh-toggle:hover{background:#0969da;color:#fff} #cx-helper-progress-window,#cx-helper-todo-window,#cx-helper-log-window{letter-spacing:0} #cx-helper-progress-window{box-sizing:border-box;position:fixed;z-index:2147483646;right:344px;bottom:16px;width:min(460px,calc(100vw - 376px));max-height:min(700px,calc(100vh - 32px));overflow:auto;padding:14px;background:#fff;color:#273142;font:13px/1.5 Arial,'Microsoft YaHei',sans-serif;border:1px solid #cbd3df;border-radius:8px;box-shadow:0 16px 38px #1f293733} #cx-helper-progress-window[hidden]{display:none} #cx-helper-progress-window .cxh-window-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px} #cx-helper-progress-window .cxh-window-title{font-size:15px;font-weight:700;color:#1f2937} #cx-helper-progress-window .cxh-close{width:28px;height:28px;padding:0;border:0;border-radius:4px;background:#f1f4f8;color:#667085;font-size:19px;line-height:28px;cursor:pointer} #cx-helper-progress-window .cxh-task-label,#cx-helper-progress-window .cxh-section-title{color:#98a2b3;font-size:11px;font-weight:700;margin-bottom:4px} #cx-helper-progress-window .cxh-current-task{font-weight:700;margin-bottom:12px;overflow-wrap:anywhere} #cx-helper-progress-window .cxh-current-meta{color:#7b8798;font-size:11px;margin-top:-8px;margin-bottom:12px;overflow-wrap:anywhere} #cx-helper-progress-window .cxh-overview{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin-bottom:14px} #cx-helper-progress-window .cxh-meter-head{display:flex;justify-content:space-between;margin-bottom:5px} #cx-helper-progress-window .cxh-meter-track{height:6px;background:#e8edf3;border-radius:3px;overflow:hidden} #cx-helper-progress-window .cxh-meter-fill{height:100%;width:0;background:#22c55e;transition:width .2s ease} #cx-helper-progress-window [data-meter="tasks"] .cxh-meter-fill{background:#1677ff} #cx-helper-progress-window [data-meter="questions"] .cxh-meter-fill{background:#f59e0b} #cx-helper-progress-window .cxh-counts{display:flex;gap:18px;margin-bottom:14px;color:#667085} #cx-helper-progress-window .cxh-counts strong{color:#1f2937} #cx-helper-progress-window .cxh-media-time{padding:7px 9px;margin-bottom:8px;background:#f3f7fc;color:#344054;border-left:3px solid #1677ff} #cx-helper-progress-window .cxh-issue{padding:8px 9px;margin-bottom:8px;background:#fff1f2;color:#b42318;border:1px solid #fecdd3;border-radius:4px} #cx-helper-progress-window .cxh-issue[hidden]{display:none} #cx-helper-progress-window .cxh-section{border-top:1px solid #e5e9f0;padding-top:11px;margin-top:11px} #cx-helper-progress-window .cxh-worker-row{padding:8px 0;border-bottom:1px solid #edf0f4} #cx-helper-progress-window .cxh-worker-header{display:flex;align-items:center;justify-content:space-between;gap:8px} #cx-helper-progress-window .cxh-state{font-size:11px;padding:2px 6px;border-radius:3px;background:#eef2f7;color:#526075} #cx-helper-progress-window .cxh-state.is-busy{background:#fff4d6;color:#9a6700} #cx-helper-progress-window .cxh-state.is-ready{background:#dcfce7;color:#18794e} #cx-helper-progress-window .cxh-state.is-idle{background:#eef2f7;color:#667085} #cx-helper-progress-window .cxh-worker-location{color:#7b8798;white-space:nowrap;overflow:hidden;text-overflow:ellipsis} #cx-helper-progress-window .cxh-worker-details{color:#526075;margin-top:2px} #cx-helper-progress-window .cxh-log-item{padding:4px 0;border-bottom:1px solid #edf0f4;overflow-wrap:anywhere} #cx-helper-progress-window .cxh-question-row{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:8px;padding:6px 0;border-bottom:1px solid #edf0f4} #cx-helper-progress-window .cxh-question-title{overflow-wrap:anywhere} #cx-helper-progress-window .cxh-question-stage{align-self:start;font-size:11px;padding:2px 6px;border-radius:3px;background:#eef2f7;color:#526075} #cx-helper-progress-window .cxh-question-reason{grid-column:1/-1;color:#b42318;font-size:11px} #cx-helper-progress-window .cxh-log-controls{display:flex;gap:6px;margin-bottom:6px} #cx-helper-progress-window .cxh-log-controls select,#cx-helper-progress-window .cxh-log-controls button{background:#fff;color:#475467;border:1px solid #cbd3df;border-radius:4px;padding:4px 7px} #cx-helper-progress-window .cxh-empty{color:#98a2b3;padding:8px 0} #cx-helper-progress-window .cxh-updated-at{color:#98a2b3;font-size:11px;margin-top:10px;text-align:right} #cx-helper-todo-window{box-sizing:border-box;position:fixed;z-index:2147483646;right:344px;bottom:16px;width:min(560px,calc(100vw - 376px));height:min(720px,calc(100vh - 32px));min-width:360px;min-height:300px;resize:both;overflow:auto;padding:14px;background:#fff;color:#273142;font:13px/1.5 Arial,'Microsoft YaHei',sans-serif;border:1px solid #cbd3df;border-radius:8px;box-shadow:0 16px 38px #1f293733} #cx-helper-todo-window[hidden]{display:none} #cx-helper-todo-window .cxh-todo-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:8px} #cx-helper-todo-window .cxh-todo-header{cursor:move;user-select:none} #cx-helper-todo-window .cxh-todo-window-title{font-size:15px;font-weight:700;color:#1f2937} #cx-helper-todo-window .cxh-todo-close{width:28px;height:28px;padding:0;border:0;border-radius:4px;background:#f1f4f8;color:#667085;font-size:19px;line-height:28px;cursor:pointer} #cx-helper-todo-window .cxh-todo-scan-status{color:#7b8798;margin-bottom:10px} #cx-helper-todo-window .cxh-todo-summary{display:flex;gap:18px;padding:9px 0;border-top:1px solid #e5e9f0;border-bottom:1px solid #e5e9f0;color:#667085} #cx-helper-todo-window .cxh-todo-summary strong{color:#1f2937} #cx-helper-todo-window .cxh-todo-controls{display:flex;flex-wrap:wrap;gap:7px;margin:10px 0} #cx-helper-todo-window .cxh-todo-controls button,#cx-helper-todo-window .cxh-todo-open,#cx-helper-todo-window .cxh-todo-retry,#cx-helper-todo-window .cxh-todo-skip{display:inline-flex;align-items:center;justify-content:center;width:auto;height:30px;min-width:0;min-height:0;margin:0;padding:4px 8px;border:1px solid #b9d7ff;border-radius:4px;background:#f3f8ff;color:#0969da;font:12px/1 Arial,'Microsoft YaHei',sans-serif;cursor:pointer} #cx-helper-todo-window .cxh-todo-filters{display:grid;grid-template-columns:minmax(130px,1fr) auto auto;gap:7px;margin:10px 0} #cx-helper-todo-window .cxh-todo-filters input,#cx-helper-todo-window .cxh-todo-filters select{min-width:0;background:#f7f9fc;color:#273142;border:1px solid #cbd3df;border-radius:4px;padding:5px 7px} #cx-helper-todo-window button:disabled{opacity:.45;cursor:default} #cx-helper-todo-window .cxh-todo-course{margin-top:12px} #cx-helper-todo-window .cxh-todo-course-title{font-weight:700;padding:6px 0;color:#344054;border-bottom:1px solid #dfe5ec;cursor:pointer} #cx-helper-todo-window .cxh-todo-course-summary{padding:5px 0;color:#7b8798;font-size:12px;overflow-wrap:anywhere} #cx-helper-todo-window .cxh-todo-course-title::before{content:'▾ ';color:#98a2b3} #cx-helper-todo-window .cxh-todo-course.is-collapsed .cxh-todo-course-title::before{content:'▸ '} #cx-helper-todo-window .cxh-todo-course.is-collapsed .cxh-todo-item,#cx-helper-todo-window .cxh-todo-course.is-collapsed .cxh-todo-course-summary{display:none} #cx-helper-todo-window .cxh-todo-item{display:grid;grid-template-columns:minmax(0,1fr) auto auto;align-items:center;gap:8px;padding:9px 0;border-bottom:1px solid #edf0f4} #cx-helper-todo-window .cxh-todo-item.is-completed{opacity:.65} #cx-helper-todo-window .cxh-todo-item.is-urgent{border-left:3px solid #f59e0b;padding-left:7px} #cx-helper-todo-window .cxh-todo-title{overflow-wrap:anywhere} #cx-helper-todo-window .cxh-todo-meta{color:#7b8798;font-size:12px;margin-top:2px} #cx-helper-todo-window .cxh-todo-status{font-size:11px;padding:2px 6px;border-radius:3px;background:#eef2f7;color:#526075} #cx-helper-todo-window .cxh-todo-status.is-running{background:#fff4d6;color:#9a6700} #cx-helper-todo-window .cxh-todo-status.is-completed{background:#dcfce7;color:#18794e} #cx-helper-todo-window .cxh-empty{color:#98a2b3;padding:14px 0} #cx-helper-log-window{box-sizing:border-box;position:fixed;z-index:2147483646;right:344px;bottom:16px;width:min(640px,calc(100vw - 376px));height:min(540px,calc(100vh - 32px));display:flex;flex-direction:column;background:#fff;color:#344054;font:13px/1.65 Consolas,'Microsoft YaHei',monospace;border:1px solid #cbd3df;border-radius:8px;box-shadow:0 16px 38px #1f293733;overflow:hidden} #cx-helper-log-window[hidden]{display:none} #cx-helper-log-window .cxh-live-header{display:flex;align-items:center;justify-content:space-between;padding:9px 11px;background:#f7f9fc;color:#344054;border-bottom:1px solid #dfe5ec;font-family:Arial,'Microsoft YaHei',sans-serif} #cx-helper-log-window .cxh-live-title{font-size:15px;font-weight:700} #cx-helper-log-window .cxh-live-actions{display:flex;gap:6px} #cx-helper-log-window button{border:1px solid #cbd3df;border-radius:4px;background:#fff;color:#475467;padding:4px 8px;cursor:pointer} #cx-helper-log-window .cxh-log-output{flex:1;overflow:auto;padding:9px 10px;scrollbar-color:#aeb8c5 #f1f4f8} #cx-helper-log-window .cxh-live-log{display:flex;gap:7px;align-items:baseline;white-space:pre-wrap;overflow-wrap:anywhere} #cx-helper-log-window .cxh-live-time{color:#98a2b3;flex:0 0 auto} #cx-helper-log-window .cxh-live-label{color:#0969da;flex:0 0 auto} #cx-helper-log-window .cxh-live-log.is-error .cxh-live-label{color:#dc2626} #cx-helper-log-window .cxh-live-log.is-media .cxh-live-label{color:#526075} @media(max-width:760px){#cx-helper-panel{right:10px;bottom:10px;width:min(312px,calc(100vw - 20px))}#cx-helper-panel.is-minimized{width:218px}#cx-helper-progress-window,#cx-helper-todo-window,#cx-helper-log-window{z-index:2147483647;left:10px;right:10px;top:10px;bottom:10px;width:auto;height:auto;min-width:0;max-height:none}#cx-helper-progress-window .cxh-overview{grid-template-columns:1fr}#cx-helper-todo-window .cxh-todo-filters{grid-template-columns:1fr 1fr}#cx-helper-todo-window .cxh-todo-filters input{grid-column:1/-1}#cx-helper-todo-window .cxh-todo-item{grid-template-columns:minmax(0,1fr) auto}#cx-helper-todo-window .cxh-todo-open{grid-column:2}} `; document.documentElement.appendChild(style); } const panel = document.createElement('div'); panel.id = 'cx-helper-panel'; panel.innerHTML = `