// ==UserScript== // @name 华医网考试助手 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 自动填写华医网考试表单 // @author You // @match *cme28.91huayi.com/pages/exam* // @grant GM_setValue // @grant GM_deleteValue // @grant GM_getValue // ==/UserScript== (function() { 'use strict'; window.addEventListener('load', function() { 'use strict'; //GM_deleteValue('savedQuestions') // 去除题目中的数字前缀(如 "1、") const cleanQuestion = (text) => text.trim().replace(/^\d+、/, ''); // 去除选项中的字母前缀(如 "D、") const cleanOption = (text) => text.trim().replace(/^[A-E]、/, ''); // 考试页面逻辑 if (window.location.pathname === '/pages/exam.aspx') { const savedQuestions = JSON.parse(GM_getValue('savedQuestions') || '[]'); const questionTables = document.querySelectorAll('.tablestyle'); questionTables.forEach(table => { const thead = table.querySelector('thead'); if (!thead) { console.warn('未找到题目标题元素'); return; } const questionText = cleanQuestion(thead.textContent); const labels = table.querySelectorAll('tbody label'); const options = Array.from(labels).map((label, index) => ({ text: cleanOption(label.textContent), status: '未选过', // 默认状态 element: label })); // 检查是否已存在相同的题目 const existingQuestion = savedQuestions.find(q => q.questionText.trim() === questionText.trim() && q.optionText.length === options.length && q.optionText.every((opt, i) => opt.text.trim() === options[i].text.trim())); if (existingQuestion) { // 更新选项状态(跳过已选过或正确答案) options.forEach((opt, index) => { if (existingQuestion.optionText[index] && opt.status === '未选过') { opt.status = existingQuestion.optionText[index].status; } }); } // 优先选择正确答案,否则随机选择未选过的选项 const correctOptions = options.filter(opt => opt.status === '正确答案'); if (correctOptions.length > 0) { // 选择第一个正确答案 const selectedOption = correctOptions[0]; selectedOption.element.click(); console.log(`选中正确答案: ${selectedOption.text}`); } else { // 随机选择一个未选过的选项 const unselectedOptions = options.filter(opt => opt.status === '未选过'); if (unselectedOptions.length > 0) { const randomIndex = Math.floor(Math.random() * unselectedOptions.length); const selectedOption = unselectedOptions[randomIndex]; selectedOption.element.click(); selectedOption.status = '已选过'; console.log(`随机选中选项: ${selectedOption.text}`); } } // 保存题目和选项(含状态) const updatedQuestion = { questionText, optionText: options.map(opt => ({ text: opt.text, status: opt.status })) }; if (!existingQuestion) { savedQuestions.push(updatedQuestion); } else { Object.assign(existingQuestion, updatedQuestion); } GM_setValue('savedQuestions', JSON.stringify(savedQuestions)); // 打印题目和选项(含状态) //console.log(`题目: ${questionText}`); options.forEach((opt, index) => console.log(`选项${index + 1}: ${opt.text} (状态: ${opt.status})`)); }); // 打印所有保存的内容 console.log('保存的题目和选项:', JSON.stringify(savedQuestions, null, 2)); // 随机10秒到15秒后点击交卷按钮 const submitButton = document.getElementById('btn_submit'); if (submitButton) { const delay = Math.floor(Math.random() * 5000) + 10000; // 10秒到15秒之间的随机延迟 console.log(`将在${delay / 1000}秒后点击交卷按钮`); setTimeout(() => { submitButton.click(); console.log('已点击交卷按钮'); }, delay); } else { console.log('未找到交卷按钮'); } } // 考试结果页面逻辑 if (window.location.pathname === '/pages/exam_result.aspx') { console.log('当前为考试结果页面'); const savedQuestions = JSON.parse(GM_getValue('savedQuestions') || '[]'); //console.log('老问题:'+ savedQuestions); const questionElements = document.querySelectorAll('.state_cour_lis'); questionElements.forEach(element => { //console.log(element.textContent); const stateLisTexts = element.querySelectorAll('.state_lis_text'); const questionTitle = stateLisTexts[0];// 第一个 .state_lis_text 是题目 const selectedOption = stateLisTexts[1].innerText;// 第二个 .state_lis_text 是选项 const questionText = cleanQuestion(questionTitle.innerText); const selectedOptionText = cleanOption(selectedOption.slice(6, -1)); console.log('题目是:'+ questionText.replace(/(\s*)/g, ''),'选项是:' + selectedOptionText); const isCorrect = element.querySelector('img[src*="bar_img.png"]') !== null; //console.log('找到的图标'+isCorrect); if (isCorrect) { //console.log('老问题:' + savedQuestions[0].questionText,'新问题:'+questionText); //console.log('保存的题目列表:', savedQuestions.map(q => q.questionText.replace(/^\s*[()\[\]{}]*\s*|\s*[()\[\]{}]*\s*$/g, ''))); const existingQuestion = savedQuestions.find(q => q.questionText.replace(/(\s*)/g, '') === questionText.replace(/(\s*)/g, '') ); console.log('相同答案:' + existingQuestion); if (existingQuestion) { // 更新选项状态 existingQuestion.optionText.forEach(opt => { console.log('找到的选项:' + opt.text); if (opt.text === selectedOptionText) { opt.status = '正确答案'; } }); } } }); // 更新保存的题目数据 GM_setValue('savedQuestions', JSON.stringify(savedQuestions)); console.log('已更新保存的题目数据:',GM_getValue('savedQuestions')); // 随机5秒到10秒后点击重新考试按钮 const retryButtons = document.querySelectorAll('.state_foot_btn.state_edu'); if (retryButtons.length >= 2) { const retryButton = retryButtons[1]; // 第二个按钮 const delay = Math.floor(Math.random() * 5000) + 5000; // 5秒到10秒之间的随机延迟 console.log(`将在${delay / 1000}秒后点击重新考试按钮`); setTimeout(() => { retryButton.click(); console.log('已点击重新考试按钮'); }, delay); } else { console.log('未找到重新考试按钮'); } } }) })();