// ==UserScript==
// @name 课程作业自动答题(从解析获取答案)+ 自动交卷
// @namespace http://tampermonkey.net/
// @version 0.0.8
// @description 自动提取解析中的答案并勾选,自动交卷,仅尝试一次点击确认弹窗
// @match https://kc.jxjypt.cn/paper/start*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
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;
const answerText = $solution.find('.right').text().trim();
if (!answerText) return null;
const match = answerText.match(/[A-D]/i);
return match ? match[0].toUpperCase() : null;
}
// 自动答题
function autoAnswer() {
log('开始扫描题目...');
let success = 0, fail = 0;
$('.m-question').each(function(idx) {
const $q = $(this);
const questionText = $q.find('.sub-dotitle pre').text().trim();
const ans = getAnswerFromSolution($q);
if (!ans) {
log(`❌ 第${idx+1}题 未找到答案`);
fail++;
return;
}
const $opt = $q.find(`dd[data-value="${ans}"]`);
if (!$opt.length) {
log(`⚠️ 第${idx+1}题 答案${ans}无对应选项`);
fail++;
return;
}
$opt.click();
$opt[0].dispatchEvent(new Event('click', { bubbles: true }));
log(`✅ 第${idx+1}题 已选 ${ans}`);
success++;
});
log(`答题完成:成功 ${success},失败 ${fail}`);
return success;
}
// 自动交卷 + 只尝试一次确认弹窗
function autoSubmit() {
const $btn = $('#btn_submit');
if (!$btn.length) {
log('❌ 未找到交卷按钮');
return;
}
log('点击交卷按钮...');
$btn.click();
$btn[0].dispatchEvent(new Event('click', { bubbles: true }));
// 等待弹窗出现,然后只尝试一次点击确定按钮(不轮询)
setTimeout(() => {
const $confirm = $('.layui-layer-btn0:contains("确定"), .layui-layer-btn0:contains("确认")');
if ($confirm.length) {
log('检测到确认弹窗,点击确定');
$confirm.click();
} else {
log('未检测到确认弹窗,可能已直接提交或弹窗选择器不匹配');
}
}, 800); // 延迟800ms给弹窗出现的时间
}
// 入口:自动执行
setTimeout(() => {
log('自动答题流程开始...');
autoAnswer();
setTimeout(() => {
autoSubmit();
}, 1500);
}, 1000);
log('脚本已加载,将自动答题并交卷(仅尝试一次确认弹窗)');
});
})();