代码// ==UserScript==
// @name Luogu 难度隐藏开关
// @namespace http://tampermonkey.net/
// @version V1.3
// @description Luogu 题目难度标签隐藏/悬停显示/始终显示,浮动按钮可拖动、记忆位置、防止拖出屏幕、拖动时半透明。
// @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';
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'
];
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; }
`;
function injectStyle() {
if (document.getElementById(styleId)) return;
const s = document.createElement('style');
s.id = styleId;
s.textContent = css;
document.head.appendChild(s);
}
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');
}
function applyModeToAll(mode) {
const combined = selectors.join(',');
document.querySelectorAll(combined).forEach(el => applyToElement(el, mode));
}
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 });
}
function saveMode(mode) {
currentMode = mode;
GM_setValue('lg_diff_mode', mode);
updateToggleText();
applyModeToAll(mode);
}
function nextMode(m) {
if (m === 'show') return 'hover';
if (m === 'hover') return 'hidden';
return 'show';
}
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';
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';
}
btn.addEventListener('click', (e) => {
if (btn._dragging) return;
saveMode(nextMode(currentMode));
});
makeDraggable(btn);
document.body.appendChild(btn);
updateToggleText();
}
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 makeDraggable(el) {
let offsetX, offsetY, isDown = false;
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 move(e) {
if (!isDown) return;
el._dragging = true;
let newLeft = e.clientX - offsetX;
let newTop = e.clientY - offsetY;
// 限制在可视区域内
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';
}
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 init() {
injectStyle();
createToggle();
applyModeToAll(currentMode);
startObserver();
window.addEventListener('load', () => setTimeout(() => applyModeToAll(currentMode), 300));
}
init();
})();
代码// ==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() {
'use strict';
// 默认隐藏
let mode = GM_getValue('pid_mode', 'hidden');
// 核心样式
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);
// 执行功能 + 【新增:侧栏暴力隐藏】
function run() {
document.body.classList.toggle('hidePid', mode === 'hidden');
// 侧栏题目编号彻底隐藏(比赛页/普通页通用)
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 = '';
}
}
}
// 主标题
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); }
}
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();
}
// 启动
window.onload = ()=>{ btn(); setTimeout(run,300); setTimeout(run,800); };
})();