版本比较
洛谷美化油猴插件
正文
增加 305 行删除 223 行
1234356567891011121314151615161718191820212223202122232424252627282627282930313233343536373839404142434445464729303149505152535455323334353657373839404142434445464748495051525354555657585960616263646559606162636667686965666768707172737475767778798081828384858687707172737475767778798889909192939495969798818283848586991001011021031041051061071081091101111121138889909192114115116949596979899100101117118119120121122123124103104105106107108125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190111112113114191192193194195196197198199116117118119200201202203204205206207208209210211212213214215216217218219220221222121122123124125126223224225226227228229230128129130231232233234235236237132133134135136137138139140238239240241242142143144145146243244245246247248249250251252253148149150151152254255256257258259260154155156157261262263264265266159160161162163164165166167168169267268269270271272273274275171172173174175176177276277278279280281282283179180284285286287288182289290291292293184185186187188189190191192193194195294295296297298299300197198199200301302303304202203204205206207208209210211212213214215216217305306307308309310311312313314219220221315316317318319320223224225226227228229230321322323324325326327328329232233234235236237238239330331332333334335336337338339340341342243244245246247343344345346347348349249250350252253254255256257258259260261262263264265266267351352353269270271354355隐藏**题号** & **难度**。
```// ==UserScript==// @name Luogu 难度隐藏开关// @name Luogu 题号+难度 整合工具// @namespace http://tampermonkey.net/// @version V1.3// @description Luogu 题目难度标签隐藏/悬停显示/始终显示,浮动按钮可拖动、记忆位置、防止拖出屏幕、拖动时半透明。// @version 1.2// @description 题号切换+难度隐藏 整合版,主页面隐藏难度按钮,题库页不干扰// @author LTTXiaochuan// @match https://www.luogu.com.cn/*// @grant GM_getValue// @grant GM_setValue// @grant GM_registerMenuCommand// @run-at document-idle// ==/UserScript==
(function () { 'use strict';(function() { 'use strict';
const DEFAULT_MODE = GM_getValue('lg_diff_mode', 'hover'); // ====================== 工具函数:页面判断 ====================== const path = location.pathname; const searchParams = new URLSearchParams(location.search);
const selectors = [ '.l-flex-info-row a[href*="problem/list?difficulty"] span', '.difficulty span span', '.difficulty span.lfe-caption' ]; // 1. 仅匹配:/problem/Pxxxx 或 /contest/xxx/problem/xxx,排除 /problem/list 等题库页 const isProblemDetailPage = (path.startsWith('/problem/') && !path.includes('/list')) || (path.includes('/contest/') && path.includes('/problem/'));
const styleId = 'tm-luogu-diff-style'; const css = ` .tm-diff-hidden, .tm-diff-hover { transition: opacity .15s ease !important; } .tm-diff-hidden { opacity: 0 !important; } .tm-diff-hover { opacity: 0 !important; } .tm-diff-hover:hover { opacity: 1 !important; } #tm-luogu-diff-toggle { position: fixed; top: 12px; right: 12px; z-index: 999999; background: rgba(0,0,0,0.6); color: #fff !important; padding: 6px 8px; font-size: 12px; border-radius: 6px; cursor: move; user-select: none; box-shadow: 0 2px 6px rgba(0,0,0,.4); } #tm-luogu-diff-toggle small { opacity: .8; display:block; font-size:11px; margin-top:2px; color: #fff !important; } `; // 2. 判断是否为洛谷主页面(根路径 / 或 /?xxx) const isHomePage = path === '/' && searchParams.get('pid') === null;
function injectStyle() { if (document.getElementById(styleId)) return; const s = document.createElement('style'); s.id = styleId; s.textContent = css; document.head.appendChild(s); } // ====================== 第一部分:题号切换功能(仅题目详情页执行) ====================== if (isProblemDetailPage) { // 默认隐藏 let mode = GM_getValue('pid_mode', 'hidden');
let currentMode = DEFAULT_MODE; // 核心样式 const css1 = ` #pidBtn { position: fixed; top: 12px; right: 12px; z-index: 999999; background: rgba(0,0,0,0.6); color: #fff !important; padding: 6px 8px; font-size: 12px; border-radius: 6px; cursor: move; user-select: none; box-shadow: 0 2px 6px rgba(0,0,0,.4); min-width: 120px; text-align: center; font-family: 'Segoe UI',微软雅黑,sans-serif; } #pidBtn small { opacity: .8; display:block; font-size:11px; margin-top:2px; color: #fff !important; } /* 隐藏题号 */ body.hidePid .breadcrumb a[href*="P"] { display:none !important; } /* 打印只隐藏按钮,不破坏页面 */ @media print { #pidBtn { display:none !important; } } `; const style1 = document.createElement('style'); style1.textContent = css1; document.head.appendChild(style1);
function applyToElement(el, mode) { el.classList.remove('tm-diff-hidden', 'tm-diff-hover'); if (mode === 'hidden') el.classList.add('tm-diff-hidden'); else if (mode === 'hover') el.classList.add('tm-diff-hover'); } // 执行功能 + 侧栏暴力隐藏 function run() { document.body.classList.toggle('hidePid', mode === 'hidden');
function applyModeToAll(mode) { const combined = selectors.join(','); document.querySelectorAll(combined).forEach(el => applyToElement(el, mode)); } if(mode === 'hidden'){ const all = document.getElementsByTagName('*'); for(let el of all){ if(el.textContent.trim() === '题目编号'){ const val = el.nextElementSibling || el.parentElement.lastElementChild; if(val && val.textContent.startsWith('P')) val.style.display = 'none'; } } }else{ const all = document.getElementsByTagName('*'); for(let el of all){ if(el.textContent.trim() === '题目编号'){ const val = el.nextElementSibling || el.parentElement.lastElementChild; if(val) val.style.display = ''; } } }
let mo = null; function startObserver() { if (mo) return; mo = new MutationObserver(muts => { if (muts.some(m => m.addedNodes && m.addedNodes.length)) { applyModeToAll(currentMode); } }); mo.observe(document.body, { childList: true, subtree: true }); } // 主标题原版逻辑 const h1 = document.querySelector('h1'); if(h1){ if(mode === 'hidden'){ h1._o = h1._o || h1.textContent; h1.textContent = 'LoveFurina'; } else { h1._o && (h1.textContent = h1._o); } } // 文字修改为:隐藏题号/显示题号 + (点击切换) const label = mode === 'hidden' ? '隐藏题号' : '显示题号'; document.getElementById('pidBtn').innerHTML = `<strong>${label}</strong><small>(点击切换)</small>`; }
function saveMode(mode) { currentMode = mode; GM_setValue('lg_diff_mode', mode); updateToggleText(); applyModeToAll(mode); } // 创建按钮 function btn() { const b = document.createElement('div'); b.id = 'pidBtn'; const x=GM_getValue('x'), y=GM_getValue('y'); if(x!=null&&y!=null){ b.style.left=x+'px'; b.style.top=y+'px'; b.style.right='auto'; }else{ b.style.bottom = "65px"; b.style.right = "12px"; b.style.top = "auto"; }
function nextMode(m) { if (m === 'show') return 'hover'; if (m === 'hover') return 'hidden'; return 'show'; } let ox,oy,d=false; let isDragged = false;
function createToggle() { if (document.getElementById('tm-luogu-diff-toggle')) return; const btn = document.createElement('div'); btn.id = 'tm-luogu-diff-toggle'; btn.title = '点击切换难度显示模式'; btn.style.minWidth = '120px'; btn.style.textAlign = 'center'; btn.style.fontFamily = 'Segoe UI,微软雅黑,sans-serif'; b.onmousedown = (e)=>{ d=true; isDragged = false; ox=e.clientX-b.offsetLeft; oy=e.clientY-b.offsetTop; b.style.opacity=0.6; };
const savedX = GM_getValue('lg_diff_x', null); const savedY = GM_getValue('lg_diff_y', null); if (savedX !== null && savedY !== null) { btn.style.left = savedX + 'px'; btn.style.top = savedY + 'px'; btn.style.right = 'auto'; document.onmousemove = (e)=>{ if(!d)return; isDragged = true; b.style.left=Math.max(0,Math.min(innerWidth-b.offsetWidth,e.clientX-ox))+'px'; b.style.top=Math.max(0,Math.min(innerHeight-b.offsetHeight,e.clientY-oy))+'px'; };
document.onmouseup = ()=>{ if(!d)return; d=false; b.style.opacity=1; GM_setValue('x',b.offsetLeft); GM_setValue('y',b.offsetTop); };
b.onclick = ()=>{ if(!isDragged){ mode=mode==='hidden'?'show':'hidden'; GM_setValue('pid_mode',mode); run(); } };
document.body.appendChild(b); run(); }
// 标签页标题(仅题目页执行) const targetTitle = "芙芙超可爱~"; function setFurinaTitle() { if (document.title !== targetTitle) { document.title = targetTitle; } } setInterval(setFurinaTitle, 300);
// 极速响应:轮询间隔50ms(仅题目页执行) setInterval(() => { const h1 = document.querySelector('h1'); if(h1){ if(mode === 'hidden'){ h1._o = h1._o || h1.textContent; h1.textContent = 'LoveFurina'; } else { h1._o && (h1.textContent = h1._o); } } }, 50);
// 极速响应:题号隐藏(仅题目页执行) setInterval(() => { if(mode === 'hidden'){ const all = document.getElementsByTagName('*'); for(let el of all){ if(el.textContent.trim() === '题目编号'){ const val = el.nextElementSibling || el.parentElement.lastElementChild; if(val && val.textContent.startsWith('P')) val.style.display = 'none'; } } } }, 50);
// 启动题号功能(仅题目页执行) window.addEventListener('load', ()=>{ btn(); setTimeout(run,300); setTimeout(run,800); }); }
btn.addEventListener('click', (e) => { if (btn._dragging) return; saveMode(nextMode(currentMode)); }); // ====================== 第二部分:难度隐藏功能(主页面除外,完全不变) ====================== if (!isHomePage) { // 主页面不执行难度功能 const DEFAULT_MODE = GM_getValue('lg_diff_mode', 'hover'); const selectors = [ '.l-flex-info-row a[href*="problem/list?difficulty"] span', '.difficulty span span', '.difficulty span.lfe-caption' ];
makeDraggable(btn); document.body.appendChild(btn); updateToggleText(); } const styleId = 'tm-luogu-diff-style'; const css2 = ` .tm-diff-hidden, .tm-diff-hover { transition: opacity .15s ease !important; } .tm-diff-hidden { opacity: 0 !important; } .tm-diff-hover { opacity: 0 !important; } .tm-diff-hover:hover { opacity: 1 !important; } #tm-luogu-diff-toggle { position: fixed; bottom: 12px; right: 12px; z-index: 999999; background: rgba(0,0,0,0.6); color: #fff !important; padding: 6px 8px; font-size: 12px; border-radius: 6px; cursor: move; user-select: none; box-shadow: 0 2px 6px rgba(0,0,0,.4); } #tm-luogu-diff-toggle small { opacity: .8; display:block; font-size:11px; margin-top:2px; color: #fff !important; } `;
function updateToggleText() { const btn = document.getElementById('tm-luogu-diff-toggle'); if (!btn) return; const label = (currentMode === 'show') ? '显示难度' : (currentMode === 'hover') ? '悬停显示难度' : '隐藏难度'; btn.innerHTML = ` <strong> ${label} </strong> <small> (点击切换) </small> `; } function injectStyle() { if (document.getElementById(styleId)) return; const s = document.createElement('style'); s.id = styleId; s.textContent = css2; document.head.appendChild(s); }
// 拖动逻辑 + 防止拖出屏幕 + 拖动时半透明 function makeDraggable(el) { let offsetX, offsetY, isDown = false; let currentMode = DEFAULT_MODE; function applyToElement(el, mode) { el.classList.remove('tm-diff-hidden', 'tm-diff-hover'); if (mode === 'hidden') el.classList.add('tm-diff-hidden'); else if (mode === 'hover') el.classList.add('tm-diff-hover'); }
el.addEventListener('mousedown', (e) => { isDown = true; el._dragging = false; offsetX = e.clientX - el.offsetLeft; offsetY = e.clientY - el.offsetTop; el.style.opacity = "0.6"; // 拖动时半透明 document.addEventListener('mousemove', move); document.addEventListener('mouseup', up); }); function applyModeToAll(mode) { const combined = selectors.join(','); document.querySelectorAll(combined).forEach(el => applyToElement(el, mode)); }
function move(e) { if (!isDown) return; el._dragging = true; let newLeft = e.clientX - offsetX; let newTop = e.clientY - offsetY; let mo = null; function startObserver() { if (mo) return; mo = new MutationObserver(muts => { if (muts.some(m => m.addedNodes && m.addedNodes.length)) { applyModeToAll(currentMode); } }); mo.observe(document.body, { childList: true, subtree: true }); }
// 限制在可视区域内 const maxLeft = window.innerWidth - el.offsetWidth; const maxTop = window.innerHeight - el.offsetHeight; newLeft = Math.max(0, Math.min(maxLeft, newLeft)); newTop = Math.max(0, Math.min(maxTop, newTop)); function saveMode(mode) { currentMode = mode; GM_setValue('lg_diff_mode', mode); updateToggleText(); applyModeToAll(mode); }
el.style.left = newLeft + 'px'; el.style.top = newTop + 'px'; el.style.right = 'auto'; } function nextMode(m) { if (m === 'show') return 'hover'; if (m === 'hover') return 'hidden'; return 'show'; }
function up() { if (!isDown) return; isDown = false; el.style.opacity = "1"; // 拖动结束恢复 document.removeEventListener('mousemove', move); document.removeEventListener('mouseup', up); GM_setValue('lg_diff_x', el.offsetLeft); GM_setValue('lg_diff_y', el.offsetTop); setTimeout(() => { el._dragging = false; }, 50); } } function createToggle() { if (document.getElementById('tm-luogu-diff-toggle')) return; const btn = document.createElement('div'); btn.id = 'tm-luogu-diff-toggle'; btn.title = '点击切换难度显示模式'; btn.style.minWidth = '120px'; btn.style.textAlign = 'center'; btn.style.fontFamily = 'Segoe UI,微软雅黑,sans-serif';
function init() { injectStyle(); createToggle(); applyModeToAll(currentMode); startObserver(); window.addEventListener('load', () => setTimeout(() => applyModeToAll(currentMode), 300)); } const savedX = GM_getValue('lg_diff_x', null); const savedY = GM_getValue('lg_diff_y', null); if (savedX !== null && savedY !== null) { btn.style.left = savedX + 'px'; btn.style.top = savedY + 'px'; btn.style.right = 'auto'; }
init();})(); btn.addEventListener('click', (e) => { if (btn._dragging) return; saveMode(nextMode(currentMode)); });
``` makeDraggable(btn); document.body.appendChild(btn); updateToggleText(); }
```// ==UserScript==// @name Luogu 题号切换// @namespace http://tampermonkey.net/// @version 1.1// @description 打印正常+默认隐藏题号+侧栏彻底隐藏+可切换// @match https://www.luogu.com.cn/problem/*// @match https://www.luogu.com.cn/contest/*/problem/*// @grant GM_getValue// @grant GM_setValue// @run-at document-idle// ==/UserScript== function updateToggleText() { const btn = document.getElementById('tm-luogu-diff-toggle'); if (!btn) return; const label = (currentMode === 'show') ? '显示难度' : (currentMode === 'hover') ? '悬停显示难度' : '隐藏难度'; btn.innerHTML = ` <strong> ${label} </strong> <small> (点击切换) </small> `; }
(function() { 'use strict'; // 默认隐藏 let mode = GM_getValue('pid_mode', 'hidden'); // 拖动逻辑 function makeDraggable(el) { let offsetX, offsetY, isDown = false;
// 核心样式 const css = ` #pidBtn { position: fixed; top:12px; right:12px; z-index:999999; background:rgba(0,0,0,0.6); color:#fff; padding:6px 8px; border-radius:6px; cursor:move; user-select:none; min-width:120px; text-align:center; } #pidBtn small { opacity:.8; display:block; font-size:11px; margin-top:2px; } /* 隐藏题号 */ body.hidePid .breadcrumb a[href*="P"] { display:none !important; } /* 打印正常 */ @media print { #pidBtn { display:none !important; } * { all:revert !important; } h1 { display:block !important; } } `; const style = document.createElement('style'); style.textContent = css; document.head.appendChild(style); el.addEventListener('mousedown', (e) => { isDown = true; el._dragging = false; offsetX = e.clientX - el.offsetLeft; offsetY = e.clientY - el.offsetTop; el.style.opacity = "0.6"; document.addEventListener('mousemove', move); document.addEventListener('mouseup', up); });
// 执行功能 + 【新增:侧栏暴力隐藏】 function run() { document.body.classList.toggle('hidePid', mode === 'hidden'); function move(e) { if (!isDown) return; el._dragging = true; let newLeft = e.clientX - offsetX; let newTop = e.clientY - offsetY;
// 侧栏题目编号彻底隐藏(比赛页/普通页通用) if(mode === 'hidden'){ const all = document.getElementsByTagName('*'); for(let el of all){ if(el.textContent.trim() === '题目编号'){ const val = el.nextElementSibling || el.parentElement.lastElementChild; if(val && val.textContent.startsWith('P')) val.style.display = 'none'; } const maxLeft = window.innerWidth - el.offsetWidth; const maxTop = window.innerHeight - el.offsetHeight; newLeft = Math.max(0, Math.min(maxLeft, newLeft)); newTop = Math.max(0, Math.min(maxTop, newTop));
el.style.left = newLeft + 'px'; el.style.top = newTop + 'px'; el.style.right = 'auto'; } }else{ // 显示时恢复侧栏 const all = document.getElementsByTagName('*'); for(let el of all){ if(el.textContent.trim() === '题目编号'){ const val = el.nextElementSibling || el.parentElement.lastElementChild; if(val) val.style.display = ''; }
function up() { if (!isDown) return; isDown = false; el.style.opacity = "1"; document.removeEventListener('mousemove', move); document.removeEventListener('mouseup', up); GM_setValue('lg_diff_x', el.offsetLeft); GM_setValue('lg_diff_y', el.offsetTop); setTimeout(() => { el._dragging = false; }, 50); } }
// 主标题 const h1 = document.querySelector('h1'); if(h1){ if(mode === 'hidden'){ h1._o = h1._o || h1.textContent; h1.textContent = 'LoveFurina'; } else { h1._o && (h1.textContent = h1._o); } function init() { injectStyle(); createToggle(); applyModeToAll(currentMode); startObserver(); window.addEventListener('load', () => setTimeout(() => applyModeToAll(currentMode), 300)); } document.getElementById('pidBtn').innerHTML = `<strong>${mode==='hidden'?'已隐藏':'已显示'}</strong><small>点击切换</small>`; }
// 创建按钮 function btn() { const b = document.createElement('div'); b.id = 'pidBtn'; // 记忆位置 const x=GM_getValue('x'), y=GM_getValue('y'); if(x!=null&&y!=null){ b.style.left=x+'px'; b.style.top=y+'px'; b.style.right='auto'; } // 点击切换 b.onclick = ()=>{ mode=mode==='hidden'?'show':'hidden'; GM_setValue('pid_mode',mode); run(); }; // 拖动 let ox,oy,d=false; b.onmousedown = (e)=>{d=true; ox=e.clientX-b.offsetLeft; oy=e.clientY-b.offsetTop; b.style.opacity=0.6;}; document.onmousemove = (e)=>{if(!d)return; b.style.left=Math.max(0,Math.min(innerWidth-b.offsetWidth,e.clientX-ox))+'px'; b.style.top=Math.max(0,Math.min(innerHeight-b.offsetHeight,e.clientY-oy))+'px';}; document.onmouseup = ()=>{if(!d)return; d=false; b.style.opacity=1; GM_setValue('x',b.offsetLeft); GM_setValue('y',b.offsetTop);}; document.body.appendChild(b); run(); // 启动难度功能 init(); }
// 启动 window.onload = ()=>{ btn(); setTimeout(run,300); setTimeout(run,800); };})();```