// ==UserScript==
// @name 课程作业自动答题 + 点击交卷(仅一次)
// @namespace http://tampermonkey.net/
// @version 0.0.1
// @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;
}
// 只点击一次交卷按钮,不处理弹窗
let submitted = false; // 确保只执行一次
function clickSubmitOnce() {
if (submitted) return;
const $btn = $('#btn_submit');
if (!$btn.length) {
log('❌ 未找到交卷按钮');
return;
}
submitted = true;
log('点击交卷按钮(仅一次)');
$btn.click();
$btn[0].dispatchEvent(new Event('click', { bubbles: true }));
log('交卷按钮已点击,请手动确认弹窗(如有)');
}
// 入口:自动答题,然后点击交卷
let autoExecuted = false;
function run() {
if (autoExecuted) return;
autoExecuted = true;
log('自动流程启动...');
setTimeout(() => {
autoAnswer();
// 答题完成后延迟 1.5 秒再点击交卷
setTimeout(() => {
clickSubmitOnce();
}, 1500);
}, 1000);
}
run();
log('脚本已加载,将自动答题并点击一次交卷按钮');
});
})();