// ==UserScript== // @name 洛谷专栏 Markdown 快速复制 (标题按钮合一版) // @namespace http://tampermonkey.net/ // @version 12.0 // @description 将大标题直接重构成一个巨大的复制按钮,完美保留标题文本,100% 成功率 // @author Gemini // @match https://*.luogu.com.cn/article/* // @match https://www.luogu.com.cn/article/* // @grant GM_setClipboard // @run-at document-end // ==/UserScript== (function() { 'use strict'; function secureReplace() { // 1. 定位页面上的大标题 const h1 = document.querySelector('h1'); // 如果找不到标题,或者已经被我们接管了,就退出 if (!h1 || h1.hasAttribute('data-md-final-injected')) return; // 2. 深度扫描内置 JSON 提取原始 Markdown 源码 let markdownContent = null; try { const contextTag = document.getElementById('lentille-context'); if (contextTag) { const contextData = JSON.parse(contextTag.textContent); // 深度递归探测器 function deepSearch(obj) { if (!obj || typeof obj !== 'object') return; if (obj.content && typeof obj.content === 'string' && obj.content.length > 50) { markdownContent = obj.content; return; } for (let key in obj) { if (markdownContent) return; deepSearch(obj[key]); } } deepSearch(contextData); } } catch (e) { console.error("解析数据失败:", e); } // 如果还没捞到数据,说明页面可能在加载中,先不急着替换 if (!markdownContent) return; // 3. 捞出原本的标题文字(例如 "题解 CF241E Flights - 洛谷专栏") // 排除掉我们可能已经改过的痕迹,只抓取最原始的文本 const originalTitleText = h1.innerText; // 打上最终标记,防止 MutationObserver 死循环 h1.setAttribute('data-md-final-injected', 'true'); // 4. 开始暴力重构大标题的外观 h1.style.backgroundColor = '#52c41a'; // 采用 AC 绿色,非常显眼 h1.style.color = '#fff'; h1.style.padding = '12px 20px'; h1.style.borderRadius = '6px'; h1.style.cursor = 'pointer'; h1.style.fontSize = '20px'; h1.style.fontWeight = 'bold'; h1.style.boxShadow = '0 3px 6px rgba(0,0,0,0.1)'; h1.style.transition = 'all 0.2s'; // 5. 把原本的标题文本和提示文字一起写进大盒子里 h1.innerText = `📄 ${originalTitleText} 【点击一键复制 Markdown 源码】`; // 6. 绑定整行点击复制事件 h1.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); try { GM_setClipboard(markdownContent, 'text'); h1.innerText = '✓ Markdown 源码已成功复制到剪贴板!'; h1.style.backgroundColor = '#13c2c2'; // 成功后短暂变青色 setTimeout(() => { h1.innerText = `📄 ${originalTitleText} 【点击一键复制 Markdown 源码】`; h1.style.backgroundColor = '#52c41a'; }, 1500); } catch (err) { alert('复制失败,请重试'); } }); // 鼠标悬浮特效 h1.addEventListener('mouseenter', () => { h1.style.opacity = '0.9'; }); h1.addEventListener('mouseleave', () => { h1.style.opacity = '1.0'; }); } // 强力网页渲染监控 const observer = new MutationObserver(secureReplace); observer.observe(document.body, { childList: true, subtree: true }); // 梯度触发延迟,确保万无一失 setTimeout(secureReplace, 300); setTimeout(secureReplace, 1000); })();
Note.ms
/yinqf