// ==UserScript== // @name 试题文本提取弹窗展示 // @namespace http://tampermonkey.net/ // @version 1.1 // @description 提取考试题目与选项文字内容,弹窗展示并可复制 // @match *://*.zhihuishu.com/* // @grant GM_addStyle // @license 俄23423 // @downloadURL https://update.greasyfork.icu/scripts/533402/%E8%AF%95%E9%A2%98%E6%96%87%E6%9C%AC%E6%8F%90%E5%8F%96%E5%BC%B9%E7%AA%97%E5%B1%95%E7%A4%BA.user.js // @updateURL https://update.greasyfork.icu/scripts/533402/%E8%AF%95%E9%A2%98%E6%96%87%E6%9C%AC%E6%8F%90%E5%8F%96%E5%BC%B9%E7%AA%97%E5%B1%95%E7%A4%BA.meta.js // ==/UserScript== (function () { 'use strict'; // 添加按钮样式 GM_addStyle(` #extractBtn { position: fixed; bottom: 30px; right: 30px; z-index: 99999; background: #4caf50; color: white; border: none; padding: 10px 14px; border-radius: 6px; font-size: 14px; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,0.2); } #popupContainer { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 99999; background: white; padding: 20px; width: 600px; max-height: 80vh; overflow-y: auto; border-radius: 10px; box-shadow: 0 2px 20px rgba(0,0,0,0.3); display: none; } #popupContainer pre { white-space: pre-wrap; word-wrap: break-word; font-size: 14px; } #popupContainer button { margin-top: 10px; padding: 6px 12px; background: #1976d2; color: white; border: none; border-radius: 4px; cursor: pointer; } #popupContainer .close-btn { position: absolute; top: 10px; right: 15px; font-weight: bold; cursor: pointer; color: #aaa; } #popupContainer .close-btn:hover { color: #000; } `); function extractTextFromElement(element) { return element?.innerText?.trim().replace(/\s+/g, ' ') || ''; } function extractAllQuestions() { const questions = document.querySelectorAll('.examPaper_subject'); const result = []; questions.forEach((q, index) => { const questionObj = {}; // 题号 const num = q.querySelector('.subject_num span'); questionObj.number = extractTextFromElement(num) || `Q${index + 1}`; // 题型 const type = q.querySelector('.subject_type span:first-child'); questionObj.type = extractTextFromElement(type); // 分数 const score = q.querySelector('.subject_type span:last-child'); questionObj.score = extractTextFromElement(score); // 题干 const stem = q.querySelector('.subject_describe'); questionObj.question = extractTextFromElement(stem); // 选项 questionObj.options = []; const opts = q.querySelectorAll('.node_detail'); opts.forEach(opt => { const label = opt.closest('.label')?.querySelector('.ABCase, .mr10')?.innerText?.trim(); const text = extractTextFromElement(opt); if (label) { questionObj.options.push(`${label} ${text}`); } }); result.push(questionObj); }); return result; } function formatQuestions(questions) { return questions.map(q => { const options = q.options.map(opt => opt).join('\n'); return `【${q.number}】${q.type} ${q.score}\n题干:${q.question}\n${options}\n`; }).join('\n----------------------\n'); } function createPopup(content) { const popup = document.createElement('div'); popup.id = 'popupContainer'; popup.innerHTML = `
×
${content}
`; document.body.appendChild(popup); // 关闭按钮 popup.querySelector('.close-btn').addEventListener('click', () => { popup.style.display = 'none'; }); // 复制按钮 popup.querySelector('#copyBtn').addEventListener('click', () => { const text = popup.querySelector('#popupText').innerText; navigator.clipboard.writeText(text).then(() => { alert('复制成功!'); }); }); } function createExtractButton() { const btn = document.createElement('button'); btn.id = 'extractBtn'; btn.textContent = '提取题目'; document.body.appendChild(btn); btn.addEventListener('click', () => { const questions = extractAllQuestions(); const formatted = formatQuestions(questions); const popup = document.getElementById('popupContainer'); if (popup) { popup.querySelector('#popupText').innerText = formatted; popup.style.display = 'block'; } else { createPopup(formatted); document.getElementById('popupContainer').style.display = 'block'; } }); } // 延时初始化(避免页面还在加载) window.addEventListener('load', () => { setTimeout(() => { createExtractButton(); }, 1000); }); })();