// ==UserScript== // @name 超星编程题小助手 // @namespace http://tampermonkey.net/ // @version 8.8.8.8 // @description 但知夕阳无限好,何必惆怅近黄昏 // @author sure // @match *://*.chaoxing.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 防重复标记,避免界面错乱 const MARKER = 'data-paste-tool-added'; // 防抖,避免频繁执行 function debounce(func, delay = 300) { let timer = null; return (...args) => { clearTimeout(timer); timer = setTimeout(() => func.apply(this, args), delay); }; } // 精准匹配超星代码编辑器容器 function getCodeContainers() { return document.querySelectorAll('div.EidtBox.marBom20.codeEditorBoxDiv'); } // 给单个编辑器添加粘贴工具(仅一次) function addPasteTool(container) { if (container.hasAttribute(MARKER)) return; container.setAttribute(MARKER, 'true'); // 创建工具容器(极简样式,不影响原界面) const toolWrap = document.createElement('div'); toolWrap.style.margin = '10px 0 0 0'; toolWrap.style.padding = '8px'; toolWrap.style.border = '1px solid #eee'; toolWrap.style.borderRadius = '4px'; toolWrap.style.background = '#fafafa'; toolWrap.style.fontSize = '13px'; // 粘贴文本区 const pasteArea = document.createElement('textarea'); pasteArea.placeholder = '粘贴代码后点击下方按钮写入'; pasteArea.style.width = '100%'; pasteArea.style.height = '80px'; pasteArea.style.marginBottom = '6px'; pasteArea.style.padding = '6px'; pasteArea.style.border = '1px solid #ddd'; pasteArea.style.borderRadius = '4px'; pasteArea.style.fontFamily = 'monospace'; pasteArea.style.fontSize = '12px'; // 写入按钮(关键:设置type="button",避免触发表单提交) const writeBtn = document.createElement('button'); writeBtn.type = 'button'; // 核心修复:设置为普通按钮,不是submit writeBtn.textContent = '写入编辑器'; writeBtn.style.padding = '4px 12px'; writeBtn.style.background = '#666'; writeBtn.style.color = '#fff'; writeBtn.style.border = 'none'; writeBtn.style.borderRadius = '3px'; writeBtn.style.cursor = 'pointer'; writeBtn.style.fontSize = '12px'; // 点击事件:阻止默认行为,双重保险 writeBtn.onclick = (event) => { // 阻止任何默认行为(比如表单提交、页面跳转) event.preventDefault(); event.stopPropagation(); const code = pasteArea.value; if (!code.trim()) return; try { // 优先写入CodeMirror(核心逻辑,不弹窗不focus) const cmEl = container.querySelector('.CodeMirror'); const cm = cmEl?.CodeMirror; const hiddenTextarea = container.querySelector('textarea.code-editor'); if (cm) { cm.setValue(code); cm.save(); // 同步到后台,不触发focus } else if (hiddenTextarea) { hiddenTextarea.value = code; hiddenTextarea.dispatchEvent(new Event('input', { bubbles: true })); hiddenTextarea.dispatchEvent(new Event('change', { bubbles: true })); } // 不做任何额外操作,不弹窗、不滚动、不修改界面 } catch (e) { console.error('写入失败:', e); // 仅在控制台打印错误,不弹任何提示 } }; // 组装并插入到编辑器下方,不影响原布局 toolWrap.appendChild(pasteArea); toolWrap.appendChild(writeBtn); container.parentNode.insertBefore(toolWrap, container.nextSibling); } // 初始化工具 const init = debounce(() => { const containers = getCodeContainers(); containers.forEach(container => addPasteTool(container)); }, 500); // 页面加载/切换题目时初始化 window.addEventListener('load', init); const observer = new MutationObserver(debounce(init, 800)); observer.observe(document.body, { childList: true, subtree: true }); })();