跳到正文

科技·工程

戒P插件

cld_123约 16 分钟
原文
文章信息与版本

文章信息

发布
2026年10月29日
收录
2026年11月1日
更新
2026年11月19日
篇幅
约 16 分钟

版本

  1. v1当前

论如何戒P,那么就是让你的P话成真。

JavaScript
// ==UserScript==
// @name         洛谷难度伪装 v19(列表红框·做题页纯红字·完整还原·迷你按钮)
// @namespace    http://tampermonkey.net/
// @version      19.0
// @description  列表页彩色难度方框→红底白字方框;做题/详情页纯文字难度→只改红色文字、不加框不加背景。悬停看原配色,完整还原,迷你圆点按钮
// @match        *://*.luogu.com.cn/*
// @match        *://*.luogu.com/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const K_MASK  = 'lg_diff_mask_enabled';
    const K_HOVER = 'lg_diff_hover_enabled';
    const MASK_TEXT = '入门';
    const RED = '#e74c3c';       // 红色
    const WHITE = '#ffffff';     // 红底上的白字

    const raw = ['入门','普及-','普及','普及+/提高-','普及/提高-','提高','提高+/省选-','省选/NOI-','NOI/NOI+/CTSC','NOI/NOI+/CTS','暂无评定'];
    const clean = s => s.replace(/[\s\u00a0\u200b]+/g,'').replace(/[−—–]/g,'-');
    const list = raw.map(clean).filter(t => t !== MASK_TEXT);

    let enabled = localStorage.getItem(K_MASK) !== '0';
    let hoverOn = localStorage.getItem(K_HOVER) !== '0';

    const isTransparent = c => !c || c === 'transparent' || /rgba\(\s*0,\s*0,\s*0,\s*0\s*\)/.test(c);

    function getBox(el){
        let cur = el, up = 0;
        while (cur && cur.nodeType === 1 && up < 3) {
            if (!isTransparent(getComputedStyle(cur).backgroundColor)) return cur;
            if (cur.textContent.trim().length > 14) break;
            cur = cur.parentElement; up++;
        }
        return el;
    }

    /* ---------- 悬停提示 ---------- */
    const tip = document.createElement('div');
    tip.style.cssText = 'position:fixed;z-index:2147483600;pointer-events:none;display:none;' +
        'padding:4px 8px;border-radius:6px;background:rgba(0,0,0,.9);font-size:12px;' +
        'box-shadow:0 2px 10px rgba(0,0,0,.4);white-space:nowrap;';
    document.documentElement.appendChild(tip);

    function moveTip(e){ tip.style.left=(e.clientX+12)+'px'; tip.style.top=(e.clientY+16)+'px'; }
    function showTip(box, e){
        const orig = box.getAttribute('data-lg-orig');
        const bg   = box.getAttribute('data-lg-bg') || 'transparent';
        const fg   = box.getAttribute('data-lg-fg') || '#fff';
        const rd   = box.getAttribute('data-lg-radius') || '4px';
        tip.textContent = '';
        const lb = document.createElement('span');
        lb.textContent = '原难度 '; lb.style.color = '#aaa'; lb.style.fontWeight = '400';
        const bd = document.createElement('span');
        bd.textContent = orig;
        bd.style.cssText = `display:inline-block;padding:1px 8px;border-radius:${rd};background:${bg};color:${fg};font-weight:700;`;
        tip.appendChild(lb); tip.appendChild(bd);
        tip.style.display = 'block'; moveTip(e);
    }
    function hideTip(){ tip.style.display = 'none'; }

    document.addEventListener('mouseover', e => {
        const b = e.target && e.target.closest ? e.target.closest('[data-lg-orig]') : null;
        if (b && hoverOn && enabled) showTip(b, e); else hideTip();
    });
    document.addEventListener('mousemove', e => { if (tip.style.display === 'block') moveTip(e); });
    document.addEventListener('mouseout', e => {
        const b = e.target && e.target.closest ? e.target.closest('[data-lg-orig]') : null;
        if (b) hideTip();
    });

    /* ---------- 伪装 ---------- */
    function maskNode(textNode){
        const inner = textNode.parentNode;
        if (!inner || inner.nodeType !== 1) return;
        if (inner.closest('[data-lg-orig]')) return;
        const box = getBox(inner);
        const cs = getComputedStyle(box);
        const ts = getComputedStyle(inner);
        const hadBg = !isTransparent(cs.backgroundColor);   // ★ 原本是不是“带背景的方框”

        box.setAttribute('data-lg-orig', textNode.nodeValue.trim());
        box.setAttribute('data-lg-bg', cs.backgroundColor);
        box.setAttribute('data-lg-fg', ts.color);
        box.setAttribute('data-lg-radius', cs.borderRadius);
        box.setAttribute('data-lg-bs', box.getAttribute('style') || '');
        box.setAttribute('data-lg-is', inner.getAttribute('style') || '');

        textNode.nodeValue = MASK_TEXT;

        if (hadBg) {
            // 列表页的彩色方框:改成红底 + 白字(仍是方框,保留原有 padding/圆角)
            box.style.setProperty('background-color', RED, 'important');
            box.style.setProperty('color', WHITE, 'important');
            inner.style.setProperty('color', WHITE, 'important');
        } else {
            // 做题/详情页的纯文字难度:只把文字染红,不加背景、不加框
            box.style.setProperty('color', RED, 'important');
            inner.style.setProperty('color', RED, 'important');
            box.style.removeProperty('background-color');
            inner.style.removeProperty('background-color');
        }
    }

    /* ---------- 完整还原 ---------- */
    function restoreBox(box){
        const orig = box.getAttribute('data-lg-orig');
        const w = document.createTreeWalker(box, NodeFilter.SHOW_TEXT, null, false);
        let n, inner = null;
        while ((n = w.nextNode())) { if (n.nodeValue === MASK_TEXT) { n.nodeValue = orig; inner = n.parentNode; break; } }

        const bs = box.getAttribute('data-lg-bs');
        const is = box.getAttribute('data-lg-is');
        if (bs === '') box.removeAttribute('style'); else box.setAttribute('style', bs);
        if (inner && inner !== box) {
            if (is === '') inner.removeAttribute('style'); else inner.setAttribute('style', is);
        }
        box.querySelectorAll('*').forEach(el => { if (el !== inner) el.removeAttribute('style'); });

        ['data-lg-orig','data-lg-bg','data-lg-fg','data-lg-radius','data-lg-bs','data-lg-is']
            .forEach(a => box.removeAttribute(a));
    }

    function scan(){
        if (!enabled) { document.querySelectorAll('[data-lg-orig]').forEach(restoreBox); return; }
        const w = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
        let n; while ((n = w.nextNode())) if (list.includes(clean(n.nodeValue))) maskNode(n);
    }

    /* ---------- 迷你按钮 ---------- */
    const panel = document.createElement('div');
    panel.style.cssText = 'position:fixed;right:6px;bottom:6px;z-index:2147483601;display:flex;' +
        'flex-direction:column;gap:5px;align-items:flex-end;user-select:none;';
    const mk = () => {
        const b = document.createElement('div');
        b.style.cssText = 'width:18px;height:18px;line-height:18px;text-align:center;border-radius:50%;' +
            'font-size:10px;color:#fff;cursor:pointer;opacity:.22;transition:opacity .15s;' +
            'box-shadow:0 1px 4px rgba(0,0,0,.3);font-weight:700;';
        b.addEventListener('mouseenter', () => b.style.opacity = '1');
        b.addEventListener('mouseleave', () => b.style.opacity = '.22');
        return b;
    };
    const b1 = mk(), b2 = mk();
    function sync(){
        b1.textContent = enabled ? '原' : '伪';
        b1.title = enabled ? '点击还原为原难度(Alt+D)' : '点击伪装为红色入门';
        b1.style.background = enabled ? RED : '#2ecc71';
        b2.textContent = '提';
        b2.title = hoverOn ? '悬停提示:开(点击关闭)' : '悬停提示:关(点击开启)';
        b2.style.background = hoverOn ? '#5865f2' : '#888';
        b2.style.display = enabled ? 'block' : 'none';
    }
    b1.onclick = () => { enabled = !enabled; localStorage.setItem(K_MASK, enabled?'1':'0'); sync(); hideTip(); scan(); };
    b2.onclick = () => { hoverOn = !hoverOn; localStorage.setItem(K_HOVER, hoverOn?'1':'0'); sync(); hideTip(); };
    panel.appendChild(b1); panel.appendChild(b2);
    document.documentElement.appendChild(panel); sync();

    document.addEventListener('keydown', e => {
        if (e.altKey && (e.key === 'd' || e.key === 'D')) { b1.click(); e.preventDefault(); }
    });

    /* ---------- 启动 ---------- */
    let timer = null;
    const schedule = () => { if (timer) return; timer = setTimeout(() => { timer = null; scan(); }, 120); };
    const obs = new MutationObserver(schedule);
    (function boot(){
        if (document.body) { obs.observe(document.body, { childList:true, subtree:true, characterData:true }); scan(); }
        else setTimeout(boot, 50);
    })();
    setInterval(scan, 800);
})();
原文