版本比较
洛谷简易插件·LuoguSP
标题
增加 1 行删除 1 行
11洛谷简易插件洛谷简易插件·LuoguSP正文
增加 33 行删除 1746 行
11234567891011121314155671617181920212292324251213141526272829303132332122343536373839402829303132333435414243444546474849373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217635051之前一直在使用斯老板的插件 [简易洛谷插件](https://www.luogu.com.cn/article/dxg99wmz),但最近洛谷更新了难度分级,引入了新的难度及颜色;同时个人页面的个人介绍也进行了权限隐藏。这导致原插件的大部分功能都已经失效或漂移,而原插件已经很久没有再进行更新,所以趁此机会对原插件进行了功能修复和完善。
**因洛谷近期更新较多,请大家尽快升级到最新版本 v2.14.6,以启用并修复更多功能。**
- Update 2.14.6:应对洛谷近期的更新,优化了题目着色速度。- Update 2.14.5:移除了提交记录中的题目着色;修复了因洛谷接口更新而产生的云剪切板无法访问的问题。- Update 2.14.0:移除了不常用的功能;美化了洛谷的鼠标悬停用户信息卡片;新增了题目的鼠标悬停信息卡片;开放了个人文章的评论、点赞、收藏功能。- Update 2.10.7:新增受限 文章/剪贴板 显示功能;新增 IDE 模式下,一键测试题目中所有样例的功能。
之前一直在使用 @realskc 的插件 [简易洛谷插件](https://www.luogu.me/article/dxg99wmz)。但最近洛谷更新了难度分级,引入了新的难度及颜色;同时网站的整体 UI 框架及 API 接口也有诸多更新。这导致原插件的大部分功能都已经失效或漂移,且原插件已经很久没有更新,所以趁此机会对原插件进行了重构,制作了新的插件 LuoguSP。

当前版本功能一览:- 题目的题号显示题目颜色,一眼分辨题目难度。- 私信界面 Ctrl+点击 聊天列表或聊天框中的用户名或头像,即可直接打开用户主页。- 显示其他用户个人页面中被系统隐藏的个人简介。- 脚本管理插件中一键更新最新版本的LuoguSP插件。- 题目悬停显示预览卡,一眼分辨题目最新情况。- 用户名/头像悬停显示预览卡,一眼观察用户数据。- 显示其他用户个人页面中的个人简介。- 显示其他用户的 文章/剪贴板,可点赞、收藏、评论互动。- 题目页面中的 IDE ,可一键测试题目中的所有样例。- 浏览器脚本插件中,一键更新最新版本的 LuoguSP 插件。
关于插件的建议及BUG反馈,评论区留言 或 私信 或 [Gitee项目](https://gitee.com/shanire/LuoguSP) 中提交Issues都可。关于插件的建议及BUG反馈,评论区留言/私信 或 [CNB云原生](https://cnb.cool/Round1/LuoguSP) [Github项目](https://github.com/shanirez/LuoguSP) 中提交Issues都可。
安装方式和之前一样:1. 先安装油猴/篡改猴/暴力猴之类的浏览器插件。推荐使用Edge浏览器,因为它的插件市场能直接访问。2. 点击新增脚本,复制下方的脚本内容,不要有所遗漏。3. 在油猴/篡改猴/暴力猴中开启脚本,并开始使用。相关功能设置在【左侧导航栏中最下方】。4. 可能还需要在【浏览器】->【管理扩展】的设置中,选择【油猴/篡改猴/暴力猴】的【详情信息】,开启功能-->【允许用户脚本】。1. 先安装油猴/篡改猴/暴力猴之类的浏览器扩展,用来管理用户脚本。推荐使用Edge浏览器,因为它的插件市场能直接访问,无需魔法上网。2. 扩展安装完成后,还需在【浏览器】->【管理扩展】的设置中,选择【油猴/篡改猴/暴力猴】的【详情信息】,开启功能-->【允许用户脚本】。3. 点击[【此处脚本链接】](https://cnb.cool/Round1/LuoguSP/-/git/raw/main/LuoguSP.user.js),油猴/篡改猴/暴力猴中会自动识别并询问是否安装脚本/更新脚本,安装后即可开始使用。相关功能设置按钮在【洛谷**左侧导航栏**中最下方】。
```javascript// ==UserScript==// @name LuoguSP// @namespace https://github.com/ShanireZ/LuoguSP// @version 2.9.2// @description LuoguSP:题目难度着色 / 私信 Ctrl+Click(用户名+头像) 跳转主页 / 显示隐藏的个人简介 / IDE 一键测试样例// @version 2.14.6// @description LuoguSP:题号显示难度颜色 / 题目悬停显示预览卡 / 用户名/头像悬停显示预览卡 / 个人页显示个人介绍 / 受限文章与剪贴板解限 / IDE 模式一键测试所有样例// @author ShanireZ, realskc (Until 1.8.2)// @license GPL-3.0// @match https://www.luogu.com.cn/*// @homepageURL https://github.com/ShanireZ/LuoguSP// @supportURL https://github.com/ShanireZ/LuoguSP/issues// @updateURL https://gitee.com/shanire/LuoguSP/raw/main/LuoguSP.user.js// @downloadURL https://gitee.com/shanire/LuoguSP/raw/main/LuoguSP.user.js// @grant none// @require https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js// @require https://cdn.jsdelivr.net/npm/marked@4.3.0/marked.min.js// @require https://cdn.jsdelivr.net/npm/dompurify@3.0.9/dist/purify.min.js// @require https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/highlight.min.js// @run-at document-end// @updateURL https://cnb.cool/Round1/LuoguSP/-/git/raw/main/LuoguSP.user.js// @downloadURL https://cnb.cool/Round1/LuoguSP/-/git/raw/main/LuoguSP.user.js// @sandbox raw// @connect luogusp.round1.cc// @grant GM_xmlhttpRequest// @require https://luogusp.round1.cc/releases/2.14.6/compat/early-gate.fe3f2a81d119b8c4.js#sha256=fe3f2a81d119b8c49ac9b5b2b359bcc9f823c0fb290ca60ee4d53147e481a3b5// @require https://luogusp.round1.cc/releases/2.14.6/compat/runtime.539a993454a5b677.js#sha256=539a993454a5b6778e577a3fcce545b80c24714cd4c8f3b517320bf40785f76b// @run-at document-start// ==/UserScript==
"use strict";(function () { // ============================================================ // 配置区(日常维护改这里) // ============================================================
const APP_NAME = "LuoguSP"; const STORAGE_PREFIX = `${APP_NAME}.`;
// 洛谷难度 → 颜色;下标 = practice/problem 接口返回的 difficulty 整数 (0..8) // 2026-07 洛谷把旧 8 档改为新 9 档:在「普及+/提高-」后插入「提高」,NOI 档色号微调。 // 维护:色号有变时,对照 luogu.com.cn 难度统计页逐行改 hex 即可,无需动别处。 const DIFFICULTY_COLORS = [ "#bfbfbf", // 0 暂无评定 "#fe4c61", // 1 入门 "#f39c11", // 2 普及- "#ffc116", // 3 普及 "#52c41a", // 4 普及+/提高- "#14b8a6", // 5 提高 ← 2026-07 新增 "#3498db", // 6 提高+/省选- "#9d3dcf", // 7 省选/NOI- "#0f172a", // 8 NOI/NOI+/CTSC ]; const FALLBACK_COLOR = "#bfbfbf"; const diffColor = (d) => DIFFICULTY_COLORS[d] || FALLBACK_COLOR; // 未知档兜底成灰,不再越界崩溃
// 依赖洛谷 DOM 的易变选择器(data-v-* 哈希会随洛谷构建变化):改版排查时优先来这里改。 const SELECTORS = { // 洛谷有两套导航:首页竖排 nav.lfe-body(条目用 .text)/ 内容页左侧栏 nav.sidebar(条目用 .title) navContainers: ["nav.lfe-body", "nav.sidebar"], // 设置入口挂载点(按序取第一个存在的) navText: ".text, .title", // 导航条目里的文字 span chatTrigger: '[slot="trigger"]', // 私信用户名触发点(旧 data-v 选择器已失效,用语义属性) userIntroColumn: ".sidebar-container .main", // 用户主页右侧内容列(补显简介的挂载点) nativeIntro: ".introduction", // 洛谷原生简介元素(存在=已显示,脚本不重复补) voidAnchor: "a[data-v-bade3303][data-v-4842157a]", // 题号着色里特殊的 javascript:void 0 链接 // —— IDE 模式(2026-07 columba 前端;侦察实录 docs/superpowers/specs/2026-07-22-ide-mode-recon-notes.md)—— ideToolbar: ".ide-toolbar", // IDE 三个分区(代码/输入/输出)各一条工具栏 ideToolbarText: ".title .text", // 工具栏标题文字(代码/输入/输出) ideToolbarActions: ".actions", // 工具栏右侧按钮容器 ideRunResult: ".run-result", // 输出工具栏里的 时间+内存 / RE 原因 ideTextarea: "textarea.ide-textarea", // 输入/输出面板的文本域 ideSampleBlock: ".io-sample-block", // 题面样例块(输入 #N / 输出 #N 各一块) cmContent: ".cm-content", // CodeMirror 6 内容层 lentilleContext: "script#lentille-context", // 新版页面数据(JSON,含 problem.samples) };
// 功能开关:key → 显示名。新增功能只需在此登记 + 在底部 FEATURES 注册启动器。 const FEATURE_LABELS = new Map([ [`${STORAGE_PREFIX}addProblemsColor`, "显示题目颜色"], [`${STORAGE_PREFIX}addMessageLink`, "私信界面 Ctrl+Click 打开用户主页"], [`${STORAGE_PREFIX}showIntro`, "显示隐藏的个人简介"], [`${STORAGE_PREFIX}ideBatchSampleTest`, "IDE 一键测试样例"], ]);
const storage = { get: (k) => localStorage.getItem(k) === "true", set: (k, v) => localStorage.setItem(k, String(v)), has: (k) => localStorage.getItem(k) !== null, }; // 首次运行:所有功能默认开启 for (const k of FEATURE_LABELS.keys()) if (!storage.has(k)) storage.set(k, true);
// ============================================================ // 设置面板(页内浮层,替代原来的 window.open + document.write) // ============================================================ function injectStyle() { if (document.getElementById("luogusp-style")) return; const style = document.createElement("style"); style.id = "luogusp-style"; style.textContent = ` #luogusp-settings{position:fixed;inset:0;z-index:100000;font-size:14px;color:#222;} #luogusp-settings .luogusp-mask{position:absolute;inset:0;background:rgba(0,0,0,.35);} #luogusp-settings .luogusp-panel{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%); background:#fff;border-radius:8px;padding:24px 30px;min-width:300px;box-shadow:0 8px 30px rgba(0,0,0,.2);} #luogusp-settings .luogusp-content{width:max-content;max-width:min(420px,calc(100vw - 88px));margin:0 auto;} #luogusp-settings h3{margin:0 0 12px;font-size:16px;} #luogusp-settings .luogusp-item{display:flex;align-items:center;gap:8px;padding:6px 0;cursor:pointer;} #luogusp-settings .luogusp-actions{display:flex;gap:8px;margin-top:16px;flex-wrap:wrap;} #luogusp-settings button{padding:5px 14px;border:1px solid #ccc;border-radius:5px;background:#f7f7f7;cursor:pointer;} #luogusp-settings button:not(.luogusp-primary):hover{background:#efefef;} #luogusp-settings .luogusp-primary{background:#0e88d3;border-color:#0e88d3;color:#fff;} #luogusp-settings .luogusp-primary:hover{background:#0879bd;border-color:#0879bd;color:#fff;} #luogusp-settings .luogusp-primary:active{background:#066ca9;border-color:#066ca9;color:#fff;} #luogusp-settings .luogusp-hint{margin:10px 0 0;color:#888;font-size:12px;} .luogusp-setting-entry{cursor:pointer;} .luogusp-intro-card li:has(> input[type="checkbox"]){list-style:none;margin-left:-1.2em;} .luogusp-intro-card li > input[type="checkbox"]{margin-right:.4em;} .luogusp-intro-card .code-container{margin:1rem 0;position:relative;} .luogusp-intro-card .code-container:hover>.copy-button{opacity:1;} .luogusp-intro-card .code-container:hover>.copy-button:hover{background-color:#ddd;} .luogusp-intro-card .copy-button{position:absolute;top:.5em;right:.5em;padding:.6em;display:flex;align-items:center;justify-content:center;transition:opacity .2s ease-in-out,color .2s ease-in-out,background-color .2s ease-in-out;opacity:0;background-color:transparent;border:0;border-radius:4px;cursor:pointer;color:#555;} .luogusp-intro-card .copy-button:focus{opacity:1;outline:1px solid #ddd;} .luogusp-intro-card .copy-button.copied{color:#52c41a;} .luogusp-intro-card .copy-icon{width:1em;height:1em;} .luogusp-intro-card .code-container>pre{background:#fafafa;color:#383a42;padding:1em;margin:0;overflow:auto;border-radius:.3em;} .luogusp-intro-card .code-container>pre code{font-family:"Fira Code","Fira Mono",Menlo,Consolas,"DejaVu Sans Mono",monospace;line-height:1.5;tab-size:2;} .luogusp-intro-card pre[class*="language-"],.luogusp-intro-card code[class*="language-"]{background:#fafafa;color:#383a42;font-family:"Fira Code","Fira Mono",Menlo,Consolas,"DejaVu Sans Mono",monospace;direction:ltr;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;line-height:1.5;tab-size:2;hyphens:none;} .luogusp-intro-card pre[class*="language-"]{padding:1em;margin:.5em 0;overflow:auto;border-radius:.3em;} .luogusp-intro-card .code-container>pre[class*="language-"]{margin:0;} .luogusp-intro-card pre code.hljs{display:block;overflow-x:visible;padding:0;background:transparent;} .luogusp-intro-card code.hljs{color:#383a42;background:#fafafa;} .luogusp-intro-card .hljs-comment,.luogusp-intro-card .hljs-quote{color:#a0a1a7;font-style:italic;} .luogusp-intro-card .hljs-doctag,.luogusp-intro-card .hljs-formula,.luogusp-intro-card .hljs-keyword{color:#a626a4;} .luogusp-intro-card .hljs-deletion,.luogusp-intro-card .hljs-name,.luogusp-intro-card .hljs-section,.luogusp-intro-card .hljs-selector-tag,.luogusp-intro-card .hljs-subst{color:#e45649;} .luogusp-intro-card .hljs-literal{color:#0184bb;} .luogusp-intro-card .hljs-addition,.luogusp-intro-card .hljs-attribute,.luogusp-intro-card .hljs-meta .hljs-string,.luogusp-intro-card .hljs-regexp,.luogusp-intro-card .hljs-string{color:#50a14f;} .luogusp-intro-card .hljs-attr,.luogusp-intro-card .hljs-number,.luogusp-intro-card .hljs-selector-attr,.luogusp-intro-card .hljs-selector-class,.luogusp-intro-card .hljs-selector-pseudo,.luogusp-intro-card .hljs-template-variable,.luogusp-intro-card .hljs-type,.luogusp-intro-card .hljs-variable{color:#986801;} .luogusp-intro-card .hljs-bullet,.luogusp-intro-card .hljs-link,.luogusp-intro-card .hljs-meta,.luogusp-intro-card .hljs-selector-id,.luogusp-intro-card .hljs-symbol,.luogusp-intro-card .hljs-title{color:#4078f2;} .luogusp-intro-card .hljs-built_in,.luogusp-intro-card .hljs-class .hljs-title,.luogusp-intro-card .hljs-title.class_,.luogusp-intro-card .hljs-title.function_{color:#c18401;} .luogusp-intro-card .hljs-emphasis{font-style:italic;} .luogusp-intro-card .hljs-strong{font-weight:700;} .luogusp-ide-tabbar{display:flex;gap:20px;padding:0 12px;border-bottom:1px solid #e8e8e8;flex:none;} .luogusp-ide-tab{font-size:13px;color:#606266;padding:7px 2px 5px;cursor:pointer;border-bottom:2px solid transparent;} .luogusp-ide-tab.on{color:#3498db;border-bottom-color:#3498db;font-weight:500;} .luogusp-ide-panel{overflow:auto;flex:1 1 0;min-height:0;padding:8px 12px 12px;font-size:13px;color:#333;} .luogusp-ide-head{display:flex;align-items:center;gap:10px;margin-bottom:8px;} .luogusp-ide-title{font-weight:500;} .luogusp-ide-summary{font-size:12px;color:#888;} .luogusp-ide-headbtns{margin-left:auto;display:flex;gap:6px;} .luogusp-ide-row{border:1px solid #e8e8e8;border-radius:6px;margin:0 0 8px;background:#fff;overflow:hidden;} .luogusp-ide-rowhead{display:flex;align-items:center;gap:10px;padding:8px 12px;cursor:pointer;} .luogusp-ide-rowhead:hover{background:#f7fbfe;} .luogusp-ide-chev{color:#999;width:10px;transition:transform .2s;} .luogusp-ide-row.open .luogusp-ide-chev{transform:rotate(90deg);} .luogusp-ide-meta{font-size:12px;color:#999;margin-left:auto;} .luogusp-ide-pill{font-size:12px;padding:1px 10px;border-radius:10px;border:1px solid transparent;white-space:nowrap;} .luogusp-ide-detail{display:none;border-top:1px solid #eee;background:#fcfcfc;padding:10px 12px;} .luogusp-ide-row.open .luogusp-ide-detail{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px;} .luogusp-ide-row.open .luogusp-ide-detail.luogusp-ide-log{display:block;} @media (max-width:1500px){.luogusp-ide-row.open .luogusp-ide-detail{grid-template-columns:1fr;}} .luogusp-ide-pane h5{margin:0 0 4px;font-size:12px;font-weight:500;color:#888;} .luogusp-ide-pane .code-container{margin:0;} .luogusp-ide-pane pre{margin:0;border:1px solid #e6e6e6;border-radius:4px;background:#fff;padding:6px 8px;font-size:12px;line-height:1.55;color:#333;overflow-x:auto;min-height:40px;font-family:"Fira Code","Fira Mono",Menlo,Consolas,"DejaVu Sans Mono",monospace;} .luogusp-ide-pane .luogusp-ide-diffline{background:#fcebeb;color:#a32d2d;display:block;margin:0 -8px;padding:0 8px;} .luogusp-ide-note{font-size:12px;color:#a32d2d;margin:0 0 8px;} .luogusp-ide-empty{color:#aaa;font-style:italic;} .luogusp-ide-legend{display:flex;flex-wrap:wrap;gap:6px;align-items:center;margin-top:10px;padding-top:8px;border-top:1px dashed #eee;font-size:12px;color:#999;} .luogusp-ide-panel .code-container:hover>.copy-button{opacity:1;} .luogusp-ide-panel .copy-button{position:absolute;top:.3em;right:.3em;padding:.45em;display:flex;align-items:center;justify-content:center;transition:opacity .2s;opacity:0;background:transparent;border:0;border-radius:4px;cursor:pointer;color:#555;} .luogusp-ide-panel .copy-button.copied{color:#52c41a;} .luogusp-ide-panel .copy-icon{width:1em;height:1em;} `; (document.head || document.documentElement).appendChild(style); }
function openSettings() { if (document.getElementById("luogusp-settings")) return; // 避免重复打开 const overlay = document.createElement("div"); overlay.id = "luogusp-settings"; overlay.innerHTML = ` <div class="luogusp-mask"></div> <div class="luogusp-panel" role="dialog" aria-modal="true"> <div class="luogusp-content"> <h3>LuoguSP 功能设置</h3> <div class="luogusp-list"> ${[...FEATURE_LABELS] .map( ([key, label]) => ` <label class="luogusp-item"> <input type="checkbox" data-key="${key}" ${storage.get(key) ? "checked" : ""}> <span>${label}</span> </label>`, ) .join("")} </div> <div class="luogusp-actions"> <button data-act="all">全选</button> <button data-act="none">全不选</button> <button data-act="save" class="luogusp-primary">保存</button> <button data-act="close">关闭</button> </div> <p class="luogusp-hint">保存后需刷新页面生效。</p> </div> </div>`; document.body.appendChild(overlay);
const boxes = () => overlay.querySelectorAll('input[type="checkbox"]'); const close = () => overlay.remove();
overlay.addEventListener("click", (e) => { const t = e.target; if (t.classList.contains("luogusp-mask")) return close(); const act = t.getAttribute && t.getAttribute("data-act"); if (act === "close") return close(); if (act === "all") boxes().forEach((b) => (b.checked = true)); if (act === "none") boxes().forEach((b) => (b.checked = false)); if (act === "save") { boxes().forEach((b) => storage.set(b.dataset.key, b.checked)); close(); if (confirm("设置已保存,是否立即刷新页面生效?")) location.reload(); } }); document.addEventListener("keydown", function esc(e) { if (e.key === "Escape") { close(); document.removeEventListener("keydown", esc); } }); }
// 齿轮图标(24×24 Material settings,fill=currentColor 跟随导航文字色) const GEAR_PATH = "M19.14 12.94c.04-.3.06-.61.06-.94 0-.32-.02-.64-.07-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.05.3-.09.63-.09.94s.02.64.07.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z"; // 把一个已存在的 <svg> 原地改成齿轮(保留它的 class/data-v/尺寸,只换 viewBox+内容,从而继承洛谷图标样式) function gearInto(svg) { svg.setAttribute("viewBox", "0 0 24 24"); svg.setAttribute("fill", "currentColor"); while (svg.firstChild) svg.removeChild(svg.firstChild); const path = document.createElementNS("http://www.w3.org/2000/svg", "path"); path.setAttribute("d", GEAR_PATH); svg.appendChild(path); return svg; } // 新建一个齿轮 svg(用于原条目没有 svg 图标时);templateIcon 存在则复用其 class 拿尺寸。 function newGear(templateIcon) { const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); if (templateIcon && templateIcon.getAttribute("class")) { svg.setAttribute("class", templateIcon.getAttribute("class")); } else { svg.style.width = "1.1em"; svg.style.height = "1.1em"; svg.style.marginRight = ".4em"; svg.style.verticalAlign = "middle"; } return gearInto(svg); }
const navTextSpan = (a) => a.querySelector(SELECTORS.navText);
function addSettingButton() { // 两套导航都试:首页竖排 nav.lfe-body / 内容页侧栏 nav.sidebar let nav = null, navSel = null; for (const sel of SELECTORS.navContainers) { const n = document.querySelector(sel); if (n) { nav = n; navSel = sel; break; } } if (!nav) return; // 该页无可识别导航,跳过 if (nav.querySelector(".luogusp-setting-entry")) return; // 已存在
// 选一个既有图标又有文字的原生条目当模板(取靠后的,落在工具/杂项区),克隆继承洛谷当前样式与间距。 const cands = [...nav.querySelectorAll("a")].filter( (a) => a.querySelector("svg, img, .icon") && navTextSpan(a), ); if (!cands.length) return; const template = cands[cands.length - 1]; const li = template.closest("li"); const unit = li && nav.contains(li) ? li : template; // 侧栏条目外套 <li>,连 li 一起克隆才对齐
const clone = unit.cloneNode(true); const link = clone.matches("a") ? clone : clone.querySelector("a"); if (!link) return; link.removeAttribute("href"); link.removeAttribute("id"); link.classList.remove( "router-link-active", "router-link-exact-active", "active", ); link.classList.add("luogusp-setting-entry"); link.setAttribute("role", "button"); const textEl = navTextSpan(link); if (textEl) { if (navSel === "nav.lfe-body") { // 首页竖排栏窄:强制「插件」「设置」两字两行,避免默认 3+1 难看折行 textEl.textContent = ""; textEl.append("插件", document.createElement("br"), "设置"); } else { textEl.textContent = "插件设置"; } } const svg = link.querySelector("svg"); // 原地把图标 svg 改成齿轮,保留其 class/data-v 尺寸 if (svg) gearInto(svg); else { const other = link.querySelector("img, i"); if (other) other.replaceWith(newGear(other)); } link.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); openSettings(); }); unit.parentNode.insertBefore(clone, unit.nextSibling); }
// 洛谷是 SPA:首页顶栏↔内容页侧栏随路由切换而重挂,入口须在导航变化时补上(rAF 节流,加了就早退)。 function watchSettingButton() { let scheduled = false; const tick = () => { scheduled = false; try { addSettingButton(); } catch (e) { console.error("LuoguSP setting entry:", e); } }; new MutationObserver(() => { if (!scheduled) { scheduled = true; requestAnimationFrame(tick); } }).observe(document.body, { childList: true, subtree: true }); addSettingButton(); }
// ============================================================ // 私信界面 Ctrl+Click 打开用户主页(用户名 + 头像) // ============================================================ function addMessageLink() { const bound = new WeakSet(); // 去重,避免重复绑定 const uidCache = new Map(); // username -> uid 缓存
const openUser = (uid) => { if (uid) window.open(`/user/${uid}`, "_blank"); }; // 已在用户链接里的元素,浏览器原生 Ctrl+Click 即可新标签打开,跳过避免重复触发。 const inUserLink = (el) => el.closest('a[href*="/user/"], a[href*="/space/"]');
async function getUidByName(username) { if (uidCache.has(username)) return uidCache.get(username); try { const res = await fetch( `/api/user/search?keyword=${encodeURIComponent(username)}`, ); const data = await res.json(); const uid = data && data.users && data.users[0] && data.users[0].uid; uidCache.set(username, uid); return uid; } catch (e) { console.error("LuoguSP getUid:", e); } } // 用户名触发点:Ctrl+Click → 按用户名查 uid function bindName(trigger) { if (bound.has(trigger) || inUserLink(trigger)) return; bound.add(trigger); trigger.addEventListener("click", async (e) => { if (!e.ctrlKey) return; e.preventDefault(); e.stopPropagation(); // Ctrl 时才拦,普通点击不影响洛谷原有行为 const name = (trigger.textContent || "").trim(); if (name) openUser(await getUidByName(name)); }); } // 头像:Ctrl+Click → 直接从 src(usericon/{uid})取 uid,无需查接口 const AVATAR_RE = /\/usericon\/(\d+)/; function bindAvatar(img) { if (bound.has(img) || inUserLink(img) || !AVATAR_RE.test(img.src || "")) return; bound.add(img); img.style.cursor = "pointer"; img.addEventListener("click", (e) => { if (!e.ctrlKey) return; e.preventDefault(); e.stopPropagation(); const m = (img.src || "").match(AVATAR_RE); // 点击时再读,兼容虚拟滚动换头像 if (m) openUser(m[1]); }); } const scan = (root) => { if (!root.querySelectorAll) return; root.querySelectorAll(SELECTORS.chatTrigger).forEach(bindName); root.querySelectorAll("img").forEach(bindAvatar); }; scan(document); new MutationObserver((muts) => { for (const m of muts) for (const n of m.addedNodes) if (n.nodeType === Node.ELEMENT_NODE) { if (n.matches && n.matches(SELECTORS.chatTrigger)) bindName(n); if (n.matches && n.matches("img")) bindAvatar(n); scan(n); } }).observe(document, { childList: true, subtree: true }); }
// ============================================================ // 显示隐藏的个人简介 // 洛谷把个人简介改为「仅国际站可见」,但境内站服务器仍把 introduction 下发到页面同源数据里 // (SSR 脚本 / _contentOnly 接口),只是前端不渲染。这里读同源数据自行补显,无需跨域。 // ============================================================ function digIntro(obj, wantUid) { let result = null; (function walk(o, depth) { if (result || !o || typeof o !== "object" || depth > 6) return; if (String(o.uid) === wantUid && typeof o.introduction === "string") { result = o.introduction; return; } for (const k in o) { const v = o[k]; if (v && typeof v === "object") walk(v, depth + 1); } })(obj, 0); return result; } async function getIntroduction(uid) { // 1) 整页加载:简介就在页面同源 SSR 脚本 JSON 里 for (const s of document.querySelectorAll("script")) { const t = (s.textContent || "").trim(); if (t[0] !== "{" || t.indexOf('"introduction"') === -1) continue; try { const intro = digIntro(JSON.parse(t), uid); if (intro != null) return intro; } catch (e) { /* 非纯 JSON,跳过 */ } } // 2) SPA 换页等:同源 _contentOnly 接口 try { const r = await fetch(`/user/${uid}?_contentOnly=1`); const intro = digIntro(await r.json(), uid); if (intro != null) return intro; } catch (e) { console.error("LuoguSP intro fetch:", e); } return null; } // 主渲染:优先用 @require 的 marked(真 GFM 解析器:表格+对齐/任务列表/嵌套列表/删除线/自动链接/裸 HTML 全支持) // + DOMPurify 消毒(marked 放行的裸 HTML 在此清理,XSS 安全)。数学公式仍走 KaTeX(先抽出占位,避免 marked 破坏 $ 内的 _ *)。 // marked/DOMPurify 未加载时回退内置轻量渲染器 renderMarkdownLite。样式统一蹭洛谷 .lfe-marked-wrap。 function renderMarkdown(md) { const mk = window.marked, dp = window.DOMPurify; if (!mk || !dp) return renderMarkdownLite(md); // 库未加载 → 回退轻量渲染器(本身 XSS 安全) const kx = (typeof window !== "undefined" && window.katex) || (typeof katex !== "undefined" && katex) || null; const tt = (f, d) => { if (!kx) return null; try { return kx.renderToString(f, { throwOnError: false, displayMode: d }); } catch (e) { return null; } }; const math = []; const hold = (h) => `%%LGMATH${math.push(h) - 1}%%`; // 数学占位:纯文本,过 marked/DOMPurify 不变,最后回填 const src = md .replace(/\$\$([\s\S]+?)\$\$/g, (m, f) => { const h = tt(f.trim(), true); return h ? hold(h) : m; }) .replace(/(?<!\\)\$([^\n$]+?)\$/g, (m, f) => { const h = tt(f, false); return h ? hold(h) : m; }); let html; try { html = mk.parse(src, { gfm: true, breaks: true }); } catch (e) { return renderMarkdownLite(md); } html = dp.sanitize(html, { ADD_ATTR: ["target"] }); // 消毒:剥离 script/on*/javascript: 等 return html .replace(/<a /gi, '<a target="_blank" rel="noopener noreferrer" ') // 链接新标签打开 .replace(/<img /gi, '<img style="max-width:100%" ') // 图片限宽防溢出 .replace(/%%LGMATH(\d+)%%/g, (_, i) => math[i]); // 回填 KaTeX(可信,消毒后再插) }
// 内置轻量 Markdown → HTML(marked 未加载时的回退;XSS 安全:转义 HTML 实体防注入;URL 仅允许 http(s)/相对,挡 javascript:; // 洛谷允许的少量裸 HTML 走白名单消毒:img/a 校验 URL、安全内联标签去所有属性防 on* 注入、其余标签转义成文本)。 // 覆盖:段落/换行、ATX+setext 标题、加粗/斜体、删除线、行内与围栏代码、链接、图片、无序/有序列表、引用、分割线、表格、 // KaTeX 公式($..$ / $$..$$)、白名单裸 HTML。不覆盖:任务列表、表格对齐、裸 URL 自动链接、嵌套列表。 function renderMarkdownLite(md) { const esc = (s) => s.replace( /[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c], ); const url = (u) => (/^(https?:)?\/\//i.test(u) || /^\//.test(u) ? u : ""); const codeLanguageClass = (raw) => { const lang = (raw || "").trim().split(/\s+/)[0].toLowerCase(); return /^[a-z0-9_+-]+$/.test(lang) ? ` class="language-${esc(lang)}"` : ""; }; const kx = (typeof window !== "undefined" && window.katex) || (typeof katex !== "undefined" && katex) || null; const tex = (f, display) => { if (!kx) return null; try { return kx.renderToString(f, { throwOnError: false, displayMode: display, }); } catch (e) { return null; } }; const spans = []; // 抽出的「原样片段」(代码/公式/白名单裸 HTML),最后回填 const stash = (html) => `@@LGB${spans.push(html) - 1}@@`; // 占位符:printable、不含 esc 目标字符、正常文本不会出现 const ga = (t, re) => (t.match(re) || [])[1] || ""; // 取标签属性值 const inline = (s) => s .replace(/`([^`]+)`/g, (_, c) => `<code>${c}</code>`) .replace(/!\[([^\]]*)\]\(([^)\s]+)[^)]*\)/g, (m, a, u) => { const x = url(u); return x ? `<img src="${x}" alt="${a}" style="max-width:100%">` : m; }) .replace(/\[([^\]]+)\]\(([^)\s]+)[^)]*\)/g, (m, t, u) => { const x = url(u); return x ? `<a href="${x}" target="_blank" rel="noopener noreferrer">${t}</a>` : m; }) .replace(/~~([^~]+)~~/g, "<del>$1</del>") .replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>") .replace(/(^|[^*])\*([^*\n]+)\*/g, "$1<em>$2</em>"); const cells = (row) => row .trim() .replace(/^\||\|$/g, "") .split("|") .map((c) => c.trim()); const table = (lines) => `<table><thead><tr>${cells(lines[0]) .map((c) => `<th>${inline(c)}</th>`) .join("")}</tr></thead><tbody>${lines .slice(2) .map( (l) => `<tr>${cells(l) .map((c) => `<td>${inline(c)}</td>`) .join("")}</tr>`, ) .join("")}</tbody></table>`; const SAFE = /^(b|strong|i|em|u|s|del|ins|mark|sub|sup|br|hr|code|kbd|small)$/i; // 允许原样的安全内联标签 // esc 前抽出原样片段:① 围栏代码 ② 白名单裸 HTML(洛谷允许少量 HTML)③ 数学公式(含 <>&\ 会被 esc 破坏) let src = md .replace(/```([^\n]*)\n?([\s\S]*?)```/g, (_, rawLang, c) => { const cls = codeLanguageClass(rawLang); return stash( `<pre${cls}><code${cls}>${esc(c.replace(/\n$/, ""))}</code></pre>`, ); }) .replace(/<img\b[^>]*>/gi, (t) => { const x = url(ga(t, /\bsrc\s*=\s*["']?([^"'\s>]+)/i)); return x ? stash( `<img src="${esc(x)}" alt="${esc(ga(t, /\balt\s*=\s*["']([^"']*)["']/i))}" style="max-width:100%">`, ) : ""; }) .replace(/<a\b[^>]*>/gi, (t) => { const x = url(ga(t, /\bhref\s*=\s*["']?([^"'\s>]+)/i)); return stash( x ? `<a href="${esc(x)}" target="_blank" rel="noopener noreferrer">` : "<span>", ); }) .replace(/<\/a>/gi, () => stash("</a>")) .replace(/<(\/?)([a-z][a-z0-9]*)\b[^>]*>/gi, (t, sl, nm) => SAFE.test(nm) ? stash(`<${sl}${nm.toLowerCase()}>`) : t, ) // 白名单标签去所有属性防 on*;其余留原样→后面 esc 成文本 .replace(/\$\$([\s\S]+?)\$\$/g, (m, f) => { const h = tex(f.trim(), true); return h ? stash(h) : m; }) .replace(/(?<!\\)\$([^\n$]+?)\$/g, (m, f) => { const h = tex(f, false); return h ? stash(h) : m; }); src = esc(src) // 其余内容转义(占位符无 esc 目标字符,存活) .replace(/^([^\n]+)\n=+[ \t]*$/gm, "# $1") // setext h1(文本下一行 ===) .replace(/^([^\n]+)\n-{2,}[ \t]*$/gm, "## $1") // setext h2(文本下一行 ---) .replace(/^(#{1,6}[ \t]+.+)$/gm, "\n\n$1\n\n") // ATX 标题是行级构造,补空行独立成块(否则和正文粘一块被漏) .replace(/^[ \t]*([-*_])\1{2,}[ \t]*$/gm, "\n\n$1$1$1\n\n"); // hr 独立成块 const html = src .split(/\n{2,}/) .map((block) => { const b = block.trim(); if (/^@@LGB\d+@@$/.test(b)) return b; // 独占一段的占位(代码块 / 行间公式) const lines = block.split("\n"); if ( lines.length >= 2 && /^\s*\|.*\|\s*$/.test(lines[0]) && /^\s*\|[\s:|-]+\|\s*$/.test(lines[1]) ) return table(lines); const h = b.match(/^(#{1,6})[ \t]+(.+)$/); if (h && !b.includes("\n")) return `<h${h[1].length}>${inline(h[2])}</h${h[1].length}>`; if (/^([-*_])\1{2,}$/.test(b)) return "<hr>"; if (lines.every((l) => /^\s*[-*+]\s+/.test(l))) return `<ul>${lines.map((l) => `<li>${inline(l.replace(/^\s*[-*+]\s+/, ""))}</li>`).join("")}</ul>`; if (lines.every((l) => /^\s*\d+\.\s+/.test(l))) return `<ol>${lines.map((l) => `<li>${inline(l.replace(/^\s*\d+\.\s+/, ""))}</li>`).join("")}</ol>`; if (lines.every((l) => /^>\s?/.test(l))) return `<blockquote>${inline(lines.map((l) => l.replace(/^>\s?/, "")).join("<br>"))}</blockquote>`; // > 已被 esc 转成 > return `<p>${inline(block).replace(/\n/g, "<br>")}</p>`; }) .join(""); return html.replace(/@@LGB(\d+)@@/g, (_, i) => spans[i]); // 回填占位(inline 公式也在此步) } function normalizeCodeLanguageClass(code) { const lang = [...code.classList].find((c) => c.startsWith("language-")); const pre = code.closest("pre"); if (lang && pre) pre.classList.add(lang); } function highlightCodeBlocks(root) { const highlighter = (typeof window !== "undefined" && window.hljs) || (typeof hljs !== "undefined" && hljs) || null; root.querySelectorAll("pre code").forEach((code) => { normalizeCodeLanguageClass(code); if (!highlighter || typeof highlighter.highlightElement !== "function") return; if (code.dataset.luoguspHighlighted === "true") return; try { highlighter.highlightElement(code); code.dataset.luoguspHighlighted = "true"; normalizeCodeLanguageClass(code); } catch (e) { console.error("LuoguSP highlight:", e); } }); } const COPY_ICON_PATH = "M192 0c-35.3 0-64 28.7-64 64l0 256c0 35.3 28.7 64 64 64l192 0c35.3 0 64-28.7 64-64l0-200.6c0-17.4-7.1-34.1-19.7-46.2L370.6 17.8C358.7 6.4 342.8 0 326.3 0L192 0zM64 128c-35.3 0-64 28.7-64 64L0 448c0 35.3 28.7 64 64 64l192 0c35.3 0 64-28.7 64-64l0-16-64 0 0 16-192 0 0-256 16 0 0-64-16 0z"; function copyText(text) { if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text); const ta = document.createElement("textarea"); ta.value = text; ta.setAttribute("readonly", ""); ta.style.cssText = "position:fixed;left:-9999px;top:-9999px;"; document.body.appendChild(ta); ta.select(); try { document.execCommand("copy"); return Promise.resolve(); } finally { ta.remove(); } } function makeCopyButton(code) { const button = document.createElement("button"); button.type = "button"; button.className = "copy-button"; button.setAttribute("aria-label", "复制代码"); button.title = "复制代码"; button.innerHTML = `<svg class="svg-inline--fa fa-copy copy-icon" data-prefix="fas" data-icon="copy" role="img" viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="${COPY_ICON_PATH}"></path></svg>`; button.addEventListener("click", async (e) => { e.preventDefault(); e.stopPropagation(); try { await copyText(code.textContent || ""); button.classList.add("copied"); button.title = "已复制"; setTimeout(() => { button.classList.remove("copied"); button.title = "复制代码"; }, 900); } catch (err) { console.error("LuoguSP copy code:", err); } }); return button; } function enhanceCodeBlocks(root) { root.querySelectorAll("pre").forEach((pre) => { if (pre.closest(".code-container")) return; const code = pre.querySelector("code"); if (!code) return; const box = document.createElement("div"); box.className = "code-container"; pre.parentNode.insertBefore(box, pre); box.append(pre, makeCopyButton(code)); }); } // 卡片外观参考国际站:浅克隆一张原生 .l-card + .header 拿到带 data-v 的作用域样式(裸 class 无边框/背景), // 内容套 .lfe-marked-wrap.introduction 走洛谷原生 Markdown 样式;追加到 .main 末尾(国际站里简介就是最后一张卡)。 function renderIntroCard(col, intro) { // 浅克隆一张原生 .l-card 拿带 data-v 的作用域外观(圆角/白底·跟随主题),className 重置只留 l-card 去掉其它卡专属类 const nativeCard = document.querySelector(".l-card"); const card = nativeCard ? nativeCard.cloneNode(false) : document.createElement("div"); card.className = "l-card luogusp-intro-card"; card.removeAttribute("id"); card.removeAttribute("style"); // ★清掉克隆源卡的内联样式:某些卡带 --l-card--padding:0 会让内容贴框 card.style.setProperty("--l-card--padding", "20.8px"); // 用国际站 intro 卡的内边距(洛谷 .l-card 靠此变量控制) // 头部:用国际站原生结构(.header > h3[margin=0]),让标题继承浏览器 h3 的字号与粗细。 const header = document.createElement("div"); header.className = "header"; const title = document.createElement("h3"); title.textContent = "个人介绍"; title.style.margin = "0px"; header.appendChild(title); const body = document.createElement("div"); body.className = "lfe-marked-wrap introduction"; // 外层容器;同时被 nativeIntro 检测用 :not 排除 body.style.cssText = "overflow-wrap:break-word;word-break:break-word;"; const content = document.createElement("div"); content.className = "lfe-marked"; // ★洛谷 markdown 样式(标题下边框/hr/列表间距等)全局作用域在 .lfe-marked,内容必须套此层 content.innerHTML = renderMarkdown(intro); // renderMarkdown 已消毒防 XSS highlightCodeBlocks(content); enhanceCodeBlocks(content); body.appendChild(content); card.append(header, body); col.appendChild(card); } async function showHiddenIntro() { const m = location.pathname.match(/^\/(?:user|space)\/(\d+)/); if (!m) return; const route = currentUserRoute(); if (!route.isHome) return; const uid = m[1]; document.querySelectorAll(".luogusp-intro-card").forEach((e) => e.remove()); // 清换页残留 if (document.querySelector(SELECTORS.nativeIntro)) return; // 原生已显示,不重复补 const intro = await getIntroduction(uid); if (!intro || !intro.trim()) return; const place = () => { if (document.querySelector(".introduction:not(.luogusp-intro-card *)")) return true; // 原生简介已出现(管理员等)→ 别补 if (document.querySelector(".luogusp-intro-card")) return true; const col = document.querySelector(SELECTORS.userIntroColumn); // 只挂内层内容列,绝不回退外层全宽(否则内容顶到最左被裁) if (!col) return false; renderIntroCard(col, intro); return true; }; if (place()) return; // 内容列尚未渲染(SPA 换页):等它出现再补,8s 后放弃 const obs = new MutationObserver(() => { if (place()) obs.disconnect(); }); obs.observe(document.body, { childList: true, subtree: true }); setTimeout(() => obs.disconnect(), 8000); } // SPA 换页时 URL 变但脚本不重跑:监听用户主页 uid 变化补显。 function currentUserRoute() { const m = location.pathname.match(/^\/(?:user|space)\/(\d+)/); const hash = location.hash || ""; return { uid: m ? m[1] : "", key: m ? `${location.pathname}${location.search}${hash}` : "", isHome: !!m && (!hash || hash === "#" || hash === "#home" || hash === "#main"), }; } function watchUrlChange(onChange) { if (!history.pushState._luoguspWrapped) { for (const method of ["pushState", "replaceState"]) { const raw = history[method]; const wrapped = function (...args) { const ret = raw.apply(this, args); window.dispatchEvent(new Event("luogusp:urlchange")); return ret; }; wrapped._luoguspWrapped = true; history[method] = wrapped; } } window.addEventListener("popstate", onChange); window.addEventListener("hashchange", onChange); window.addEventListener("luogusp:urlchange", onChange); } function watchHiddenIntro() { let requestedRouteKey = ""; const check = () => { const route = currentUserRoute(); const uid = route.uid; // 原生简介出现(管理员等原生可见)→ 移除我的卡,避免重复渲染 if (document.querySelector(".introduction:not(.luogusp-intro-card *)")) { document .querySelectorAll(".luogusp-intro-card") .forEach((e) => e.remove()); requestedRouteKey = route.key; return; } if (!uid || !route.isHome) { document .querySelectorAll(".luogusp-intro-card") .forEach((e) => e.remove()); requestedRouteKey = ""; return; } if (document.querySelector(".luogusp-intro-card")) { requestedRouteKey = route.key; return; } if (route.key !== requestedRouteKey) { requestedRouteKey = route.key; showHiddenIntro(); } }; check(); let scheduled = false; const queueCheck = () => { if (!scheduled) { scheduled = true; requestAnimationFrame(() => { scheduled = false; check(); }); } }; new MutationObserver(queueCheck).observe(document.body, { childList: true, subtree: true, }); watchUrlChange(queueCheck); }
// ============================================================ // 题目难度着色 // ============================================================ class FetchRateLimiter { // launchGap:相邻两次发起 fetch 的最小间隔(越小越快,但过快可能被洛谷限流) // concurrency:最大同时在飞请求数(并发上限,网络慢时防堆积) // 单队列 FIFO:按入队顺序处理 = 页面 DOM 顺序(主列表在前先染,讨论标签最后)。 constructor(launchGap, concurrency) { this.launchGap = launchGap; this.concurrency = concurrency; this.queue = []; this.active = 0; this.nextAt = 0; this.cache = new Map(); } _drain() { while (this.active < this.concurrency && this.queue.length) { const wait = this.nextAt - Date.now(); if (wait > 0) { setTimeout(() => this._drain(), wait); return; } // 距上次发起未满 launchGap this.nextAt = Date.now() + this.launchGap; const job = this.queue.shift(); this.active++; fetch(job.url) .then(job.resolve, job.reject) .finally(() => { this.active--; this._drain(); }); } } fetchText(url) { if (this.cache.has(url)) return this.cache.get(url); const p = new Promise((resolve, reject) => { this.queue.push({ url, resolve, reject }); this._drain(); }).then((res) => res.text()); this.cache.set(url, p); return p; } } const limiter = new FetchRateLimiter(200, 3); // 200ms 发起间隔、最多 3 并发(原为 300ms 串行)
const colorCache = new Map(); // pid -> css 颜色 const DIFF_RE = /"difficulty":\s*(\d+)/; // HTML 回退用 let contentOnlyOk = null; // null 未知 / true 支持 / false 不支持
// 优先走 _contentOnly JSON 接口(轻量、结构稳定),失败回退整页 HTML 正则。 async function fetchDifficulty(pid) { if (contentOnlyOk !== false) { let text; try { text = await limiter.fetchText(`/problem/${pid}?_contentOnly=1`); } catch (e) { /* 网络错误,别据此判定接口不支持 */ } if (text != null) { try { const d = JSON.parse(text)?.currentData?.problem?.difficulty; if (typeof d === "number") { contentOnlyOk = true; return d; } } catch (e) { if (contentOnlyOk === null) contentOnlyOk = false; // 拿到的是整页 HTML,判定不支持 } } } try { const html = await limiter.fetchText(`/problem/${pid}`); const m = html.match(DIFF_RE); if (m) return Number(m[1]); } catch (e) { console.error("LuoguSP difficulty:", pid, e); } return null; }
// 记录列表 / 练习页:洛谷已把整批题目难度注入 _feInstance,直接批量取,省去逐题抓取。 function cacheDifficulty(pid, difficulty) { if (pid && typeof difficulty === "number") colorCache.set(pid, diffColor(difficulty)); } function harvestFromFeInstance() { const cur = window._feInstance && window._feInstance.currentData; if (!cur) return; const url = location.href; if (url.startsWith("https://www.luogu.com.cn/record/list")) { const list = cur.records && cur.records.result; if (list && !list._luogusp) { for (const it of list) cacheDifficulty( it.problem && it.problem.pid, it.problem && it.problem.difficulty, ); list._luogusp = true; } } if (/^https:\/\/www\.luogu\.com\.cn\/user\/\d+#practice$/.test(url)) { for (const key of ["submittedProblems", "passedProblems"]) { const list = cur[key]; if (list && !list._luogusp) { for (const it of list) cacheDifficulty(it.pid, it.difficulty); list._luogusp = true; } } } }
async function getProblemColor(pid) { harvestFromFeInstance(); if (colorCache.has(pid)) return colorCache.get(pid); const d = await fetchDifficulty(pid); if (d == null) return null; const color = diffColor(d); colorCache.set(pid, color); return color; }
function isProblemId(id) { if (id.startsWith("AT_")) return true; return /[a-zA-Z]/.test(id) && /[0-9]/.test(id); }
// 把子树中第一处 pid 文本包成 <b>(纯 DOM,避免 innerHTML.replace 误伤属性内同名子串、 // 重建整个锚点子树、抖掉已绑定的监听)。返回是否成功包裹。 function wrapPidText(root, pid, color) { const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); let node; while ((node = walker.nextNode())) { const idx = node.nodeValue.indexOf(pid); if (idx === -1) continue; const mid = node.splitText(idx); // mid 以 pid 开头 mid.splitText(pid.length); // 把 pid 之后的部分切走,mid 现在正好等于 pid const b = document.createElement("b"); b.style.color = color; b.textContent = pid; mid.parentNode.replaceChild(b, mid); return true; } return false; }
async function colorAnchor(a) { let pid = a.href.split("/").pop(); let isForum = false; if (pid.includes("?forum=")) { pid = pid.split("=").pop(); isForum = true; } pid = pid.split("?")[0].split("=").pop(); let isVoid = false; if (a.matches(SELECTORS.voidAnchor) && pid === "javascript:void 0") { pid = a.innerText.split(" ")[0]; isVoid = true; } if (!isProblemId(pid)) return; // 只处理确属题目的锚点——/problem/ 链接、讨论区 ?forum= 标签、void 特链。 // 避免把 /paste/xxx、/user/xxx 等「字母+数字」尾段误判成题号而空跑请求。 if (!isForum && !isVoid && !a.href.includes("/problem/")) return; if (!a.innerText.startsWith(pid)) return; if (a.dataset.luoguspPid === pid) return; // 已着色,跳过(防重复处理/嵌套 <b>)
const span = a.children[0]; if (span && span.matches("span.pid") && span.innerText === pid) { const color = await getProblemColor(pid); if (color) { span.style.color = color; span.style.fontWeight = "bold"; a.dataset.luoguspPid = pid; } } else { const color = await getProblemColor(pid); if (color && wrapPidText(a, pid, color)) a.dataset.luoguspPid = pid; } }
function addProblemsColor() { new MutationObserver(async (muts) => { for (const m of muts) { if (m.type === "childList") { for (const n of m.addedNodes) { if (n.nodeType !== Node.ELEMENT_NODE) continue; if (n.matches && n.matches("a[href]")) colorAnchor(n); if (n.querySelectorAll) n.querySelectorAll("a[href]").forEach(colorAnchor); } } else if (m.type === "characterData") { const span = m.target.parentElement; if (span && span.matches && span.matches("span.pid")) { const color = await getProblemColor(span.textContent); if (color) { span.style.color = color; span.style.fontWeight = "bold"; } } } } }).observe(document, { childList: true, subtree: true, characterData: true, });
document.querySelectorAll("a[href]").forEach(colorAnchor); }
// ============================================================ // IDE 一键测试样例 // 洛谷新版题目页(columba)IDE 模式(#ide)下,逐组驱动题面样例的原生「运行」, // 结果从输出面板 DOM 捕获(结果经页面常驻 WS 推送,网络层拿不到——勿改走拦截)。 // 锚点与配色的事实来源:docs/superpowers/specs/2026-07-22-ide-mode-recon-notes.md // ============================================================ function ideToolbarByTitle(title) { for (const tb of document.querySelectorAll(SELECTORS.ideToolbar)) { const t = tb.querySelector(SELECTORS.ideToolbarText); if (t && t.textContent.trim() === title) return tb; } return null; }
const IDE_BATCH = { running: false, // 批测进行中(防重入) stopReq: false, // 「停止」请求:当前组跑完即停 driving: false, // 程序化点击原生按钮的瞬间为 true(区分用户手点) activeTab: "custom", // custom=原生输入输出 / samples=样例面板 tabBar: null, panel: null, ioLayout: null, // 原生 输入|输出 水平分栏(tab 切换时显隐) rowsEl: null, summaryEl: null, stopBtn: null, results: null, // 本轮各组结果(过期标注用) stale: false, inputSnapshot: null, // 批测前用户自定义输入快照 };
function ideModeActive() { return ( location.hash === "#ide" && !!document.querySelector(SELECTORS.ideToolbar) ); }
function lentilleProblem() { try { const el = document.querySelector(SELECTORS.lentilleContext); if (!el) return null; const json = JSON.parse(el.textContent); return (json && json.data && json.data.problem) || null; } catch (e) { return null; } } function currentPid() { const m = location.pathname.match(/^\/problem\/([A-Za-z0-9_]+)/); return m ? m[1] : ""; } async function getIdeSamples() { const pid = currentPid(); if (!pid) return null; const p = lentilleProblem(); if (p && p.pid === pid && Array.isArray(p.samples)) return p.samples; // SPA 换题后 lentille-context 滞留旧题(真机实测)→ 新版内容接口兜底。 // 注意:旧 `?_contentOnly=1` 在 columba 页面已死(返回整页 HTML), // 正确姿势是带 x-lentille-request 头(真机实测 2026-07-22)。 try { const res = await fetch(`/problem/${pid}`, { headers: { "x-lentille-request": "content-only" }, }); const json = await res.json(); const prob = json && json.data && json.data.problem; if (prob && Array.isArray(prob.samples)) return prob.samples; } catch (e) { console.error("LuoguSP ide samples:", e); } return null; } function sampleRunButtons() { // 「输入 #N」「输出 #N」各一块都带「运行」;只取输入块的,按 DOM 序=样例序 const btns = []; for (const block of document.querySelectorAll(SELECTORS.ideSampleBlock)) { if (!/^(输入|Input)/i.test((block.textContent || "").trim())) continue; const run = [...block.querySelectorAll("a, button")].find( (b) => (b.textContent || "").trim() === "运行", ); if (run) btns.push(run); } return btns; } function readIdeCode() { const content = document.querySelector(SELECTORS.cmContent); if (!content) return ""; // 洛谷构建把 CM6 的 cmView 命名为 cmTile;拿不到就退化为可见文本(空代码检测够用) const view = content.cmTile && content.cmTile.view; if (view && view.state && view.state.doc) return view.state.doc.toString(); return content.textContent || ""; }
let ideSubmitWaiter = null; function installIdeSubmitObserver() { if (XMLHttpRequest.prototype.open.__luoguspIde) return; const rawOpen = XMLHttpRequest.prototype.open; const rawSend = XMLHttpRequest.prototype.send; const open = function (method, url) { this.__luoguspIdeSubmit = typeof url === "string" && url.indexOf("/api/ide_submit") !== -1; return rawOpen.apply(this, arguments); }; const send = function () { if (this.__luoguspIdeSubmit) this.addEventListener("loadend", () => { if (ideSubmitWaiter) { const w = ideSubmitWaiter; ideSubmitWaiter = null; w(this.status); } }); return rawSend.apply(this, arguments); }; open.__luoguspIde = true; XMLHttpRequest.prototype.open = open; XMLHttpRequest.prototype.send = send; } function waitIdeSubmit(ms) { return new Promise((resolve) => { const timer = setTimeout(() => { if (ideSubmitWaiter === fn) ideSubmitWaiter = null; resolve(null); }, ms); const fn = (status) => { clearTimeout(timer); resolve(status); }; ideSubmitWaiter = fn; }); } const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
function outputParts() { const tb = ideToolbarByTitle("输出"); if (!tb) return null; const actions = tb.querySelector(SELECTORS.ideToolbarActions); const spans = actions ? [...actions.querySelectorAll("span")] : []; return { pill: spans.find((s) => !s.classList.contains("run-result")) || null, rr: actions ? actions.querySelector(SELECTORS.ideRunResult) : null, textarea: tb.parentElement ? tb.parentElement.querySelector(SELECTORS.ideTextarea) : null, }; } // 完成锚点:胶囊 存在→消失→重现(实测清空 300~560ms、结果 1~3.5s;详见侦察实录) // 注意:此处不看 stopReq——设计口径是「当前组跑完即停」,停止只在组间生效 async function waitIdePill(present, timeoutMs) { const t0 = Date.now(); while (Date.now() - t0 < timeoutMs) { const parts = outputParts(); if (!parts) return null; // IDE 已卸载 if (!!parts.pill === present) return parts.pill || true; await sleep(150); } return null; }
async function runOneSample(runBtn) { const before = outputParts(); if (!before) return { verdict: "UKE", note: "IDE 面板不存在" }; const hadPill = !!before.pill; let submitP = waitIdeSubmit(10000); IDE_BATCH.driving = true; runBtn.click(); IDE_BATCH.driving = false; let status = await submitP; if (status === 429) { await sleep(3000); // 限流:等 3s 原地重试一次 submitP = waitIdeSubmit(10000); IDE_BATCH.driving = true; runBtn.click(); IDE_BATCH.driving = false; status = await submitP; } if (status == null || status < 200 || status >= 300) return { verdict: "UKE", note: status == null ? "未观测到提交请求" : `提交失败 HTTP ${status}`, }; if (hadPill && (await waitIdePill(false, 5000)) === null) return { verdict: "UKE", note: "旧结果未清空,疑似运行未开始" }; const pill = await waitIdePill(true, 30000); if (!pill || pill === true) return { verdict: "UKE", note: "30s 未返回结果" }; const parts = outputParts(); return { verdict: (pill.textContent || "").trim() || "UKE", pillStyle: pill.getAttribute("style") || "", detail: parts.rr ? parts.rr.textContent.trim() : "", output: parts.textarea ? parts.textarea.value : "", }; }
function ideBatchHint(msg) { const btn = document.querySelector(".luogusp-ide-batch-btn"); if (!btn) return; const old = btn.textContent; btn.textContent = msg; btn.disabled = true; setTimeout(() => { btn.textContent = old; btn.disabled = IDE_BATCH.running; }, 1500); } async function startIdeBatch() { if (IDE_BATCH.running) return; mountIdeTabs(); const samples = await getIdeSamples(); if (!samples || !samples.length) return ideBatchHint("本题无样例"); if (!readIdeCode().trim()) return ideBatchHint("代码为空"); const runBtns = sampleRunButtons(); if (!runBtns.length) return ideBatchHint("找不到样例运行按钮"); const n = Math.min(samples.length, runBtns.length); if (runBtns.length !== samples.length) console.error( "LuoguSP ide batch: 样例数与运行按钮数不一致", samples.length, runBtns.length, ); IDE_BATCH.running = true; IDE_BATCH.stopReq = false; IDE_BATCH.stale = false; IDE_BATCH.results = new Array(n).fill(null); const inputTa = (() => { const tb = ideToolbarByTitle("输入"); return tb && tb.parentElement ? tb.parentElement.querySelector(SELECTORS.ideTextarea) : null; })(); IDE_BATCH.inputSnapshot = inputTa ? inputTa.value : null; const batchBtn = document.querySelector(".luogusp-ide-batch-btn"); const selfTest = (() => { const tb = ideToolbarByTitle("代码"); const actions = tb && tb.querySelector(SELECTORS.ideToolbarActions); return actions ? [...actions.querySelectorAll("button")].find( (b) => (b.textContent || "").trim() === "自测", ) : null; })(); if (batchBtn) batchBtn.disabled = true; if (selfTest) selfTest.disabled = true; // 批测中禁原生自测防互相干扰 if (IDE_BATCH.stopBtn) IDE_BATCH.stopBtn.style.display = ""; switchIdeTab("samples"); renderIdeRows(samples); if (IDE_BATCH.summaryEl) IDE_BATCH.summaryEl.textContent = "测试中…"; let ceLog = null; for (let i = 0; i < n; i++) { if (IDE_BATCH.stopReq) break; const p = ideRowParts(i); if (p) { p.pill.setAttribute("style", IDE_PILL_RUN); p.pill.textContent = "运行中"; } expandIdeRow(i); let r; try { r = await runOneSample(runBtns[i]); } catch (e) { console.error("LuoguSP ide batch:", e); r = { verdict: "UKE", note: String(e) }; } IDE_BATCH.results[i] = r; applyIdeResult(i, r, samples[i]); if (r.verdict === "CE") { ceLog = r.output || ""; for (let j = i + 1; j < n; j++) { IDE_BATCH.results[j] = { verdict: "CE", output: ceLog, note: "编译错误", }; applyIdeResult(j, IDE_BATCH.results[j], samples[j]); } break; } if (i < n - 1 && !IDE_BATCH.stopReq) await sleep(500); // 组间限速 } if (inputTa && IDE_BATCH.inputSnapshot != null) { inputTa.value = IDE_BATCH.inputSnapshot; // 还原用户自定义输入 inputTa.dispatchEvent(new Event("input", { bubbles: true })); // 同步 Vue 绑定 } IDE_BATCH.running = false; if (batchBtn) batchBtn.disabled = false; if (selfTest) selfTest.disabled = false; if (IDE_BATCH.stopBtn) IDE_BATCH.stopBtn.style.display = "none"; finishIdeSummary(); }
// 判定口径同洛谷:CRLF 归一、去行尾空格、去末尾空行。仅用于 diff 渲染与交叉校验, // 最终判定以原生胶囊为准(AC/WA 由洛谷前端本地比较,侦察实录 §6)。 function normalizeIdeOut(s) { return String(s == null ? "" : s) .replace(/\r\n?/g, "\n") .split("\n") .map((l) => l.replace(/[ \t]+$/, "")) .join("\n") .replace(/\n+$/, ""); } function idePane(title, lines, badSet, emptyNote) { const pre = document.createElement("pre"); if (!lines.length || (lines.length === 1 && lines[0] === "")) { if (emptyNote) { const span = document.createElement("span"); span.className = "luogusp-ide-empty"; span.textContent = emptyNote; pre.appendChild(span); } } else { lines.forEach((l, k) => { const span = document.createElement("span"); if (badSet && badSet.has(k)) span.className = "luogusp-ide-diffline"; span.textContent = l; pre.appendChild(span); if (k < lines.length - 1) pre.appendChild(document.createTextNode("\n")); }); } const box = document.createElement("div"); box.className = "code-container"; box.append(pre, makeCopyButton(pre)); const pane = document.createElement("div"); pane.className = "luogusp-ide-pane"; const h = document.createElement("h5"); h.textContent = title; pane.append(h, box); return pane; } function applyIdeResult(i, r, sample) { const p = ideRowParts(i); if (!p) return; p.pill.textContent = r.verdict; p.pill.setAttribute( "style", r.pillStyle || "background-color:#3d3d3d;border-color:#333;color:#fff;", ); p.detail.innerHTML = ""; p.detail.classList.remove("luogusp-ide-log"); if (r.verdict === "CE") { // CE:不显示三栏,直接展示编译日志(输出框捕获;侦察实录 §5) p.meta.textContent = ""; p.detail.classList.add("luogusp-ide-log"); p.detail.appendChild( idePane( "编译信息", String(r.output || "").split("\n"), null, "(无编译输出)", ), ); return; } p.meta.textContent = r.detail || ""; if (r.note) { const note = document.createElement("p"); note.className = "luogusp-ide-note"; note.textContent = r.note; p.detail.appendChild(note); if (r.output == null) { p.detail.classList.add("luogusp-ide-log"); // UKE 无产物,只留说明 return; } } if (r.verdict === "RE" && r.detail) { const note = document.createElement("p"); note.className = "luogusp-ide-note"; note.textContent = r.detail; // RE 原因在 run-result 位置(侦察实录 §5) p.detail.appendChild(note); p.meta.textContent = ""; } const expLines = normalizeIdeOut(sample[1]).split("\n"); const actLines = normalizeIdeOut(r.output).split("\n"); const bad = new Set(); if (r.verdict !== "AC") { const m = Math.max(expLines.length, actLines.length); for (let k = 0; k < m; k++) if ((expLines[k] || "") !== (actLines[k] || "")) bad.add(k); } p.detail.append( idePane("输入", normalizeIdeOut(sample[0]).split("\n"), null, "(空)"), idePane("期望输出", expLines, bad, "(空)"), idePane( "实际输出", actLines, bad, r.verdict === "AC" ? "(空)" : "(未产生输出)", ), ); }
// 实测原生内联配色(侦察实录 §5);MLE/OLE/UKE 未实测,行内以运行时复制为准 const IDE_LEGEND = [ ["AC", "rgb(83,196,26)", "rgb(80,161,39)", "#fff"], ["WA", "rgb(231,77,60)", "rgb(208,69,53)", "#fff"], ["TLE", "rgb(5,34,66)", "rgb(10,31,54)", "#fff"], ["RE", "rgb(156,61,207)", "rgb(138,62,179)", "#fff"], ["CE", "rgb(250,219,20)", "rgb(215,190,28)", "#614700"], ["UKE", "#3d3d3d", "#333", "#fff"], ]; function finishIdeSummary() { if (!IDE_BATCH.summaryEl || !IDE_BATCH.results) return; const rs = IDE_BATCH.results; const counts = {}; let ac = 0, tested = 0; rs.forEach((r) => { if (!r) return; tested++; if (r.verdict === "AC") ac++; else counts[r.verdict] = (counts[r.verdict] || 0) + 1; }); let text = `${ac}/${rs.length} 通过`; for (const k in counts) text += ` · ${counts[k]} ${k}`; if (tested < rs.length) { text += " · 已停止"; rs.forEach((r, i) => { if (r) return; const p = ideRowParts(i); if (p) p.pill.textContent = "未测"; }); } IDE_BATCH.summaryEl.textContent = text; const firstBad = rs.findIndex((r) => r && r.verdict !== "AC"); if (firstBad !== -1) expandIdeRow(firstBad); else if (IDE_BATCH.rowsEl) IDE_BATCH.rowsEl .querySelectorAll(".luogusp-ide-row.open") .forEach((r) => r.classList.remove("open")); }
function markIdeStale() { if (IDE_BATCH.stale || IDE_BATCH.running || !IDE_BATCH.results) return; IDE_BATCH.stale = true; if (IDE_BATCH.summaryEl && document.contains(IDE_BATCH.summaryEl)) IDE_BATCH.summaryEl.textContent += " · 结果可能已过期,建议重新测试"; } function hookIdeStaleAndGuard() { // 代码变更 → 过期标注(CM6 是 contenteditable,input/keydown 均会冒泡) const stale = (e) => { if (e.target && e.target.closest && e.target.closest(SELECTORS.cmContent)) markIdeStale(); }; document.addEventListener("input", stale, true); document.addEventListener("keydown", stale, true); // 批测中拦掉用户手点原生 运行/自测(程序化点击带 driving 标记放行) document.addEventListener( "click", (e) => { if (!IDE_BATCH.running || IDE_BATCH.driving) return; const t = e.target && e.target.closest && e.target.closest( `${SELECTORS.ideSampleBlock} a, ${SELECTORS.ideToolbar} a.run`, ); if (t) { e.preventDefault(); e.stopPropagation(); } }, true, ); }
function mountIdeButton() { const tb = ideToolbarByTitle("代码"); if (!tb) return; const actions = tb.querySelector(SELECTORS.ideToolbarActions); if (!actions || actions.querySelector(".luogusp-ide-batch-btn")) return; const selfTest = [...actions.querySelectorAll("button")].find( (b) => (b.textContent || "").trim() === "自测", ); if (!selfTest) return; // 克隆原生「自测」按钮继承洛谷样式(含 data-v 作用域),只换文字 const btn = selfTest.cloneNode(true); btn.textContent = "一键测试"; btn.classList.add("luogusp-ide-batch-btn"); btn.disabled = false; btn.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); startIdeBatch(); }); actions.insertBefore(btn, selfTest); }
function mountIdeTabs() { if (IDE_BATCH.tabBar && document.contains(IDE_BATCH.tabBar)) return; const inputTb = ideToolbarByTitle("输入"); if (!inputTb) return; const ioLayout = inputTb.closest(".panel-layout"); // 底部 输入|输出 水平分栏 const host = ioLayout && ioLayout.parentElement; if (!host) return; host .querySelectorAll(".luogusp-ide-tabbar, .luogusp-ide-panel") .forEach((e) => e.remove()); const tabBar = document.createElement("div"); tabBar.className = "luogusp-ide-tabbar"; tabBar.innerHTML = '<span class="luogusp-ide-tab" data-tab="custom">自定义测试</span>' + '<span class="luogusp-ide-tab" data-tab="samples">样例测试</span>'; tabBar.addEventListener("click", (e) => { const t = e.target.closest("[data-tab]"); if (t) switchIdeTab(t.dataset.tab); }); const panel = document.createElement("div"); panel.className = "luogusp-ide-panel"; panel.innerHTML = '<div class="luogusp-ide-head">' + '<span class="luogusp-ide-title">样例测试</span>' + '<span class="luogusp-ide-summary">尚未运行</span>' + '<span class="luogusp-ide-headbtns"></span>' + "</div>" + '<div class="luogusp-ide-rows"></div>' + '<div class="luogusp-ide-legend"></div>'; // 停止/重新测试:同样克隆原生「自测」继承样式 const tpl = [ ...document.querySelectorAll(`${SELECTORS.ideToolbar} button`), ].find( (b) => (b.textContent || "").trim() === "自测" || b.classList.contains("luogusp-ide-batch-btn"), ); const headBtns = panel.querySelector(".luogusp-ide-headbtns"); const mkBtn = (text, cls, onClick) => { const b = tpl ? tpl.cloneNode(true) : document.createElement("button"); b.textContent = text; b.className = (tpl ? tpl.className : "") + " " + cls; b.classList.remove("luogusp-ide-batch-btn"); b.disabled = false; b.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); onClick(); }); headBtns.appendChild(b); return b; }; IDE_BATCH.stopBtn = mkBtn("停止", "luogusp-ide-stop", () => { IDE_BATCH.stopReq = true; }); IDE_BATCH.stopBtn.style.display = "none"; mkBtn("重新测试", "luogusp-ide-rerun", () => startIdeBatch()); host.insertBefore(tabBar, ioLayout); host.appendChild(panel); IDE_BATCH.tabBar = tabBar; IDE_BATCH.panel = panel; IDE_BATCH.ioLayout = ioLayout; IDE_BATCH.rowsEl = panel.querySelector(".luogusp-ide-rows"); IDE_BATCH.summaryEl = panel.querySelector(".luogusp-ide-summary"); panel.querySelector(".luogusp-ide-legend").innerHTML = "图例:" + IDE_LEGEND.map( ([t, bg, bd, fg]) => `<span class="luogusp-ide-pill" style="background-color:${bg};border-color:${bd};color:${fg};">${t}</span>`, ).join("") + '<span style="margin-left:4px;">行内颜色实时取自洛谷原生结果</span>'; syncIdeTabVisibility(); } function switchIdeTab(tab) { IDE_BATCH.activeTab = tab; syncIdeTabVisibility(); } function syncIdeTabVisibility() { const { tabBar, panel, ioLayout } = IDE_BATCH; if (!tabBar || !document.contains(tabBar) || !panel || !ioLayout) return; const samples = IDE_BATCH.activeTab === "samples"; ioLayout.style.display = samples ? "none" : ""; panel.style.display = samples ? "" : "none"; tabBar.querySelectorAll(".luogusp-ide-tab").forEach((t) => { t.classList.toggle("on", (t.dataset.tab === "samples") === samples); }); }
const IDE_PILL_WAIT = "background-color:#bfbfbf;border-color:#b3b3b3;color:#fff;"; const IDE_PILL_RUN = "background-color:#3498db;border-color:#2f89c5;color:#fff;"; function renderIdeRows(samples) { const rowsEl = IDE_BATCH.rowsEl; if (!rowsEl) return; rowsEl.innerHTML = samples .map( (s, i) => ` <div class="luogusp-ide-row" data-idx="${i}"> <div class="luogusp-ide-rowhead"> <span class="luogusp-ide-chev">▶</span>样例 #${i + 1} <span class="luogusp-ide-meta"></span> <span class="luogusp-ide-pill" style="${IDE_PILL_WAIT}">等待</span> </div> <div class="luogusp-ide-detail"></div> </div>`, ) .join(""); rowsEl.querySelectorAll(".luogusp-ide-rowhead").forEach((h) => { h.addEventListener("click", () => { const row = h.parentElement; const was = row.classList.contains("open"); rowsEl .querySelectorAll(".luogusp-ide-row.open") .forEach((r) => r.classList.remove("open")); if (!was) row.classList.add("open"); }); }); } function ideRowParts(i) { const row = IDE_BATCH.rowsEl && IDE_BATCH.rowsEl.querySelector(`.luogusp-ide-row[data-idx="${i}"]`); if (!row) return null; return { row, pill: row.querySelector(".luogusp-ide-pill"), meta: row.querySelector(".luogusp-ide-meta"), detail: row.querySelector(".luogusp-ide-detail"), }; } function expandIdeRow(i) { if (!IDE_BATCH.rowsEl) return; IDE_BATCH.rowsEl .querySelectorAll(".luogusp-ide-row.open") .forEach((r) => r.classList.remove("open")); const p = ideRowParts(i); if (p) p.row.classList.add("open"); }
function ensureIdeBatchUI() { if (!ideModeActive()) { unmountIdeBatchUI(); return; } mountIdeButton(); mountIdeTabs(); syncIdeTabVisibility(); }
function unmountIdeBatchUI() { if (IDE_BATCH.running) IDE_BATCH.stopReq = true; // 退出 IDE/换题:请求停止 IDE_BATCH.activeTab = "custom"; // 复位,防再次进入时默认落在空面板 IDE_BATCH.tabBar = IDE_BATCH.panel = IDE_BATCH.ioLayout = null; IDE_BATCH.rowsEl = IDE_BATCH.summaryEl = IDE_BATCH.stopBtn = null; }
// SPA:进出 IDE 模式/换题时补挂与清理(rAF 节流,同既有 watchSettingButton 模式) function watchIdeBatch() { installIdeSubmitObserver(); hookIdeStaleAndGuard(); let scheduled = false; const tick = () => { scheduled = false; try { ensureIdeBatchUI(); } catch (e) { console.error("LuoguSP ide batch:", e); } }; const queue = () => { if (!scheduled) { scheduled = true; requestAnimationFrame(tick); } }; new MutationObserver(queue).observe(document.body, { childList: true, subtree: true, }); watchUrlChange(queue); ensureIdeBatchUI(); }
// ============================================================ // 启动 // ============================================================ const isChatPage = location.href.startsWith("https://www.luogu.com.cn/chat");
const FEATURES = [ { always: true, run: watchSettingButton }, { key: `${STORAGE_PREFIX}addProblemsColor`, run: () => setTimeout(addProblemsColor, 500), }, { key: `${STORAGE_PREFIX}addMessageLink`, run: () => { if (isChatPage) setTimeout(addMessageLink, 500); }, }, { key: `${STORAGE_PREFIX}showIntro`, run: watchHiddenIntro }, { key: `${STORAGE_PREFIX}ideBatchSampleTest`, run: watchIdeBatch }, ];
injectStyle(); for (const f of FEATURES) { if (!f.always && !storage.get(f.key)) continue; try { f.run(); } catch (e) { console.error("LuoguSP feature failed:", e); } }})();(()=>{var i=globalThis.__LUOGUSP_CDN_RUNTIME__;(!i||i.apiVersion!==1)&&console.error("LuoguSP CDN runtime was not initialized. Please update or reinstall LuoguSP.");})();```