版本比较
洛谷增强插件
正文
增加 127 行删除 125 行
39 行未改动4041424344444546474849505150515253545555565756575859606162635960616465666768697066717273747576777879808182838485868788899091925 行未改动98991001018182102103104105106107200 行未改动全部展开3083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483496 行未改动3563573583593053063073083603613623103113123133143153163173183193203633643653663 行未改动3703713723733743753763773783799 行未改动38939039139234935035139339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342436236336436536636736836937037137237337437537637737837942538138238338438538638738838939039139239339439539639739839940040140240340440540640740840941042642741241341441541641741841942842942243043143243343412 行未改动44744844945045144344444545245345445545645745845946046146245046346446546646746846947045545645745847147247347447547627 行未改动50450550650749450850951051151251351451550250350450550650750850951051151651751851952020 行未改动54154254354454054154254354454554654754854954554654755255354855455554955055155255355455555655737 行未改动59559659759860559960060860160260360460560661560760860961061162161261361461561661761861962062162262362432 行未改动65765865966066166266366466543 行未改动70971071171271371471571671724 行未改动7427437447457457467477487497507517524 行未改动39 行未改动**用户颜色**:通过洛谷官方 API `https://www.luogu.com.cn/api/user/search?keyword={uid}` 获取用户信息,将 `color` 字段映射为 CSS 颜色。
## 安装
### 方法一:Chrome 扩展(crx 文件)### 方法一:Chrome/Edge 扩展(crx 文件)
由于非官方扩展无法直接拖拽安装,需要通过组策略强制安装。
插件已打包为 `crx` 文件,点击下方链接下载:
- **插件下载**:[洛谷增强插件.crx](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/c00oy7z7)- **组策略模板**:[chrome.adm](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/9melhw1x)- **插件下载**:[洛谷增强插件.crx](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/r73mtxte)- **Chrome组策略模板**:[chrome.adm](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/9melhw1x)- **Edge组策略模板**:[msedge.adm](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/d5iigel7)
安装步骤:
1. 打开 Chrome,在地址栏输入 `chrome://extensions/` 并回车。2. 开启右上角的「开发者模式」。3. 将 `crx` 文件拖入页面,Chrome 会提示无法安装,此时在页面上可以看到该扩展的 ID(一串字母),复制下来。1. 打开浏览器,进入扩展管理页面:- Chrome:`chrome://extensions/`- Edge:`edge://extensions/`2. 开启开发者/开发人员模式:- Chrome:开启右上角的 「开发者模式」- Edge:开启左下角的 「开发人员模式」3. 将 `crx` 文件拖入页面,浏览器会提示无法安装,此时在页面上可以看到该扩展的 ID(一串字母),复制下来。4. 按下 `Win+R`,输入 `gpedit.msc` 打开本地组策略编辑器。5. 依次点击「计算机配置 → 管理模板」,右键选择「添加/删除模板」,导入 `chrome.adm` 文件。6. 找到路径「经典管理模板(ADM) → Google → Google Chrome → 扩展程序」。7. 双击「配置扩展程序安装许可名单」,选择「已启用」,点击「显示」。5. 依次点击「计算机配置 → 管理模板」,右键选择「添加/删除模板」,导入 `chrome.adm`或 `msedge.adm` 文件。6. 找到路径「经典管理模板(ADM) → Google → Google Chrome → 扩展程序」 或「经典管理模板(ADM) → Microsoft Edge → 扩展」。7. 双击「配置扩展程序安装许可名单」或「允许安装特定扩展」,选择「已启用」,点击「显示」。8. 将刚才复制的 ID 填入白名单列表,点击「应用」并按「确定」键关闭。9. 点击浏览器右上角的洛谷增强插件图标,点击「打开插件设置」,即可选择需要的功能(默认全开),点击保存并刷新页面即可。> 如需更新插件,只需直接将crx文件拖入即可,无需进入本地组策略编辑器
### 方法二:Tampermonkey(油猴)如果你的电脑没有本地组策略管理器,可尝试注册表法:
1. 按下 `Win + R`,输入 `regedit` 打开注册表编辑器,定位到路径:- **Chrome**: `计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\ExtensionInstallAllowlist`- **Edge**: `计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallAllowlist` > 如果 `ExtensionInstallAllowlist` 文件夹不存在,在 `Chrome` 或 `Edge` 项下右键 → 新建 → 项,命名为 `ExtensionInstallAllowlist`。 > 2. 在右侧空白处,右键 → 新建 → 字符串值,名称命名为 `1`(如果已经有其他白名单,则继续用 `2`、`3`……)。3. 双击新建的字符串值,在「数值数据」中填入你刚才复制的扩展 ID,点击确定。4. 关闭注册表编辑器,**重启浏览器**。5. 在地址栏输入 `chrome://policy/`(或 `edge://policy/`),点击「重新加载策略」,确认「ExtensionInstallAllowlist」策略已生效。6. 再次将 `crx` 文件拖入扩展管理页面,即可成功安装。
### 方法二:Tampermonkey(支持火狐等大部分浏览器)
1. 安装 [Tampermonkey](https://www.tampermonkey.net/) 浏览器扩展。2. 点击 Tampermonkey 图标 →「添加新脚本」。3. 将下方代码粘贴进去,按 `Ctrl+S` 保存。4. 点击油猴图标,点击「打开插件设置」,保存并刷新页面即可生效。5 行未改动```javascript// ==UserScript==// @name 洛谷增强插件// @namespace http://tampermonkey.net/// @version 3.0.0// @description 洛谷增强插件:广告屏蔽、题目颜色、用户颜色、保存站跳转// @version 3.0.1// @description 洛谷增强插件:广告屏蔽、题目颜色、用户颜色、保存站跳转(题号规则:字母数字下划线,空格结束)// @author realskc (modified) & linch & Vitamin_B// @match *://*.luogu.com/*// @match *://*.luogu.com.cn/*// @grant GM_registerMenuCommand200 行未改动全部展开 if (difficulty > 8) difficulty = 8; return `rgb(${problemDifficultyToColor[difficulty].join(',')})`; }
// ========== 统一题号着色函数(按空格提取) ========== function colorizeLinkElement(link, color) { if (link.dataset.lgbotProblemColored === '1') return;
// 如果有 .pid 元素(官方结构),直接染它 const pidSpan = link.querySelector('.pid'); if (pidSpan) { pidSpan.style.color = color; pidSpan.dataset.lgbotProblemColored = '1'; return; }
const text = link.textContent.trim(); // 取第一个空格前的内容作为题号 let problemId = text; const spaceIndex = text.indexOf(' '); if (spaceIndex > 0) { problemId = text.substring(0, spaceIndex); }
// 验证是否由字母/数字/下划线组成(长度1~20) if (!/^[A-Za-z0-9_]{1,20}$/.test(problemId)) { // 回退:整链着色 link.style.setProperty('color', color, 'important'); link.dataset.lgbotProblemColored = '1'; return; }
// 将题号部分用带颜色的 span 包裹(只替换第一次出现的题号) const newHtml = text.replace(problemId, `<span style="color:${color} !important;">${problemId}</span>`); link.innerHTML = newHtml; link.dataset.lgbotProblemColored = '1'; }
function colorizePracticePageFromLabels() { const groups = document.querySelectorAll('.difficulty'); let count = 0; groups.forEach(group => {6 行未改动 } if (!problems) return; const links = problems.querySelectorAll('a[href^="/problem/"]'); links.forEach(link => { const pidSpan = link.querySelector('.pid'); if (pidSpan && pidSpan.dataset.lgbotProblemColored !== '1') { pidSpan.style.color = bgColor; pidSpan.dataset.lgbotProblemColored = '1'; if (link.dataset.lgbotProblemColored !== '1') { colorizeLinkElement(link, bgColor); count++; } else if (!pidSpan) { const text = link.innerText.trim(); const match = text.match(/^([A-Z]+\d+)/); if (match) { const pid = match[1]; if (link.dataset.lgbotProblemColored !== '1') { link.innerHTML = text.replace(pid, `<span style="color:${bgColor};">${pid}</span>`); link.dataset.lgbotProblemColored = '1'; count++; } } } }); }); if (count > 0) console.log(`洛谷增强插件: 练习页已为 ${count} 个题目编号着色(从难度标签)`);3 行未改动 async function colorizePracticePageWithoutLabels() { const problemBlocks = document.querySelectorAll('.problems'); let count = 0; let submittedMap = new Map();
// 从 _feInjection / _feInstance 读取 try { const feData = window._feInjection || window._feInstance; if (feData && feData.currentData) { const data = feData.currentData;9 行未改动 } } } catch(e) {}
for (const block of problemBlocks) { const prev = block.previousElementSibling; if (prev && prev.matches('.difficulty')) continue; // 从 lentille-context 读取 try { const scriptEl = document.querySelector('script#lentille-context'); if (scriptEl) { const jsonData = JSON.parse(scriptEl.textContent); const data = jsonData.data; if (data && data.passed) { for (const p of data.passed) { submittedMap.set(p.pid, getDifficultyColor(p.difficulty)); } } if (data && data.submitted) { for (const p of data.submitted) { submittedMap.set(p.pid, getDifficultyColor(p.difficulty)); } } } } catch(e) { console.warn('洛谷增强插件: 解析 lentille-context 失败', e); }
for (const block of problemBlocks) { const links = block.querySelectorAll('a[href^="/problem/"]:not([data-lgbot-problem-colored="1"])'); if (links.length === 0) continue;
for (const link of links) { let problemid = link.href.split('/problem/')[1].split('?')[0].split('/')[0]; if (!problemid || !/[a-zA-Z]/.test(problemid) || !/[0-9]/.test(problemid)) continue;
let color = submittedMap.get(problemid); if (!color) color = await fetchProblemColorForLink(problemid); if (!color) continue; const pidSpan = link.querySelector('.pid'); if (pidSpan) { pidSpan.style.color = color; pidSpan.dataset.lgbotProblemColored = '1'; count++; } else { const text = link.innerText.trim(); const match = text.match(/^([A-Z]+\d+)/); if (match && match[1] === problemid) { link.innerHTML = text.replace(problemid, `<span style="color:${color};">${problemid}</span>`); link.dataset.lgbotProblemColored = '1'; count++; } } } } if (count > 0) console.log(`洛谷增强插件: 练习页已为 ${count} 个题目编号着色(通过缓存/请求)`); }
async function colorizeRecordPage() { const links = document.querySelectorAll('a[href^="/problem/"]:not([data-lgbot-problem-colored="1"])'); if (links.length === 0) return;
let recordMap = new Map(); try { const feData = window._feInjection || window._feInstance; if (feData && feData.currentData && feData.currentData.records) { const records = feData.currentData.records.result; if (records && records.length > 0) { for (const item of records) { if (item.problem && item.problem.pid) { recordMap.set(item.problem.pid, getDifficultyColor(item.problem.difficulty)); } } } } } catch(e) {}
let count = 0; for (const link of links) { let problemid = link.href.split('/problem/')[1].split('?')[0].split('/')[0]; if (!problemid || !/[a-zA-Z]/.test(problemid) || !/[0-9]/.test(problemid)) continue; let color = recordMap.get(problemid); if (!color) color = await fetchProblemColorForLink(problemid); if (!color) continue; const pidSpan = link.querySelector('.pid'); if (pidSpan) { pidSpan.style.color = color; pidSpan.dataset.lgbotProblemColored = '1'; colorizeLinkElement(link, color); count++; } else { const text = link.innerText.trim(); const match = text.match(/^([A-Z]+\d+)/); if (match && match[1] === problemid) { link.innerHTML = text.replace(problemid, `<span style="color:${color};">${problemid}</span>`); link.dataset.lgbotProblemColored = '1'; count++; } } } if (count > 0) console.log(`洛谷增强插件: 记录页已为 ${count} 个题目编号着色`); if (count > 0) console.log(`洛谷增强插件: 练习页已为 ${count} 个题目编号着色(通过缓存/请求)`); }
async function fetchProblemColorForLink(problemid) { if (problemToColorMap.has(problemid)) return problemToColorMap.get(problemid);12 行未改动 const doc = parser.parseFromString(html, 'text/html'); let scriptText = ''; for (const i of doc.querySelectorAll("script")) scriptText += i.textContent; const match = scriptText.match(/"difficulty":\d/); let color; if (!match) return null; const difficulty = Number(match[0].match(/\d/)[0]); const color = getDifficultyColor(difficulty); if (match) { const difficulty = Number(match[0].match(/\d/)[0]); color = getDifficultyColor(difficulty); } else { color = getDifficultyColor(0); console.log(`洛谷增强插件: 题目 ${problemid} 未找到难度,使用默认灰色`); } problemToColorMap.set(problemid, color); return color; } catch (error) { console.warn(`获取题目 ${problemid} 颜色失败:`, error.message); return null; const defaultColor = getDifficultyColor(0); problemToColorMap.set(problemid, defaultColor); return defaultColor; } }
// ========== 修改:新题号规则 ========== function isProblemId(problemid) { if (problemid.startsWith('AT_')) return true; if (!/[a-zA-Z]/.test(problemid) || !/[0-9]/.test(problemid)) return false; if (problemid.includes('html') || problemid.includes('void') || problemid.length > 20) return false; return true; // 只允许字母、数字、下划线,长度 1~20 return /^[A-Za-z0-9_]{1,20}$/.test(problemid); }
let scanTimer = null;
27 行未改动 if (pidSpan && pidSpan.dataset.lgbotProblemColored === '1') continue; if (!pidSpan && link.dataset.lgbotProblemColored === '1') continue;
const text = link.innerText.trim(); if (!pidSpan && !text.startsWith(problemid)) continue; if (!pidSpan && !text.includes(problemid)) continue;
toProcess.push({ link, pidSpan, problemid }); } if (toProcess.length > 0) { for (const { link, pidSpan, problemid } of toProcess) { const color = await fetchProblemColorForLink(problemid); if (!color) continue; if (pidSpan) { pidSpan.style.color = color; pidSpan.dataset.lgbotProblemColored = '1'; } else { const text = link.innerText.trim(); if (text.startsWith(problemid)) { link.innerHTML = text.replace(problemid, `<span style="color:${color};">${problemid}</span>`); link.dataset.lgbotProblemColored = '1'; } } colorizeLinkElement(link, color); } console.log(`洛谷增强插件: 已处理 ${toProcess.length} 个题目编号`); }
20 行未改动 colorizePracticePageWithoutLabels(); }, 200); return; } if (path.startsWith('/record/list')) { await colorizeRecordPage(); const observer = new MutationObserver(() => { colorizeRecordPage(); }); observer.observe(document.body, { childList: true, subtree: true }); window.__lgbotProblemObserver = observer; startPeriodicScan(colorizeRecordPage, 200); return; } // 所有其他页面(包括记录页、讨论区、犇犇等)统一处理 await scanAllLinks(); startPeriodicScan(scanAllLinks, 200);
const observer = new MutationObserver(() => { const observer = new MutationObserver(() => { scanAllLinks(); }); scanAllLinks(); }); observer.observe(document, { childList: true, subtree: true, characterData: true }); window.__lgbotProblemObserver = observer; }
// ========== 用户颜色相关 ========== const userColorCache = new Map(); const userFetcher = new ConcurrencyLimiter(3, 200);
function mapApiColorToCss(apiColor) {37 行未改动 } return null; }
// ---------- 修改点 1:删除正则限制;修改点 2:支持绝对路径 ---------- async function colorizeUserLink(link) { const href = link.getAttribute('href'); // 提取路径部分(去掉域名) const path = href.replace(/^https?:\/\/[^\/]+/, ''); if (!/^\/user\/\d+$/.test(path)) return;
const text = link.innerText.trim(); const navTexts = ['个人中心', '主页', '用户设置', '练习情况', '练习', '专栏', '我/TA 关注的人', '个人设置', '我的收藏', '我的团队', '我的比赛', '我的题库']; if (navTexts.some(t => text === t || text.includes(t))) return; // 原正则已删除,现在不限制字符类型
const match = path.match(/\/user\/(\d+)/); if (!match) return; const uid = match[1]; const info = await userFetcher.push(() => fetchUserInfo(uid)); if (info) { if (!info) { link.dataset.lgbotColored = '1'; return; }
if (text.startsWith('@')) { const username = text.substring(1); link.innerHTML = `<span style="color:inherit;">@</span><span style="color:${info.color} !important;">${username}</span>`; } else { link.style.setProperty('color', info.color, 'important'); } link.dataset.lgbotColored = '1'; }32 行未改动 }); window.__lgbotUserColorObserver = observer; }
// ========== 保存站跳转 ========== function saveSiteJump() { const url = window.location.href; const title = document.title; const html = document.documentElement.outerHTML;43 行未改动 return; } }
// ========== 启动逻辑 ========== if (localStorage.getItem('lgbot1SaveSiteJump') !== 'false') { if (window.location.pathname.match(/^\/(paste|article|discuss)\//)) { saveSiteJump(); }24 行未改动## 附件
| 文件 | 链接 ||------|------|| 洛谷增强插件.crx | [下载](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/c00oy7z7) || 洛谷增强插件.crx | [下载](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/r73mtxte) || chrome.adm | [下载](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/9melhw1x) || msedge.adm | [下载](https://www.luogu.com.cn/fe/api/problem/downloadAttachment/d5iigel7) |
## 说明
本文在写作过程中使用了 DeepSeek AI 进行代码生成、文字辅助润色和格式修改。所有代码均经过手工测试与验证,保证作者对文章的贡献大于AI。4 行未改动