luogu 难度屏蔽器 搬运
// ==UserScript==
// @name Luogu 难度隐藏开关
// @namespace http://tampermonkey.net/
// @version V1.2
// @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', // 题目信息行里的 <span>
'.difficulty span span' // 题卡里的嵌套 <span>
];
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;
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; }
`;
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'; // 避免和默认 right 冲突
}
btn.addEventListener('click', (e) => {
// 拖动时也会触发 click,这里要排除
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;
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', up);
});
function move(e) {
if (!isDown) return;
el._dragging = true;
el.style.left = (e.clientX - offsetX) + 'px';
el.style.top = (e.clientY - offsetY) + 'px';
el.style.right = 'auto'; // 移动后固定 left/top
}
function up(e) {
if (!isDown) return;
isDown = false;
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); // 避免立即触发 click
}
}
function init() {
injectStyle();
createToggle();
applyModeToAll(currentMode);
startObserver();
window.addEventListener('load', () => setTimeout(() => applyModeToAll(currentMode), 300));
}
init();
})();
以下是博客签名,正文无关
本文来自博客园,作者:Wy_x,转载请在文首注明原文链接:https://www.cnblogs.com/Wy-x/articles/19059239
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议(CC-BY-NC-SA 4.0 协议)进行许可。

浙公网安备 33010602011771号