diff --git a/qingshuxuetang_homework_answer.user.js b/qingshuxuetang_homework_answer.user.js index 9b33af0..d7472d0 100644 --- a/qingshuxuetang_homework_answer.user.js +++ b/qingshuxuetang_homework_answer.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 平顶山学院(青书学堂)作业答案助手 // @namespace http://tampermonkey.net/ -// @version 2026-06-05.1 +// @version 2026-06-05.2 // @description 在青书学堂作业页面展示官方答案,缺少官方答案时使用 DeepSeek 生成参考答案 // @author JK // @license MIT @@ -232,6 +232,44 @@ }; }; + const htmlToText = (html = "") => { + const element = document.createElement("div"); + element.innerHTML = html; + + return element.textContent.trim(); + }; + + const parseAnswerLabels = (solution = "") => { + const text = htmlToText(solution).toUpperCase(); + const labels = text.match(/[A-Z]/g) || []; + + return Array.from(new Set(labels)); + }; + + const formatOfficialAnswer = (questionData) => { + const solution = questionData.solution || ""; + const answerLabels = parseAnswerLabels(solution); + + if (answerLabels.length === 0 || !questionData.options?.length) { + return htmlToText(solution) || solution; + } + + const optionMap = new Map( + questionData.options.map((option) => [ + String(option.label || "").trim().toUpperCase(), + htmlToText(option.description), + ]) + ); + + const matchedAnswers = answerLabels.map((label) => { + const optionText = optionMap.get(label); + + return optionText ? label + ". " + optionText : label; + }); + + return matchedAnswers.join("\n"); + }; + const getQuestionTypeName = (typeId) => { if (typeId === 1) { return "单选题"; @@ -375,7 +413,11 @@ if (questionData.solution) { questionElement.appendChild( - createAnswerDisplay("官方答案", questionData.solution, "official") + createAnswerDisplay( + "官方答案", + formatOfficialAnswer(questionData), + "official" + ) ); continue; }