// ==UserScript== // @name 超星通用题目提取器 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 从超星“查看已批阅作业”或类似考试回顾页面提取题目。支持自动提取和手动提取,带自定义提示。 // @author 毫厘 // @match *://*.chaoxing.com/exam-ans/exam/test/reVersionPaperMarkContentNew* // @match *://*.chaoxing.com/mooc-ans/work/selectWorkQuestionYiPiYue* // @grant GM_setClipboard // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // ==/UserScript== (function() { // 使用立即执行函数表达式 (IIFE) 封装代码,避免污染全局作用域 'use strict'; // 启用严格模式,有助于捕获常见错误 // 用于存储自动提取设置的键名,确保版本更新后键名唯一性以避免冲突 const AUTO_EXTRACT_KEY = 'chaoxingUniversalQuizAutoExtractEnabled_v3.0'; // --- 辅助函数 (Combined and refined) --- /** * 规范化文本:移除多余空格,并将所有空白符(包括换行、制表符等)替换为单个空格,最后去除首尾空格。 * @param {string} text - 待处理的原始文本。 * @returns {string} 规范化后的文本。如果输入不是字符串,则返回空字符串。 */ function normalizeText(text) { if (typeof text !== 'string') return ""; return text.replace(/\s+/g, ' ').trim(); } /** * 规范化题目文本: * 1. 移除题目前缀,如 "【单选题】"。 * 2. 移除题目末尾的分数提示,如 "(2.0 分)" 或 "(2 分)"。 * 3. 标准化空括号,如将 "( )" 或 "( )" 统一为 "( )" 或 "( )"。 * 4. 进行基础的 normalizeText 处理。 * @param {string} rawText - 原始的题目文本。 * @returns {string} 规范化后的题目文本。 */ function normalizeQuestionText(rawText) { if (!rawText) return ""; // 移除题型前缀,例如 "【单选题】 " let text = rawText.replace(/^【.*?题】\s*/, '').trim(); // 移除题干末尾的分数部分,例如 "( 2.0 分 )" 或 "( 2 分 )" text = text.replace(/\s*(\s*\d+(\.\d+)?\s*分\s*)\s*$/, '').trim(); text = text.replace(/\s*\(\s*\d+(\.\d+)?\s*分\s*\)\s*$/, '').trim(); // 将内容为空或只有空格/ 的括号标准化 text = text.replace(/\(\s*( |\s)*\)/g, '( )'); // 半角括号 text = text.replace(/(\s*( |\s)*)/g, '( )'); // 全角括号 // 再次确保完全空的括号被标准化 text = text.replace(/\(\s*\)/g, '( )').replace(/(\s*)/g, '( )'); return normalizeText(text); // 最后进行通用文本规范化 } /** * 从 DOM 元素中提取纯文本内容。 * 该函数会克隆元素,移除其中的