根据等级分来改变首字母的插件!
起因是我发现luogu的等级分好像很没用,不如 CF 和 AT 的可以改变名字颜色,所以我使用 AI 写了一个插件,可以根据等级分来改变首字母颜色。
颜色对照表: | Rating | 名字首字母颜色 | | :---------- | :---------- | | 0~500 | 浅蓝 | | 500~1000 | 浅绿 | | 1000~1200 | 青 | | 1200~1500 | 铁 | | 1500~1800 | 铜 | | 1800~2100 | 银 | | 2100~2400 | 金 | | 2400+ | 黑 |
插件源码:
// ==UserScript==
// @name Luogu 等级分首字母染色
// @namespace https://github.com/luogu-userscript
// @version 2.6
// @description 根据洛谷比赛等级分(rating)给页面上的用户名首字母按自定义色表染色(最终版)
// @author zbl2012
// @match *://www.luogu.com.cn/*
// @match *://luogu.com.cn/*
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @connect www.luogu.com.cn
// @connect luogu.com.cn
// ==/UserScript==
(function () {
'use strict';
/* =========================================================
* 等级分 -> 颜色 映射(最终版 v2)
* 0~500 亮蓝 500~1000 浅绿 1000~1200 青色
* 1200~1500 亮灰 1500~1800 橙铜 1800~2100 亮银
* 2100~2400 金色 2400+ 深空灰
* ========================================================= */
const COLOR_TABLE = [
{ max: 500, color: '#6BC5FF', name: '亮蓝' },
{ max: 1000, color: '#7ED957', name: '浅绿' },
{ max: 1200, color: '#00D4E0', name: '青色' },
{ max: 1500, color: '#8A8A8A', name: '亮灰' },
{ max: 1800, color: '#E8893A', name: '橙铜' },
{ max: 2100, color: '#B8C6D4', name: '亮银' },
{ max: 2400, color: '#FFC116', name: '金色' },
{ max: Infinity, color: '#3A3A3A', name: '深空灰' },
];
const BOLD_FIRST = true;
const COLOR_WHOLE = false;
const MAX_CONCURRENCY = 4;
const CACHE_DAYS = 7;
const CACHE_KEY = 'luogu_rating_cache_v2';
function ratingToColor(r) {
for (const c of COLOR_TABLE) if (r < c.max) return c.color;
return '#3A3A3A';
}
/* ---------- 本地缓存 ---------- */
let cache = {};
try { cache = JSON.parse(GM_getValue(CACHE_KEY, '{}') || '{}'); } catch (e) { cache = {}; }
function getCached(uid) {
const e = cache[uid];
if (!e) return undefined;
if (Date.now() - e.t > CACHE_DAYS * 86400000) return undefined;
return e.r;
}
function setCached(uid, r) {
cache[uid] = { t: Date.now(), r: r };
try { GM_setValue(CACHE_KEY, JSON.stringify(cache)); } catch (e) {}
}
/* ---------- 解析用户主页 HTML ---------- */
function parseRating(html) {
if (!html) return null;
let m = html.match(/<script id="lentille-context"[^>]*>(\{[\s\S]*?\})<\/script>/);
if (!m) m = html.match(/<script>(\{"instance":"main"[\s\S]*?\})\s*<\/script>/);
if (!m) return null;
let obj;
try { obj = JSON.parse(m[1]); } catch (e) { return null; }
const elo = (obj.data && obj.data.elo) || [];
if (elo.length) {
const latest = elo.find(e => e.latest) || elo[elo.length - 1];
return latest.rating || 0;
}
return 0;
}
/* ---------- 取等级分 ---------- */
const inflight = new Map();
function fetchRating(uid) {
const cached = getCached(uid);
if (cached !== undefined) return Promise.resolve(cached);
if (inflight.has(uid)) return inflight.get(uid);
const p = new Promise((resolve) => {
GM_xmlhttpRequest({
method: 'GET',
url: location.origin + '/user/' + uid,
headers: { 'Accept': 'text/html,application/xhtml+xml' },
timeout: 15000,
onload: (resp) => {
const r = parseRating(resp.responseText);
const v = (r === null) ? 0 : r;
setCached(uid, v);
resolve(v);
},
onerror: () => { setCached(uid, 0); resolve(0); },
ontimeout: () => { setCached(uid, 0); resolve(0); }
});
});
inflight.set(uid, p);
p.then(() => inflight.delete(uid));
return p;
}
const queue = [];
let active = 0;
function enqueue(uid, cb) {
queue.push({ uid, cb });
pump();
}
function pump() {
while (active < MAX_CONCURRENCY && queue.length) {
const { uid, cb } = queue.shift();
active++;
fetchRating(uid).then(cb).catch(() => cb(0)).finally(() => { active--; pump(); });
}
}
/* ---------- 染色函数 ---------- */
function wrapFirstChar(el, color) {
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null);
let node;
while ((node = walker.nextNode())) {
if (node.nodeValue && node.nodeValue.length) {
const ch = node.nodeValue.charAt(0);
const span = document.createElement('span');
span.style.color = color;
if (BOLD_FIRST) span.style.fontWeight = 'bold';
span.textContent = ch;
node.nodeValue = node.nodeValue.slice(1);
el.insertBefore(span, node);
return;
}
}
}
function colorLink(a) {
if (a.dataset.lgColored) return;
const href = (a.getAttribute('href') || '').split('#')[0];
const mm = href.match(/^\/user\/(\d+)\/?$/);
if (!mm) return;
const uid = mm[1];
a.dataset.lgColored = '1';
if (!((a.textContent || '').trim())) return;
enqueue(uid, (rating) => {
const color = ratingToColor(rating);
if (COLOR_WHOLE) {
a.style.color = color;
if (BOLD_FIRST) a.style.fontWeight = 'bold';
} else {
wrapFirstChar(a, color);
}
});
}
function processAll(root) {
(root || document).querySelectorAll('a[href^="/user/"]').forEach(colorLink);
}
/* ---------- 监听 SPA ---------- */
let scheduled = false;
function scheduleProcess() {
if (scheduled) return;
scheduled = true;
setTimeout(() => { scheduled = false; processAll(document); }, 250);
}
const observer = new MutationObserver((muts) => {
for (const m of muts) if (m.addedNodes && m.addedNodes.length) { scheduleProcess(); return; }
});
processAll(document);
if (document.body) observer.observe(document.body, { childList: true, subtree: true });
})();