版本比较
洛谷简易插件·LuoguSP
正文
增加 191 行删除 92 行
18 行未改动192021222323242526271,769 行未改动全部展开179717981799180018011801180218031804180518061807180818097 行未改动18171818181918201821182218231824182518261827182818291830183118321833225 行未改动全部展开205920602061206220502051205220532063206420652066206720682069207020712072207359 行未改动21332134213521362121212221232124213721382139214021412142214321442145212721282129214621472148214921502151215221532154213221552156215721586 行未改动2165216621672168214721692170217121722152215321542155215621572173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215216921702216221722182219222022212222217321742175217621772178217921802181218221832184218521862187218821892190219121922193222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122728 行未改动22812282228322842210221122122213221422152216221722182219222022212222228522862287228822892290229122922293229422952296229722982299230023012302222422252303230423052306230723082227222822292230223122322233223422352236223723092310231122392240231223132314231523162243224422452246224722482249225022512317231823192254225522562257232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235980 行未改动2440244124422443244424452349235023512352235323542355244624472448244924502451245224532454245524562457245818 行未改动```javascript// ==UserScript==// @name LuoguSP// @namespace https://github.com/ShanireZ/LuoguSP// @version 2.10.9// @version 2.11.5// @description LuoguSP:题目难度着色 / 私信 Ctrl+Click(用户名+头像) 跳转主页 / 显示隐藏的个人简介 / IDE 一键测试样例 / 受限文章与剪贴板直接显示// @author ShanireZ, realskc (Until 1.8.2)// @license GPL-3.0// @match https://www.luogu.com.cn/*1,769 行未改动全部展开 ".luogusp-rst-abtn .text{text-align:center;font-size:.75em;}" + ".luogusp-rst-abtn>*{color:#3498db !important;}" + ".luogusp-rst-pactions{display:flex;align-items:center;}" + ".luogusp-rst-pbtn{font-size:.875em;line-height:1.5;padding:.3125em 1em;margin-left:.5em;color:#fff;background:#3498db;border:1px solid #3498db;border-radius:3px;cursor:pointer;}" + ".luogusp-rst-pbtn:hover{background:rgba(52,152,219,.9);}"; ".luogusp-rst-pbtn:hover{background:rgba(52,152,219,.9);}" + ".luogusp-rst-off{opacity:.55;cursor:not-allowed;pointer-events:none;}" + // 剪贴板页「更新时间」与同行左侧「发表时间」的水平间隔(author 行内横排; // ★勿用 margin-top——会把本项在行内往下推出错位。div 选择器只命中剪贴板项,文章页是内联 span 不受影响) "div.luogusp-rst-updtime{margin-left:1em;}"; // 扩展按钮图标(FontAwesome Free 6.7.2 solid 原版 path:arrows-rotate / arrow-up-right-from-square) const RST_BTN_ICONS = { refresh: { vb: "0 0 512 512",7 行未改动
function rstAvatar(uid) { return `https://cdn.luogu.com.cn/upload/usericon/${uid}.png`; } // 保存站 ISO 时间 → 本地 "YYYY-MM-DD HH:mm[:ss]"(对齐洛谷原生 <time> 显示格式) function rstFmtTime(iso, withSec) { const ms = Date.parse(iso || ""); if (!ms) return null; const d = new Date(ms); const p = (n) => String(n).padStart(2, "0"); const base = `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`; return withSec ? `${base}:${p(d.getSeconds())}` : base; } // 作者等用户数据一律走国内站同源接口(owner 要求:不吃保存站/国际站的用户数据;头像也全走 .cn CDN)。 // 接口=/api/user/search?keyword={uid}(拦截页源实测可用;旧 /user/{uid}?_contentOnly=1 已死,返回 HTML), // 返回 userSummary:{uid,name,avatar,slogan,badge,color,ccfLevel,xcpcLevel,…}。失败回退存档快照。 const rstUserCache = new Map();225 行未改动全部展开 `</head><body><div id="app"></div></body></html>`; document.open(); document.write(html); document.close(); rstMountArticleButtons(info); // document.write 抹掉了启动期挂在旧 body 上的设置入口观察器 → 重挂(格式随导航自适应) injectStyle(); watchSettingButton(); rstOnNewDocReady(() => { rstMountArticleButtons(info, data); // document.write 把旧文档上的监听/观察器全部抹掉 → 全量功能重挂 // (设置入口/难度着色/简介/IDE 批测等;受限接管自身重跑会因拦截页锚点不匹配安全空转) startFeatures(); rstArmReturnReload(); }); }
// 剪贴板页:合成 window._feInjection(currentTemplate PasteShow)+ 官方 lfe 前端 async function rstBootPaste(info, data) {59 行未改动 `</head><body><div id="app"><noscript><h3>请<b style="color:#f00;">不要禁用</b>脚本,否则网页无法正常加载</h3></noscript></div></body></html>`; document.open(); document.write(html); document.close(); rstMountPasteButtons(info); // document.write 抹掉了启动期挂在旧 body 上的设置入口观察器 → 重挂(格式随导航自适应) injectStyle(); watchSettingButton(); rstOnNewDocReady(() => { rstMountPasteButtons(info, data); // document.write 把旧文档上的监听/观察器全部抹掉 → 全量功能重挂 // (设置入口/难度着色/简介/IDE 批测等;受限接管自身重跑会因拦截页锚点不匹配安全空转) startFeatures(); rstArmReturnReload(); }); }
// 扩展按钮(文章页):等官方前端渲染出互动条再注入;Vue 重渲染会抹节点,观察器负责补种 function rstMountArticleButtons(info) { const make = (icon, extraCls, text, title, onClick) => { // 扩展按钮(文章页):等官方前端渲染出互动条再注入;Vue 重渲染会抹节点,观察器负责补种。 // 同时在正文底部「创建时间:…」后方补「更新时间」=保存站存档最近更新时间(updatedAt), // 供 owner 判断是否需要点「申请更新」。 function rstMountArticleButtons(info, data) { const updText = rstFmtTime(data && data.updatedAt, true); // owner 要求:扩展按钮不带 title 悬浮说明(与时间栏一致,页面不出浏览器浮泡) const make = (icon, extraCls, text, onClick) => { const div = document.createElement("div"); div.className = `button-2line luogusp-rst-abtn ${extraCls}`; div.title = title; div.innerHTML = `<svg class="svg-inline--fa icon" style="font-size:1.25em" viewBox="${icon.vb}" aria-hidden="true"><path fill="currentColor" d="${icon.d}"/></svg><span class="text">${text}</span>`; div.addEventListener("click", onClick); return div; };6 行未改动 make( RST_BTN_ICONS.refresh, "luogusp-rst-btn-refresh", "申请更新", "向保存站申请更新存档", () => rstManualRefresh(info), ), ); bar.appendChild( make( RST_BTN_ICONS.external, "", "国际站原文", "前往国际站查看原文(可点赞/收藏/评论)", () => window.open(info.origUrl, "_blank", "noopener"), make(RST_BTN_ICONS.external, "", "国际站原文", () => window.open(info.origUrl, "_blank", "noopener"), ), ); }); // owner 要求:指向创建/更新时间不出浏览器悬浮泡 → 整条时间栏剥 title // (removeAttribute 无属性时不产生变更记录,天然幂等) document .querySelectorAll(".article-content .update-info") .forEach((bar) => { bar.removeAttribute("title"); bar .querySelectorAll("[title]") .forEach((n) => n.removeAttribute("title")); }); if (updText) document .querySelectorAll(".article-content .update-info") .forEach((bar) => { if (bar.querySelector(".luogusp-rst-updtime")) return; const ref = [...bar.querySelectorAll("span")].find((s) => /创建时间/.test(s.textContent || ""), ); const span = document.createElement("span"); if (ref) for (const at of ref.attributes) if (at.name.startsWith("data-v-")) span.setAttribute(at.name, at.value); // 继承 data-v 作用域样式 span.classList.add("luogusp-rst-updtime"); span.textContent = `更新时间:${updText}`; const sep = document.createTextNode(" "); if (ref) ref.after(sep, span); else bar.append(sep, span); }); rstApplyRefreshBtns(); // Vue 重种出的「申请更新」按钮要重新套用当前状态 }; inject(); new MutationObserver(inject).observe( document.body || document.documentElement, { childList: true, subtree: true }, ); } // 扩展按钮(剪贴板页):内容卡首行(content-card-top)最右侧两枚实心蓝钮 // (首行是 flex space-between,作者信息在左,本容器落位最右) function rstMountPasteButtons(info) { // (首行是 flex space-between,作者信息在左,本容器落位最右)。 // 同时在「发表时间: …」行下方补「更新时间」行=保存站存档最近更新时间(updatedAt)。 function rstMountPasteButtons(info, data) { const updText = rstFmtTime(data && data.updatedAt, false); const inject = () => { const top = document.querySelector(".card .content-card-top"); if (!top) return; if (!top || top.querySelector(".luogusp-rst-pactions")) return; const mk = (extraCls, text, title, onClick) => { const b = document.createElement("button"); b.type = "button"; b.className = `luogusp-rst-pbtn ${extraCls}`; b.textContent = text; b.title = title; b.addEventListener("click", onClick); return b; }; const box = document.createElement("div"); box.className = "luogusp-rst-pactions"; box.append( mk("luogusp-rst-btn-refresh", "申请更新", "向保存站申请更新存档", () => rstManualRefresh(info), ), mk("", "国际站原文", "前往国际站查看原文", () => window.open(info.origUrl, "_blank", "noopener"), ), ); top.appendChild(box); if (!top.querySelector(".luogusp-rst-pactions")) { // owner 要求:扩展按钮不带 title 悬浮说明(与时间栏一致,页面不出浏览器浮泡) const mk = (extraCls, text, onClick) => { const b = document.createElement("button"); b.type = "button"; b.className = `luogusp-rst-pbtn ${extraCls}`; b.textContent = text; b.addEventListener("click", onClick); return b; }; const box = document.createElement("div"); box.className = "luogusp-rst-pactions"; box.append( mk("luogusp-rst-btn-refresh", "申请更新", () => rstManualRefresh(info), ), mk("", "国际站原文", () => window.open(info.origUrl, "_blank", "noopener"), ), ); top.appendChild(box); } const author = top.querySelector(".author"); // owner 要求:指向发表/更新时间不出浏览器悬浮泡 → 发表时间行剥 title // (removeAttribute 无属性时不产生变更记录,天然幂等) const pubRow = author ? [...author.querySelectorAll("div.lfe-caption")].find((d) => /发表时间/.test(d.textContent || ""), ) : null; if (pubRow) { pubRow.removeAttribute("title"); pubRow .querySelectorAll("[title]") .forEach((n) => n.removeAttribute("title")); } if (updText && pubRow && !author.querySelector(".luogusp-rst-updtime")) { // 浅克隆保留 lfe-caption 类与 data-v 作用域属性(title 已在上方剥净) const row = pubRow.cloneNode(false); row.classList.add("luogusp-rst-updtime"); const span = document.createElement("span"); span.textContent = `更新时间: ${updText}`; row.appendChild(span); pubRow.after(row); } rstApplyRefreshBtns(); // Vue 重种出的「申请更新」按钮要重新套用当前状态 }; inject(); new MutationObserver(inject).observe( document.body || document.documentElement,8 行未改动 return saverPost(`/workflow/create/template/${info.type}-save-pipeline`, { targetId: info.id, }); } async function rstPollFresh(info, oldHash, times, gapMs) { for (let i = 0; i < times; i++) { await sleep(gapMs); let q; try { q = await rstQuery(info); } catch (e) { continue; // 网络抖动,下轮再试 } if (q && q.code === 200 && q.data && q.data.contentHash !== oldHash) return q.data; } return null; // 申请更新(手动):状态机 idle=可点 / busy=更新中… / done=已申请。 // owner 口径(2026-07-23):提交成功即锁定「已申请」且不可再点,不轮询不自动刷新, // 直至用户主动刷新页面(保存工作流异步完成,刷新后自然装配新档);仅提交失败允许重试。 // Vue 重渲染会抹掉按钮由观察器重种,故状态存模块级、每次补种后重新套用(rstApplyRefreshBtns)。 let rstRefreshState = "idle"; let rstRefreshText = "申请更新"; // ★本函数被 inject 观察器(body childList+subtree)的回调无条件调用,必须幂等: // textContent 同值重写也会删旧建新 Text 节点、产生 childList 变更记录, // 会把观察器自己再触发一遍 → 微任务死循环整页卡死(2.11.0 事故),故同值不写。 function rstApplyRefreshBtns() { const off = rstRefreshState !== "idle"; document.querySelectorAll(".luogusp-rst-btn-refresh").forEach((el) => { const t = el.querySelector(".text") || el; if (t.textContent !== rstRefreshText) t.textContent = rstRefreshText; el.classList.toggle("luogusp-rst-off", off); if (el.tagName === "BUTTON" && el.disabled !== off) el.disabled = off; }); } // 申请更新:发保存工作流(文章同时刷评论存档)→ 轮询到新档 → 整页重载重走接管;无变化=提示已最新 let rstRefreshing = false; function rstSetRefresh(state, text) { rstRefreshState = state; rstRefreshText = text; rstApplyRefreshBtns(); } async function rstManualRefresh(info) { if (rstRefreshing) return; rstRefreshing = true; const setText = (t) => { document .querySelectorAll(".luogusp-rst-btn-refresh .text") .forEach((el) => (el.textContent = t)); document .querySelectorAll("button.luogusp-rst-btn-refresh") .forEach((el) => (el.textContent = t)); }; setText("更新中…"); if (rstRefreshState !== "idle") return; rstSetRefresh("busy", "更新中…"); try { const cur = await rstQuery(info); await rstTriggerSave(info); const r = await rstTriggerSave(info); if (r && typeof r.code === "number" && r.code >= 500) throw new Error(`保存站返回 ${r.code}`); if (info.type === "article") saverPost(`/article/comments/${info.id}/refresh`).catch(() => {}); const fresh = await rstPollFresh( info, cur && cur.data ? cur.data.contentHash : "", 10, 3000, ); if (fresh) return location.reload(); setText("已是最新"); setTimeout(() => setText("申请更新"), 3000); rstSetRefresh("done", "已申请"); } catch (e) { console.error("LuoguSP restricted refresh:", e); setText("更新失败"); setTimeout(() => setText("申请更新"), 3000); } finally { rstRefreshing = false; rstSetRefresh("idle", "更新失败"); setTimeout(() => { if (rstRefreshState === "idle") rstSetRefresh("idle", "申请更新"); }, 3000); } }
// 接管文档是官方 SPA:站内路由(pushState/popstate,含浏览器前进后退)驶向任一 // article/paste 路由时,官方前端会去真实接口取数——受限内容必失败,且没有页面加载、 // 脚本不会重跑(本次接管文档的监听已随 document.write 重挂,history 包装器天然存活)。 // 侦测到 pathname 切到 article/paste 即整页刷新:受限 → 服务器重出拦截页 → 脚本重新接管; // 公开 → 原生页面正常整页加载,无副作用。 function rstArmReturnReload() { let lastPath = location.pathname; watchUrlChange(() => { const p = location.pathname; if (p === lastPath) return; // 同路径 hash/query 变化不刷 lastPath = p; if (/^\/(article|paste)\/[A-Za-z0-9]+\/?$/.test(p)) location.reload(); }); }
// ★document.write 的新文档解析是异步收尾的:close() 返回时 body 可能尚未建出, // 直接 observe(document.body) 会抛 TypeError(真机实测 2026-07-23)→ 等 body 就绪再挂。 function rstOnNewDocReady(fn) { const run = () => { try { fn(); } catch (e) { console.error("LuoguSP restricted post-boot:", e); } }; if (document.body) return run(); document.addEventListener("DOMContentLoaded", run, { once: true }); }
async function rstBootPage(info, data) { try { if (info.type === "article") await rstBootArticle(info, data); else await rstBootPaste(info, data);80 行未改动 { key: `${STORAGE_PREFIX}ideBatchSampleTest`, run: watchIdeBatch }, { key: `${STORAGE_PREFIX}showRestrictedContent`, run: watchRestrictedPage }, ];
// 受限内容接管 document.write 重建文档后也会调用本函数,重挂全部功能 function startFeatures() { 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); 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); } } } startFeatures();})();```