// ==UserScript== // @name 华医网自动化考试助手 (V2.3.5) // @namespace http://tampermonkey.net/ // @version 2.3.5 // @description 按课程、题干和选项内容识别试题,核对本次提交记录,避免重考乱序串题。 // @author Gemini (Key Optimized) // @match *://*.91huayi.com/pages/course.aspx* // @match *://*.91huayi.com/pages/exam.aspx* // @match *://*.91huayi.com/pages/exam_result.aspx* // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @grant GM_addStyle // @run-at document-idle // @license GPL-3.0 // ==/UserScript== (function() { 'use strict'; // --- 配置项 --- const BASE_ANSWER_DELAY_MS = 500; const BASE_SUBMIT_DELAY_MS = 2000; const BASE_RETRY_DELAY_MS = 3000; const SCRIPT_STATE_KEY = 'exam_script_state_v11'; // 版本号更新 const COURSE_LIST_URL_FRAGMENT = '/pages/course.aspx'; const PROJECT_CONTEXT_KEY = 'exam_queue_project_v12'; let executionGeneration = 0; // --- 辅助函数 --- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); const getRandomDelay = (base, range = 1500) => base + Math.random() * range; // --- 数据存储模块 --- const db = { load: (key, defaultValue) => { const value = GM_getValue(key); return value === undefined ? defaultValue : JSON.parse(value); }, save: (key, data) => GM_setValue(key, JSON.stringify(data)), clear: (key) => GM_deleteValue(key) }; // --- 数据库定义 (版本号更新) --- // 使用独立的新题库,保留旧库原始数据,避免沿用可能串题的历史判断。 const correctAnswersDB = { key: 'correct_answers_db_v12', get: () => db.load(correctAnswersDB.key, {}), set: (data) => db.save(correctAnswersDB.key, data) }; const wrongAttemptsDB = { key: 'wrong_attempts_db_v12', get: () => db.load(wrongAttemptsDB.key, {}), set: (data) => db.save(wrongAttemptsDB.key, data), clear: () => db.clear(wrongAttemptsDB.key) }; const examQueueDB = { key: 'exam_queue_db_v11', get: () => db.load(examQueueDB.key, []), set: (data) => db.save(examQueueDB.key, data), clear: () => db.clear(examQueueDB.key) }; const returnUrlDB = { key: 'return_url_db_v11', get: () => db.load(returnUrlDB.key, null), set: (data) => db.save(returnUrlDB.key, data), clear: () => db.clear(returnUrlDB.key) }; const questionOptionMapDB = { key: 'question_option_map_v12', get: () => db.load(questionOptionMapDB.key, null), set: (data) => db.save(questionOptionMapDB.key, data), clear: () => db.clear(questionOptionMapDB.key) }; const debugKeysDB = { key: 'debug_keys_db_v11', get: () => db.load(debugKeysDB.key, []), set: (data) => db.save(debugKeysDB.key, data), clear: () => db.clear(debugKeysDB.key) }; // --- 核心逻辑 --- // --- V2.3.3 核心修改:优化Key的构造逻辑 --- function normalizeQuestion(text) { return String(text || '').normalize('NFKC').trim() .replace(/^(?:第\s*\d+\s*题\s*[:、.]?|\(\d+\)\s*[:、.]?|\d+\s*[、.:](?!\d))\s*/, '') .replace(/\s+/g, ''); } function normalizeOption(text) { return String(text || '').normalize('NFKC').trim() .replace(/^(?:\([A-Z]\)|[A-Z][、.:)])\s*/i, '') .replace(/\s+/g, ''); } const currentCourseId = () => (new URL(location.href).searchParams.get('cwid') || '').toLowerCase(); const textOf = element => element?.innerText ?? element?.textContent ?? ''; function questionIdentity(stem, options) { return JSON.stringify([currentCourseId(), stem, options.map(o => o.content).sort()]); } function stopExam(reason) { console.error(`自动考试已停止:${reason}`); setScriptState(false); updatePanel(); } function collectCourseStates() { return Array.from(document.querySelectorAll('.course')).map(courseEl => { const link = courseEl.querySelector('.cw-title-link, h3 a.f14blue'); const progress = courseEl.querySelector('.cw-progress-row[data-studystate]'); const statusElement = courseEl.querySelector('.cw-status button') || courseEl.querySelector('h3 > span'); const status = (progress?.getAttribute('data-studystate') || statusElement?.textContent || '').trim(); let cwid = null; try { cwid = new URL(link?.getAttribute('href') || '', location.href).searchParams.get('cwid'); } catch (error) { /* 标记为无法识别 */ } return { cwid, name: link?.textContent.trim() || '', status, recognized: !!link && /^[a-f0-9-]{36}$/i.test(cwid || '') && ['待考试', '学习中', '未学习', '已完成'].includes(status) }; }); } function getOptionInfo(optionEl) { if (!optionEl) return null; const label = optionEl.querySelector('label'); if (!label) return null; const rawText = textOf(label).normalize('NFKC').trim(); const match = rawText.match(/^(?:\(([A-Z])\)|([A-Z])[、.:)])/i); return { letter: match ? (match[1] || match[2]).toUpperCase() : null, content: normalizeOption(rawText), // 使用标准化的选项内容 element: optionEl.querySelector('input.qo_name') }; } function handleCourseListPage() { console.log("脚本:进入课程列表页面。"); const courses = collectCourseStates(); if (courses.length === 0 || courses.some(course => !course.recognized)) { setScriptState(false); updatePanel(); alert('课程列表尚未加载完整或存在无法识别的课程状态,请检查页面后重新启动。'); return; } const pendingExams = [...new Map(courses.filter(course => course.status === '待考试') .map(course => [course.cwid.toLowerCase(), { cwid: course.cwid, name: course.name }])).values()]; if (pendingExams.length > 0) { console.log(`脚本:发现 ${pendingExams.length} 个待考课程。已创建考试队列。`); examQueueDB.set(pendingExams); returnUrlDB.set(window.location.href); db.save(PROJECT_CONTEXT_KEY, { cid: new URL(location.href).searchParams.get('cid'), url: location.href }); updatePanel(); processNextInQueue(); } else { const allCompleted = courses.every(course => course.status === '已完成'); console.log(allCompleted ? '脚本:所有课程均已完成。' : '脚本:当前没有待考试课程,仍有课件需要学习。'); examQueueDB.clear(); returnUrlDB.clear(); db.clear(PROJECT_CONTEXT_KEY); alert(allCompleted ? '所有课程均已完成。' : '当前没有待考试课程,仍有课件需要学习。'); setScriptState(false); updatePanel(); } } async function handleExamPage() { const generation = executionGeneration; const stillRunning = () => getScriptState() && executionGeneration === generation; console.log("脚本:进入考试页面。开始根据内容智能作答..."); const correctAnswers = correctAnswersDB.get(); const wrongAttempts = wrongAttemptsDB.get(); const questions = document.querySelectorAll('.tablestyle'); const attempt = { version: 12, courseId: currentCourseId(), submitted: false, questions: [] }; const examPageGeneratedKeys = []; if (!attempt.courseId) { stopExam('未识别到当前课件标识。'); return; } for (const questionEl of Array.from(questions)) { if (!stillRunning()) return; const stem = normalizeQuestion(textOf(questionEl.querySelector('.q_name'))); const pageOptions = Array.from(questionEl.querySelectorAll('tbody tr')).map(getOptionInfo).filter(Boolean); if (!stem || pageOptions.length < 2 || pageOptions.some(o => !o.letter || !o.content || !o.element || o.element.type !== 'radio' || o.element.disabled) || new Set(pageOptions.map(o => o.content)).size !== pageOptions.length || new Set(pageOptions.map(o => o.letter)).size !== pageOptions.length || attempt.questions.some(q => q.stem === stem)) { stopExam('题干、单选项或重复题目无法唯一识别,未提交。'); return; } const questionKey = questionIdentity(stem, pageOptions); examPageGeneratedKeys.push(questionKey); const knownCorrects = correctAnswers[questionKey] || []; const knownWrongs = wrongAttempts[questionKey] || []; const correctContents = new Set(knownCorrects.map(item => item.content)); const wrongContents = new Set(knownWrongs.map(item => item.content)); if (correctContents.size > 1 || [...correctContents].some(content => wrongContents.has(content))) { stopExam('题库中存在互相矛盾的判断,未提交。'); return; } const selected = pageOptions.find(o => correctContents.has(o.content)) || pageOptions.find(o => !wrongContents.has(o.content)); if (!selected) { stopExam('所有选项均已记为错误,需要检查本次结果,未重复选择第一个选项。'); return; } selected.element.click(); if (!selected.element.checked || pageOptions.filter(o => o.element.checked).length !== 1) { stopExam('选项未实际选中,未提交。'); return; } attempt.questions.push({ key: questionKey, stem, options: pageOptions.map(({ letter, content }) => ({ letter, content })), selectedContent: selected.content, selectedLetter: selected.letter }); console.log('本次选择(按内容)', stem, selected.content, `当前字母 ${selected.letter}`); await sleep(getRandomDelay(BASE_ANSWER_DELAY_MS, 1000)); } if (!stillRunning() || questions.length === 0) { if (questions.length === 0) { setScriptState(false); updatePanel(); console.error('未识别到试题,已停止,未提交。'); } return; } console.log("脚本:保存本次考试的<问题-选项内容-字母>映射表..."); questionOptionMapDB.set(attempt); debugKeysDB.set(examPageGeneratedKeys); const submitDelay = getRandomDelay(BASE_SUBMIT_DELAY_MS, 2000); console.log(`脚本:作答完毕,将在约 ${(submitDelay / 1000).toFixed(1)} 秒后提交...`); await sleep(submitDelay); if (!stillRunning()) return; const submit = document.getElementById('btn_submit'); if (!submit || submit.disabled) { stopExam('提交按钮不可用。'); return; } // 延迟期间若用户改过选择,不再拿旧选择解释结果。 const currentQuestions = Array.from(document.querySelectorAll('.tablestyle')); if (currentQuestions.length !== attempt.questions.length || currentQuestions.some((el, index) => { const options = Array.from(el.querySelectorAll('tbody tr')).map(getOptionInfo).filter(Boolean); const selected = options.filter(o => o.element?.checked); return questionIdentity(normalizeQuestion(textOf(el.querySelector('.q_name'))), options) !== attempt.questions[index].key || selected.length !== 1 || selected[0].content !== attempt.questions[index].selectedContent; })) { stopExam('提交前检测到题目或选择变化,请重新启动。'); return; } attempt.submitted = true; attempt.submittedAt = Date.now(); questionOptionMapDB.set(attempt); submit.click(); } async function handleResultPage() { const generation = executionGeneration; const stillRunning = () => getScriptState() && executionGeneration === generation; console.log("脚本:进入结果页面,准备进行高保真学习。"); const previousExamKeys = debugKeysDB.get(); if (previousExamKeys.length > 0) { console.log("--- 考试页Key摘要 (用于对比) ---"); console.table(previousExamKeys); console.log("---------------------------------"); debugKeysDB.clear(); } const isPassed = !!document.querySelector('.tips_text')?.innerText.includes('考试通过'); if (isPassed) { console.log("考试通过!准备处理下一个课程..."); console.log("正在清除本次考试的答案库(适配不同考试)..."); db.clear(correctAnswersDB.key); wrongAttemptsDB.clear(); questionOptionMapDB.clear(); const cwidMatch = window.location.href.match(/cwid=([a-f0-9-]+)/); if (cwidMatch) { const completedCwid = cwidMatch[1]; let queue = examQueueDB.get(); queue = queue.filter(exam => exam.cwid.toLowerCase() !== completedCwid.toLowerCase()); examQueueDB.set(queue); } await sleep(1000); if (!stillRunning()) return; processNextInQueue(); } else { console.log("考试未通过。正在更新智能题库并准备重试..."); const attempt = questionOptionMapDB.get(); if (!/未通过|没有通过|不及格|未合格|不合格|考试失败/.test(textOf(document.querySelector('.tips_text')))) { stopExam('尚未识别到明确的考试结果。'); return; } if (attempt?.version !== 12 || !attempt.submitted || attempt.courseId !== currentCourseId() || !attempt.questions?.length) { stopExam('没有当前课件的本次提交记录,请从考试页重新启动。'); return; } const correctAnswers = correctAnswersDB.get(); const wrongAttempts = wrongAttemptsDB.get(); const feedback = [], seen = new Set(); for (const item of document.querySelectorAll('.state_cour_ul .state_cour_lis')) { const texts = item.querySelectorAll('.state_lis_text'); const stem = normalizeQuestion(texts[0]?.title || textOf(texts[0])); const matches = attempt.questions.filter(q => q.stem === stem); const source = item.querySelector('.state_error')?.getAttribute('src'); if (matches.length !== 1 || !texts[1] || !source || seen.has(matches[0]?.key)) { stopExam('结果页题目或正误标记无法与本次提交唯一对应,未更新题库。'); return; } const question = matches[0]; const raw = textOf(texts[1]).normalize('NFKC').trim() .replace(/^【?\s*您的答案\s*:\s*/, '').replace(/】\s*$/, '').trim(); // 字母仅用于解释刚提交的那一份试卷;下一次作答只认内容。 const plain = raw.replace(/\s+/g, ''); const answer = question.options.find(o => o.content === plain) || (/^[A-Z]$/i.test(raw) ? question.options.find(o => o.letter === raw.toUpperCase()) : question.options.find(o => o.content === normalizeOption(raw))); if (!answer || answer.content !== question.selectedContent) { stopExam('结果页答案与本次实际提交不一致,未更新题库。'); return; } seen.add(question.key); feedback.push({ questionKey: question.key, content: answer.content, isCorrect: /(?:^|\/)bar_img\.png(?:[?#]|$)/i.test(source) }); } if (feedback.length !== attempt.questions.length) { stopExam('结果页反馈不完整,未更新题库。'); return; } // 全卷核对成功后一次性更新,避免半张试卷的错误映射污染题库。 for (const { questionKey, content, isCorrect } of feedback) { const knowledgeBit = { content }; if (isCorrect) { correctAnswers[questionKey] = [knowledgeBit]; if (wrongAttempts[questionKey]) { wrongAttempts[questionKey] = wrongAttempts[questionKey].filter(k => k.content !== knowledgeBit.content); if (wrongAttempts[questionKey].length === 0) delete wrongAttempts[questionKey]; } } else { if (correctAnswers[questionKey]) { correctAnswers[questionKey] = correctAnswers[questionKey].filter(k => k.content !== content); if (!correctAnswers[questionKey].length) delete correctAnswers[questionKey]; } if (!wrongAttempts[questionKey]) wrongAttempts[questionKey] = []; if (!wrongAttempts[questionKey].some(k => k.content === knowledgeBit.content)) { wrongAttempts[questionKey].push(knowledgeBit); } } } correctAnswersDB.set(correctAnswers); wrongAttemptsDB.set(wrongAttempts); await sleep(getRandomDelay(BASE_RETRY_DELAY_MS, 2000)); if (!stillRunning()) return; const retryButton = Array.from(document.querySelectorAll('input.state_foot_btn')).find(btn => btn.value === '重新考试'); if(retryButton) retryButton.click(); else console.error("找不到“重新考试”按钮!"); } } function processNextInQueue() { if (!getScriptState()) return; const project = db.load(PROJECT_CONTEXT_KEY, null); if (!project?.cid || project.url !== returnUrlDB.get()) { setScriptState(false); updatePanel(); alert('本次考试已处理。请返回目标项目列表重新启动,以免使用旧项目队列。'); return; } const queue = examQueueDB.get(); if (queue.length > 0) { const nextExam = queue[0]; console.log(`脚本:正在导航到下一个考试,课程名: ${nextExam.name}`); window.location.href = `/pages/exam.aspx?cwid=${nextExam.cwid}`; } else { console.log("队列已清空!所有考试均已完成!"); const returnUrl = returnUrlDB.get(); examQueueDB.clear(); returnUrlDB.clear(); db.clear(PROJECT_CONTEXT_KEY); wrongAttemptsDB.clear(); questionOptionMapDB.clear(); setScriptState(false); alert("所有待考课程均已完成!脚本已停止。即将返回课程列表页。"); setTimeout(() => { if (returnUrl) window.location.href = returnUrl; }, 2000); } } // --- UI 控制面板 --- function setupUI() { if (document.getElementById('exam-helper-panel')) return; const panel = document.createElement('div'); panel.id = 'exam-helper-panel'; document.body.appendChild(panel); const dbViewer = document.createElement('div'); dbViewer.id = 'db-viewer-panel'; dbViewer.style.display = 'none'; dbViewer.innerHTML = `
队列为空
'; if (remainingQueue.length > 0) queueHtml = '脚本状态: ${isRunning ? '运行中' : '已停止'}
当前操作: ${currentExam}
待考列表:${JSON.stringify(correct, null, 2)}${JSON.stringify(wrong, null, 2)}`;
viewer.style.display = 'flex';
}
function getScriptState() { return db.load(SCRIPT_STATE_KEY, false); }
function setScriptState(isRunning) { executionGeneration++; db.save(SCRIPT_STATE_KEY, isRunning); }
function toggleScript() {
const currentState = getScriptState(); setScriptState(!currentState);
if (!currentState) main(); else console.log("脚本已由用户暂停。");
updatePanel();
}
function main() {
setupUI();
if (!getScriptState()) {
console.log("脚本当前为停止状态,请在课程列表页点击【开始自动考试】按钮启动。");
return;
}
const urlPath = window.location.pathname;
if (urlPath.includes(COURSE_LIST_URL_FRAGMENT)) handleCourseListPage();
else if (urlPath.includes('/exam.aspx')) handleExamPage();
else if (urlPath.includes('/exam_result.aspx')) handleResultPage();
}
main();
})();