一个适用于strong的插件

· · 闲话

::::warning{open} 本脚本需要油猴插件,请自行下载。 本插件可能会被洛谷自动封禁AI封禁,请谨慎使用,被封禁概不负责。 :::: ::::info{open} 为什么我不去申请全站推荐,因为这是Deepseek生成的。 :::: 这个插件可以让你strong修改你的用户名颜色(不含用户名牌子)让你在别人面前strong一下子。
你可以在插件开头这里配置你的用户名和颜色:

    const CONFIG = {
        username: 'is_ware',           // 要匹配的用户名
        highlightColor: '红',           // 高亮颜色
        // 原色(默认链接颜色),这些颜色的链接会被跳过
        originalColors: [
            'rgb(52, 152, 219)',        // 洛谷默认蓝色
            'rgb(52,152,219)',
            '#3498db',
            '#3498DB',
            'rgb(0, 102, 204)',
            'rgb(0,102,204)'
        ]
    };
    // ====================

但是这个插件会卡顿一下子,这个也已经做到最优了,没法优化了。

插件下载地址:

直接使用下面的代码:

// ==UserScript==
// @name         洛谷用户名改色
// @namespace    https://luogu.com.cn/user/1831977
// @version      17.0
// @description  跳过渲染的用户名也修改牌子颜色
// @match        https://www.luogu.com.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // ===== 配置区域 =====
    const CONFIG = {
        username: 'is_ware',      // 要匹配的用户名
        highlightColor: '红'            // 高亮颜色
    };
    // ====================

    const COLOR_MAP = {
        '棕': '#AD8B00',
        '灰': '#BFBFBF',
        '蓝': '#3498DB',
        '绿': '#52C41A',
        '橙': '#F39C12',
        '红': '#FE4C61',
        '紫': '#9D3DCF'
    };

    const targetColor = COLOR_MAP[CONFIG.highlightColor];
    if (!targetColor) {
        console.warn(`未知颜色: ${CONFIG.highlightColor}`);
        return;
    }

    const username = CONFIG.username;
    console.log(`[文字高亮] 目标用户名: "${username}"`);
    console.log(`[文字高亮] 目标颜色: ${targetColor}`);

    // 标准化颜色字符串
    function normalizeColor(color) {
        if (!color) return '';
        return color.toLowerCase().replace(/\s/g, '');
    }

    // 获取元素的颜色
    function getElementColor(element) {
        try {
            const color = window.getComputedStyle(element).color || 'rgb(0,0,0)';
            return normalizeColor(color);
        } catch (e) {
            return 'rgb(0,0,0)';
        }
    }

    // 检查是否是原色链接(蓝色)
    function isOriginalColor(element) {
        const color = getElementColor(element);
        const originalColors = [
            'rgb(52,152,219)',
            '#3498db',
            '#3498db'
        ];
        return originalColors.some(original => color === normalizeColor(original));
    }

    // 检查是否是大用户名(主页顶部)
    function isBigUsername(element) {
        if (!element) return false;

        if (element.tagName === 'SPAN') {
            const parent = element.parentElement;
            if (parent && parent.className && typeof parent.className === 'string') {
                const classes = parent.className.split(' ');
                if (classes.includes('luogu-username') && classes.includes('user-name')) {
                    return true;
                }
            }
        }
        return false;
    }

    // 查找并修改用户名旁边的牌子
    function modifyBadge(usernameElement) {
        // 向上查找 luogu-username 容器
        let container = usernameElement;
        let depth = 0;
        const maxDepth = 5;
        let modified = false;

        while (container && depth < maxDepth) {
            if (container.className && typeof container.className === 'string') {
                const classes = container.className.split(' ');
                if (classes.includes('luogu-username')) {
                    // 找到容器内的所有 span 标签(通常是牌子)
                    const spans = container.querySelectorAll('span');
                    spans.forEach(span => {
                        // 检查是否是牌子(通常有背景色,且不是用户名本身)
                        const style = window.getComputedStyle(span);
                        const bgColor = style.backgroundColor;
                        // 如果有背景色,且不是白色/透明,认为是牌子
                        if (bgColor && bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent' && bgColor !== 'rgba(255, 255, 255, 0)') {
                            // 修改牌子的颜色
                            span.style.backgroundColor = targetColor;
                            span.style.borderColor = targetColor;
                            span.style.color = '#FFFFFF';
                            span.style.setProperty('background-color', targetColor, 'important');
                            span.style.setProperty('border-color', targetColor, 'important');
                            console.log(`[文字高亮] 🏷️ 修改牌子颜色: "${span.textContent.trim()}" -> ${targetColor}`);
                            modified = true;
                        }
                    });
                    break;
                }
            }
            container = container.parentElement;
            depth++;
        }
        return modified;
    }

    // 检查是否应该高亮用户名
    function shouldHighlight(element) {
        // 如果是大用户名,跳过
        if (isBigUsername(element)) {
            return false;
        }

        // 如果是链接且颜色是原色,跳过
        if (element.tagName === 'A') {
            if (isOriginalColor(element)) {
                return false;
            }
        }

        return true;
    }

    // 高亮文本节点
    function highlightTexts() {
        let count = 0;
        let skippedBigUser = 0;
        let skippedOriginal = 0;
        let badgeModified = 0;
        let processed = 0;
        const maxProcess = 200;

        const walker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            {
                acceptNode: function(node) {
                    const parent = node.parentElement;
                    if (!parent) return NodeFilter.FILTER_REJECT;
                    const tag = parent.tagName;
                    if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'NOSCRIPT') {
                        return NodeFilter.FILTER_REJECT;
                    }

                    const text = node.textContent;
                    if (text.trim() === username) {
                        return NodeFilter.FILTER_ACCEPT;
                    }
                    return NodeFilter.FILTER_REJECT;
                }
            }
        );

        const nodes = [];
        let node;
        while (node = walker.nextNode()) {
            nodes.push(node);
            processed++;
            if (processed > maxProcess) break;
        }

        if (nodes.length === 0) {
            return 0;
        }

        console.log(`[文字高亮] 找到 ${nodes.length} 个 "${username}"`);

        nodes.forEach(textNode => {
            const parent = textNode.parentElement;
            if (!parent) return;

            // 检查是否应该高亮用户名
            const highlight = shouldHighlight(parent);

            // 如果是大用户名,跳过渲染但修改牌子
            if (isBigUsername(parent)) {
                skippedBigUser++;
                // 仍然尝试修改牌子
                const modified = modifyBadge(parent);
                if (modified) badgeModified++;
                return;
            }

            // 如果是原色链接,跳过渲染但修改牌子
            if (parent.tagName === 'A' && isOriginalColor(parent)) {
                skippedOriginal++;
                // 仍然尝试修改牌子
                const modified = modifyBadge(parent);
                if (modified) badgeModified++;
                return;
            }

            // 正常高亮
            if (parent.dataset.highlighted === 'true') return;

            if (parent.tagName === 'A') {
                parent.style.color = targetColor;
                parent.style.fontWeight = 'bold';
                parent.style.setProperty('color', targetColor, 'important');
                parent.dataset.highlighted = 'true';
                count++;
                console.log(`[文字高亮] ✅ 高亮链接: "${textNode.textContent.trim()}"`);

                // 修改旁边的牌子
                const modified = modifyBadge(parent);
                if (modified) badgeModified++;
            } else {
                const span = document.createElement('span');
                span.style.color = targetColor;
                span.style.fontWeight = 'bold';
                span.style.setProperty('color', targetColor, 'important');
                span.dataset.highlighted = 'true';
                span.textContent = textNode.textContent;
                parent.replaceChild(span, textNode);
                count++;
                console.log(`[文字高亮] ✅ 高亮文字: "${textNode.textContent.trim()}"`);
            }
        });

        console.log(`[文字高亮] 结果: 高亮 ${count} 个,跳过 ${skippedBigUser} 个(大用户名),跳过 ${skippedOriginal} 个(原色链接),修改 ${badgeModified} 个牌子`);
        return count;
    }

    // 清除高亮标记
    function clearHighlights() {
        document.querySelectorAll('[data-highlighted="true"]').forEach(el => {
            delete el.dataset.highlighted;
        });
    }

    // 防抖执行
    let timer = null;
    let isRunning = false;

    function runHighlight() {
        if (isRunning) return;

        if (timer) {
            clearTimeout(timer);
            timer = null;
        }

        timer = setTimeout(() => {
            isRunning = true;
            try {
                clearHighlights();
                highlightTexts();
            } catch (e) {
                console.error('[文字高亮] 错误:', e);
            } finally {
                isRunning = false;
                timer = null;
            }
        }, 500);
    }

    // 初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', () => {
            setTimeout(runHighlight, 800);
        });
    } else {
        setTimeout(runHighlight, 800);
    }

    setTimeout(runHighlight, 2000);

    // DOM变化监听
    let mutationTimer = null;
    const observer = new MutationObserver(() => {
        if (mutationTimer) {
            clearTimeout(mutationTimer);
        }
        mutationTimer = setTimeout(() => {
            runHighlight();
            mutationTimer = null;
        }, 1000);
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        characterData: true
    });

    // 路由变化
    window.addEventListener('popstate', () => setTimeout(runHighlight, 500));
    window.addEventListener('hashchange', () => setTimeout(runHighlight, 500));

    const origPushState = history.pushState;
    history.pushState = function() {
        origPushState.apply(this, arguments);
        setTimeout(runHighlight, 500);
    };

    console.log('[文字高亮] 脚本已启动');
})();