// ==UserScript==
// @name 课程作业自动答题(从解析获取答案)
// @namespace http://tampermonkey.net/
// @version 0.0.4
// @description 自动提取解析中的答案并勾选,支持一键交卷
// @match https://kc.jxjypt.cn/paper/start/* // 请根据实际考试页面URL修改匹配规则
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// 等待页面加载完成(确保jQuery可用)
function waitForJQuery(callback) {
if (window.jQuery) {
callback(window.jQuery);
} else {
setTimeout(() => waitForJQuery(callback), 100);
}
}
waitForJQuery(function($) {
// 创建浮动控制面板
const panel = $(`
`).appendTo('body');
// 日志输出
function log(msg) {
$('#answerLog').prepend(`${new Date().toLocaleTimeString()} - ${msg}
`);
if ($('#answerLog div').length > 20) $('#answerLog div:last').remove();
}
// 从解析区域获取答案字母
function getAnswerFromSolution($question) {
const $solution = $question.find('.solution');
if (!$solution.length) return null;
// 答案示例:C
const answerText = $solution.find('.right').text().trim();
if (!answerText) return null;
// 匹配 A、B、C、D 等单个字母(支持可能的多字母,但只取第一个)
const match = answerText.match(/[A-D]/i);
return match ? match[0].toUpperCase() : null;
}
// 自动答题主函数
function autoAnswer() {
log('开始扫描题目...');
let successCount = 0;
let failCount = 0;
// 遍历所有题目区块
$('.m-question').each(function(index) {
const $q = $(this);
const questionText = $q.find('.sub-dotitle pre').text().trim();
const answerLetter = getAnswerFromSolution($q);
if (!answerLetter) {
log(`❌ 第${index+1}题 未找到答案(题目:${questionText.substring(0,20)}...)`);
failCount++;
return;
}
// 查找对应选项的 dd 元素(data-value 为 A B C D)
const $targetOption = $q.find(`dd[data-value="${answerLetter}"]`);
if (!$targetOption.length) {
log(`⚠️ 第${index+1}题 答案 ${answerLetter} 但无对应选项(题目:${questionText.substring(0,20)}...)`);
failCount++;
return;
}
// 模拟点击选项(触发 click 事件)
$targetOption.click();
// 同时触发原生事件以确保可能的事件监听
$targetOption[0].dispatchEvent(new Event('click', { bubbles: true }));
log(`✅ 第${index+1}题 已勾选答案 ${answerLetter}(${questionText.substring(0,30)})`);
successCount++;
});
log(`答题完成!成功 ${successCount} 题,失败 ${failCount} 题。`);
// 刷新答题卡显示(页面自带函数可能更新计数,但无需额外处理)
}
// 交卷
function submitPaper() {
const $submitBtn = $('#btn_submit');
if ($submitBtn.length) {
log('正在交卷...');
$submitBtn.click();
$submitBtn[0].dispatchEvent(new Event('click', { bubbles: true }));
// 有些交卷可能会有二次确认弹窗,这里简单处理,如需自动确认可添加定时器
setTimeout(() => {
// 尝试处理可能的确认对话框
const confirmBtn = $('.layui-layer-btn0:contains("确定")');
if (confirmBtn.length) confirmBtn.click();
}, 500);
} else {
log('❌ 未找到交卷按钮(#btn_submit)');
}
}
// 绑定按钮事件
$('#startAnswerBtn').on('click', autoAnswer);
$('#submitPaperBtn').on('click', submitPaper);
log('脚本已加载,点击“开始答题”自动勾选答案,“交卷”按钮手动提交。');
});
})();