版本比较
一个适用于strong的插件
正文
增加 320 行删除 4 行
1123456789101112 行未改动24252627242526282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342::::info{open}::::warning{open}本脚本需要油猴插件,请自行下载。本插件可能会被洛谷自动封禁AI封禁,请谨慎使用,被封禁概不负责。::::::::info{open}为什么我不去申请全站推荐,因为这是Deepseek生成的。::::这个插件~~可以让你strong~~修改你的用户名颜色(不含用户名牌子)~~让你在别人面前strong一下子。~~ 你可以在插件开头这里配置你的用户名和颜色: ```javascript const CONFIG = {12 行未改动 // ====================
```但是这个插件会卡顿一下子,这个也已经做到最优了,没法优化了。# 插件下载地址:飞书链接:<https://icnuq91erjxa.feishu.cn/drive/folder/QPkxfQ5fNlgFPbdVIR5c5M2mnoe?from=from_copylink> 密码:3c554&67 在插件文件夹里的名叫"用户名改色-15.0.user.js"。# 插件下载地址: 直接使用下面的代码:```javascript line-numbers lines=13-17// ==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('[文字高亮] 脚本已启动');})();```