版本比较
洛谷美化样式
正文
增加 189 行删除 155 行
35 行未改动3637383940414041424443444546472 行未改动50515253555657585960616263646566676869707172737475767778798081545556858687888990575859949596606162636465991001011021031041051061071081091101111121136667686970717212173747576777879128808182831321331341351361378485868788143899091929394959697981531541551561579910015916016110110210310425 行未改动13013113213319519613413513613713 行未改动15115215315415515615715815916022016116222322416316416516616716816917022822923017117217323217417517617717817918018118218318418518618718818919019116 行未改动2082092102112572582592602612622632642652662122132142152712162172182742192202212222232792242252262272282842852862872882892902912292302312322 行未改动23523623723830223924024124230730830931031131231331431531631731831932032132232332432532632732824324424524615 行未改动2622632642653522662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743553753763773583783793803813823833843663673853863873883893723903913923763933943953965 行未改动40240340440539039140640740840941041141241339940041441541641741841940742042142242342442542642742842941741843043143243333 行未改动46746846947046047147246247347447547647746746847847948048147348248348448548624 行未改动5115125135145065075155165175185196 行未改动5265275285295305315325335345352 行未改动53853954054153253354254354454554654719 行未改动56756856957056157157257357457511 行未改动5875885895905815915925935945956 行未改动6026036046055966066076086096104 行未改动61561661761860961962062162262362462562661862762862963028 行未改动65966066166265566366466566666766866166967067167267354 行未改动7287297307317247327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697702 行未改动7737747757767777787797807817826 行未改动35 行未改动
把下面的代码复制进去。
:::info[code]```// ==UserScript==```// ==UserScript==// @name 洛谷主题 亮/暗 切换 + 站内设置// @namespace local.lotletos.luogu-theme-toggle// @version 1.0.1791347014// @version 1.0.1794666590// @description 洛谷主题的亮/暗切换按钮 + 站内设置面板(背景图、卡片/侧栏不透明度、过渡时长)// @author lotletos// @match https://*.luogu.com.cn/*// @run-at document-start2 行未改动// @updateURL http://127.0.0.1:8791/luogu-theme-toggle.user.js// @downloadURL http://127.0.0.1:8791/luogu-theme-toggle.user.js// ==/UserScript==
/* * 和 Stylus 样式的关系 * -------------------- * 样式文件 luogu-dark.user.css 里:暗色是默认,亮色由 <html data-lg-theme="light"> 触发。 * 本脚本负责三件事: * ① 尽早写 data-lg-theme(document-start,避免刷新时先闪一下暗色) * ② 顶栏插「暗 / 亮」切换按钮 * ③ 顶栏插「⚙」设置面板 —— 在站内就能调背景图和几个常用值 * * 面板里的改动怎么写进去: * 全部通过【行内变量】写到 <html> 上(root.style.setProperty)。 * 行内声明优先于任何作者样式表,所以能稳稳压过 Stylus 注入的 @var :root 块, * 也不会被主题里那些 !important(!important 只影响"声明之间"的优先级, * 行内普通声明本来就高于一切作者声明)。 * * ⚠ 为什么必须【按模式分别存、切模式时重写一遍】: * 行内变量挂在 <html> 上,而 <html> 是两个模式共用的。 * 如果只写一份,切到另一个模式它照样生效,就会把另一套配色也污染掉。 * 所以:切模式时先把本脚本写过的行内变量全部清掉,再写入"该模式"的那一套。 * * 为什么不用别的方案(都推演过): * · Stylus 的 @var 做不到切换:它注入的只是 `:root { --名字: 值 }`, * 而 CSS 没有"按自定义属性的值分支"的能力,正文没法据此换整套调色板。 * · 整页 filter: invert() 不行:filter 会给 position:fixed 重建包含块, * 主题里顶栏/左侧栏/右侧胶囊全是 fixed,会跟着页面滚动跑掉。 */
(function () { 'use strict';
// ── 防重复执行 ── // 篡改猴按「@namespace + @name」认脚本。如果改过 @name(本脚本就改过, // 从「洛谷主题 亮/暗 切换」改成加了「+ 站内设置」),篡改猴会以为是另一个脚本、 // 装成【第二份】,旧的那份还在跑 —— 结果顶栏出现两个亮/暗按钮。 // 这个全局标记能挡住"同一份脚本被注入两次",但挡不住"两份不同脚本共存", // 所以下面还有一段重复检测 + 提示用户去删。 if (window.__lgThemeScriptLoaded) return; window.__lgThemeScriptLoaded = true;
const KEY_THEME = 'lg-theme'; // 亮/暗 const KEY_PREFS = 'lg-prefs'; // 面板里调的那些值 const ATTR = 'data-lg-theme'; const KEY_THEME = 'lg-theme'; const KEY_PREFS = 'lg-prefs'; const ATTR = 'data-lg-theme'; // 本样式用的历史属性 const SITE_ATTR = 'data-lcolor-theme'; // 站点原生主题(2026-11 上线) const root = document.documentElement;
// ── 偏好存哪儿:优先用篡改猴的【脚本存储】,localStorage 只当退路 ── // 为什么不能只用 localStorage:洛谷主站和它的子站是【不同源】, // www / ti / help / class 各有一份互不相通的 localStorage。 // 实测:在 www 上设好背景图,点侧栏的「洛谷有题」进 ti.luogu.com.cn, // localStorage.getItem('lg-prefs') 是 null,data-lg-theme / data-lg-cardfx // 也全是空 —— 亮暗、背景图、卡片不透明度、卡片特效统统退回默认。 // 而 Stylus 那份 usercss 用的是 domain("luogu.com.cn"),子站照样生效, // 于是就变成"样式在、设置不在"。 // GM_getValue / GM_setValue 的存储是【按脚本】隔离、而不是按域名隔离的, // 同一份脚本 @match 到的所有站点共用一份,正好补上这个缺口。 // ⚠ 所以文件头必须写 @grant GM_getValue / GM_setValue。 // 写了 @grant 篡改猴会把脚本放进沙箱执行,本脚本只用到 document、 // 定时器、MutationObserver、localStorage,沙箱里都有,不受影响。 // 拿不到 GM_* 时(其他管理器、或被人改回 @grant none)自动退回 localStorage, // 行为等同于加这段之前 —— 只是设置仍按域名各存一份。 const HAS_GM = typeof GM_getValue === 'function' && typeof GM_setValue === 'function';
function storeGet(key) { if (HAS_GM) { try { const v = GM_getValue(key, null); if (v !== null && v !== undefined) return v; } catch (e) { /* 存储被禁用时继续往下走 */ } } catch (e) { } } try { return localStorage.getItem(key); } catch (e) { return null; } }
function storeSet(key, value) { if (HAS_GM) { try { GM_setValue(key, value); } catch (e) {} } // 两边都写一份:读到 GM 那份就优先用 GM 的,读不到时才用这份。
try { localStorage.setItem(key, value); } catch (e) {} }
// 迁移:这个版本之前,设置只存在 localStorage 里(也就是只有 www 那一份)。 // 如果不管它,用户升级完必须【随便动一下设置】才会把值推到 GM 存储里, // 在那之前点进 ti./help. 仍然是默认值 —— 看起来就像"没修好"。 // 所以启动时补一次:GM 那份是空的、而本域名有一份旧的,就推上去。 // ⚠ 只在 GM 为空时推,绝不反向覆盖 —— 否则在某个子站上开一次页面 // 就会用那里的默认值把主站的设置冲掉。 function migrateToGM() { if (!HAS_GM) return; for (const k of [KEY_THEME, KEY_PREFS]) { let gm = null; try { gm = GM_getValue(k, null); } catch (e) { continue; } if (gm !== null && gm !== undefined && gm !== '') continue; // GM 已经有值,不动 if (gm !== null && gm !== undefined && gm !== '') continue; let ls = null; try { ls = localStorage.getItem(k); } catch (e) {} if (ls !== null && ls !== undefined && ls !== '') { try { GM_setValue(k, ls); } catch (e) {} } } } migrateToGM();
// 面板底部显示的版本号 —— 用来一眼确认"更新到底装上没有"。 // ⚠ 这里必须手动跟着改(服务端会把文件里的 @version 改写成 1.0.<mtime> // 好让篡改猴能识别更新,所以 @version 是给机器看的、这里这个才是给人看的)。 // 每次改脚本记得一起 +1。 const VERSION = '1.4.2'; const VERSION = '1.6.0';
// 检测页面上是否已经存在同名按钮 —— 只有"两份脚本共存"时才会出现。 // 这里不做自动清理(旧脚本自己的 MutationObserver 会把它再插回来,会打架), // 只明确告诉用户去篡改猴里删掉旧的那份。 function detectDuplicates() { const n = document.querySelectorAll('[id="lg-theme-toggle"]').length; if (n > 1 && !sessionStorage.getItem('lg-dup-warned')) { try { sessionStorage.setItem('lg-dup-warned', '1'); } catch (e) {}25 行未改动 document.body.appendChild(d); setTimeout(() => d.remove(), 20000); }
// 面板能调的东西。每一行 = 一个 CSS 变量 + 一套控件描述。 // perTheme=true 表示"按模式分别存"(暗色 / 亮色 各一份)。 const FIELDS = [ { key: 'bg', css: '--lg-bg-image', kind: 'text', perTheme: true, label: '页面背景图', hint: '只填网址就行(自动补 url("https://…"));填 none 去掉背景图' }, { key: 'card', css: '--lg-card-glass', kind: 'range', perTheme: true, color: true,13 行未改动 { v: 'glass', label: '液态玻璃(高光 + 厚度)' }, ], hint: '液态玻璃会叠四层高光做出厚度:左上镜面光斑 + 斜向渐亮 + 上下缘亮线 + 内发光,' + '悬停时卡片抬起 2px。做法参考 BigBallOfMud-Luogu。' }, { key: 'sidebar', kind: 'check', perTheme: false, label: '默认收起左侧栏', hint: '打开后每次进页面,左侧栏默认是收起的(只在左边留一个小圆按钮)。' + '页面里那个折叠按钮只影响当前页面,不会被记住 —— 刷新或换页后仍按这里的默认值。' }, ];
// 本脚本写过的行内变量名,切模式时要逐个清掉 const MANAGED = FIELDS.filter(f => f.css).map(f => f.css);
// ---------------------------------------------------------------- 状态
/* 主题有两个消费者,必须同时驱动: ① 站点原生主题 —— html[data-lcolor-theme](导航、卡片、弹层、滚动条、color-scheme 都看它) ② 本样式 —— html[data-lg-theme](历史属性,保留以兼容旧版样式 / 其他插件) 少写一个就会出现「按钮点了,样式变了但站点弹层还是白的」这种半截状态。 */ let theme = 'dark'; let savedTheme = null; try { const saved = storeGet(KEY_THEME); if (saved === 'light' || saved === 'dark') theme = saved; } catch (e) { /* 隐私模式下存储可能不可用 */ } root.setAttribute(ATTR, theme); if (saved === 'light' || saved === 'dark') { theme = saved; savedTheme = saved; } } catch (e) { }
// prefs 结构:{ light: {bg:…, card:…}, dark: {…}, trans: … } /* painting 用来区分「我们写的」和「站点写的」, 否则下面那个 observer 会把自己的写入当成站点改动再处理一遍。 */ let painting = false; function paintTheme(t) { painting = true; root.setAttribute(ATTR, t); root.setAttribute(SITE_ATTR, t); root.style.colorScheme = t; if (document.body) document.body.style.colorScheme = t; setTimeout(function () { painting = false; }, 0); } // 用户明确选过:一进页面就断言,不等站点那套 跟随系统 的判定 if (savedTheme) paintTheme(savedTheme);
let prefs = {}; try { prefs = JSON.parse(storeGet(KEY_PREFS) || '{}') || {}; } catch (e) { prefs = {}; }
function savePrefs() {16 行未改动 } savePrefs(); }
// ---------------------------------------------------------------- 背景图兜底 // // Stylus 的 @var 值是被【原样】塞进 background-image 的, // 而用户在设置面板里很容易敲出这两种有问题的写法: // ① 完全裸的: cdn.luogu.com.cn/a.webp → 声明非法,整条被丢弃 → 纯色 // ② 有 url() 但缺协议: url("cdn.luogu.com.cn/a.webp") → 声明合法,但相对地址会拼到 // 当前页面域名后面(→ www.luogu.com.cn/…/cdn.luogu.com.cn/…), // 洛谷对未知路径返回 200 + text/html,浏览器解不出图 → 还是纯色,且看 CSS 完全看不出问题 // 所以这里统一补成 url("https://…")。填面板的值也走同一个函数。
function normalizeBg(v) { if (!v) return null; v = String(v).trim(); if (!v) return null; if (/^(none|inherit|initial|unset|revert)$/i.test(v)) return v; // 合法,原样用 if (/^(none|inherit|initial|unset|revert)$/i.test(v)) return v; if (/^(repeating-)?(linear|radial|conic)-gradient\(/i.test(v)) return v; if (/^(image-set|cross-fade|element|paint)\(/i.test(v)) return v; if (/^var\(/i.test(v)) return null; // 只是个别名,不去动它 if (/^var\(/i.test(v)) return null;
const m = /^url\(\s*(['"]?)([\s\S]*?)\1\s*\)$/i.exec(v); const inner = (m ? m[2] : v).trim(); if (!inner) return null; // 已经是绝对地址 / 内联数据 / 根路径 / 锚点 → 不动
if (/^(data:|blob:|[a-z][a-z0-9+.-]*:\/\/|\/\/|\/|#)/i.test(inner)) return m ? null : `url("${inner}")`; return 'url("https://' + inner.replace(/"/g, '') + '")'; }
// ---------------------------------------------------------------- 颜色 alpha 工具 // // ⚠ 这里踩过一个坑:--lg-card-glass / --lg-nav 这两个变量装的是【颜色】 // (比如 rgba(255,255,255,0.06)),不是数字。 // 滑块如果直接把 0.46 写进去,变量就成了 `0.46` —— 非法颜色, // 卡片底色会被整个丢掉(变成透明,比原来还糟),而且不报错。 // 所以这两个滑块必须"读出当前 RGB,只换 alpha"。
function parseColor(v) { const m = /rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)/i.exec(v || ''); if (!m) return null; return { r: +m[1], g: +m[2], b: +m[3], a: m[4] === undefined ? 1 : +m[4] };2 行未改动 function withAlpha(rgb, a) { return `rgba(${Math.round(rgb.r)}, ${Math.round(rgb.g)}, ${Math.round(rgb.b)}, ${a})`; }
// 取某个变量"当前实际生效"的 RGB,作为滑块改 alpha 的基准 function currentRgb(cssVar) { return parseColor(getComputedStyle(root).getPropertyValue(cssVar)) || { r: 128, g: 128, b: 128, a: 1 }; }
// ---------------------------------------------------------------- 关模糊 // // 实现:给 <html> 挂 data-lg-noblur,配一条 <style> 把 backdrop-filter 打掉。 // // ⚠ 必须用 @layer,不能靠堆特异性: // 主题里给卡片加模糊的是 // .l-card.l-card:not(:has(.ide-container)) { backdrop-filter: … !important } // 特异性 (0,3,0) 且带 !important;hover 那条 `#app .l-card.l-card:hover` // 更是 (1,2,0)。想用选择器压过它们,得往选择器里塞 id、重复属性名, // 又脏又容易漏(实测第一版就没压住卡片,只把 blur 变成了 blur(0px))。 // // CSS 层叠规则里有一条反直觉但正好救命的约定: // · 普通声明: 未分层的 赢过 分层的 // · !important 声明:分层的 反而 赢过 未分层的 // 所以在 @layer 里写 !important,可以【无视特异性】压过整张样式表里 // 所有未分层的 !important。这才是干这事的正确工具。 // // 为什么不用 `*` 通吃:那会让引擎给【每个元素】都算一遍 backdrop-filter, // 而本来只有几十个元素有这属性 —— 这开关就是为省性能,自己不能先费掉。 // // 另外 apply() 里还会把 --lg-card-blur / --lg-ide-blur 设成 0px: // 让变量驱动的那批从源头就不产生模糊,双保险。 const NOBLUR_CSS = `@layer lg-noblur { html[data-lg-noblur] :is( .l-card, .card, .lg-article, .top-bar, .navbar, .user-nav, .user-header-bottom,15 行未改动 s.textContent = NOBLUR_CSS; (document.head || document.documentElement).appendChild(s); }
// ---------------------------------------------------------------- 应用 // ---------------------------------------------------------------- 左侧栏折叠 // // 行为:侧栏里多一个「«」按钮,点一下把整条侧栏推出屏幕; // 屏幕上只在左侧留一个 30px 的「»」小圆按钮,点它还原。 // // ⚠ 状态【不落存储】:用户明确要求"伸缩只在当前页面生效"。 // 所以折叠标记只挂在 <html data-lg-sidebar> 上,刷新 / 换页就回到默认。 // 设置面板里的「默认收起左侧栏」才是持久化的,它决定每次进页面时的初始状态。 // // ⚠⚠ 动画只能用 left,【绝对不能用 transform,也不要用 opacity】。 // 右侧那两组胶囊(.nav-group.on-expand)是 position:fixed 钉在右边的, // 但 DOM 上它们是 nav.lside 的【子元素】(实测 x=1327,就在 nav 里面)。 // 于是: // · transform 会给 fixed 后代重建包含块 → 它们跟着侧栏一起跑到左边, // 动画结束 transform 消失才瞬移回右边(用户反馈的正是这个); // · opacity 是乘到整棵子树上的 → 淡出侧栏会把右侧胶囊一起淡掉。 // left 两个问题都没有:它不创建包含块,fixed 子元素仍以视口定位, // 而且 left 不会向下继承。代价是 left 走布局、每帧重排, // 但这里只有一个元素,实测 0.28s 很顺。 // // ⚠ 右侧那两组胶囊也要一起收(用户要的是"两侧一起缩")。 // 它们是 position:fixed; right:12px,所以用 right 让它们滑出右边即可 —— // 同样不创建包含块、不影响别的元素。宽度 96px,给 -110px 保证完全出屏。 // 两组是上下各一半的胶囊,位移量必须一致,否则接缝会错开。 const SIDEBAR_CSS = `@layer lg-sidebar { nav.lside, nav.sidebar { transition: left .28s ease !important; } html[data-lg-sidebar="collapsed"] nav.lside, html[data-lg-sidebar="collapsed"] nav.sidebar { left: -88px !important; } nav.lside .nav-group.on-expand, nav.sidebar .nav-group.on-expand { transition: right .28s ease, opacity .22s ease !important; } html[data-lg-sidebar="collapsed"] .nav-group.on-expand { right: -110px !important; opacity: 0 !important; } #lg-sidebar-restore { display: none; } html[data-lg-sidebar="collapsed"] #lg-sidebar-restore { display: flex !important; }}`;
function ensureSidebarStyle() { if (document.getElementById('lg-sidebar-style')) return; const s = document.createElement('style'); s.id = 'lg-sidebar-style'; s.textContent = SIDEBAR_CSS; (document.head || document.documentElement).appendChild(s); }
const ATTR_SIDEBAR = 'data-lg-sidebar'; let sidebarBtn = null, restoreBtn = null;
function setSidebarCollapsed(v) { if (v) root.setAttribute(ATTR_SIDEBAR, 'collapsed'); else root.removeAttribute(ATTR_SIDEBAR); }
function makeSidebarButton(id, text, title, fixed) { const b = document.createElement('a'); b.id = id; b.href = 'javascript:void 0'; b.title = title; b.textContent = text; b.setAttribute('role', 'button'); const base = [ 'align-items:center', 'justify-content:center', 'cursor:pointer', 'border-radius:999px', 'font-size:15px', 'line-height:1', 'color:var(--lg-text, #ddd)', 'text-decoration:none', 'user-select:none', 'opacity:.8', ]; if (fixed) { base.push( 'position:fixed', 'left:12px', 'top:50%', 'transform:translateY(-50%)', 'z-index:2147483000', 'width:30px', 'height:30px', 'margin:0', 'padding:0', 'background:var(--lg-nav, rgba(10,12,18,.82))', 'border:1px solid var(--lg-line, rgba(128,128,128,.35))', 'box-shadow:0 4px 14px rgba(0,0,0,.3)' ); } else { base.push('display:flex', 'width:100%', 'height:32px', 'margin:2px 0', 'padding:0'); } b.setAttribute('style', base.join(';')); b.addEventListener('mouseenter', function () { b.style.opacity = '1'; }); b.addEventListener('mouseleave', function () { b.style.opacity = '.8'; }); b.addEventListener('click', function (ev) { ev.preventDefault(); ev.stopPropagation(); setSidebarCollapsed(id === 'lg-sidebar-collapse'); }); return b; }
function injectSidebarToggle() { const nav = document.querySelector('nav.lside, nav.sidebar'); if (!nav) return false; ensureSidebarStyle();
if (!sidebarBtn) sidebarBtn = makeSidebarButton('lg-sidebar-collapse', '«', '收起左侧栏', false); if (!restoreBtn) restoreBtn = makeSidebarButton('lg-sidebar-restore', '»', '展开左侧栏', true);
if (!sidebarBtn.isConnected) nav.insertBefore(sidebarBtn, nav.firstChild); if (!restoreBtn.isConnected) (document.body || root).appendChild(restoreBtn); return true; }
function apply() { // ① 先清掉上一轮写过的(<html> 是两个模式共用的,不清会串)
MANAGED.forEach(k => root.style.removeProperty(k));
// ①b 关模糊开关:不在 FIELDS 的变量循环里,因为它改的是属性不是变量 if (prefs.noblur) { ensureNoblurStyle(); root.setAttribute('data-lg-noblur', ''); root.style.setProperty('--lg-card-blur', '0px'); root.style.setProperty('--lg-ide-blur', '0px'); } else { root.removeAttribute('data-lg-noblur'); // ⚠ 关闭时要把上面写过的行内值清掉,否则会残留 blur(0px), // 看不出区别但确实没恢复成用户在 Stylus 里设的模糊强度
root.style.removeProperty('--lg-card-blur'); root.style.removeProperty('--lg-ide-blur'); }
// ①c 卡片特效:同样改的是 <html> 上的属性,不进变量循环 if (prefs.cardfx) root.setAttribute('data-lg-cardfx', prefs.cardfx); else root.removeAttribute('data-lg-cardfx');
// ② 写入当前模式该有的值 for (const f of FIELDS) { if (!f.css) continue; let v = getPref(f); if (v === undefined || v === null || v === '') continue;5 行未改动 } else { const n = Number(v); if (!isFinite(n)) continue; if (f.color) { // 注意:本函数第 ① 步已经把行内值清掉了,所以这里读到的是 // 【该模式自身的】基准色(暗色偏蓝黑、亮色纯白),不会被上一轮覆盖污染
root.style.setProperty(f.css, withAlpha(currentRgb(f.css), n)); } else { root.style.setProperty(f.css, f.unit ? n + f.unit : String(n)); } } }
// ③ 兜底:Stylus 里那两个 @var 如果填了裸网址/缺协议,也顺手修掉 // (只在没被面板覆盖时才有意义,所以放在最后) fixStylusBgVars(); }
function fixStylusBgVars() { const cs = getComputedStyle(root); for (const k of ['--lg-bg-image', '--lg-bg-image-light']) { if (root.style.getPropertyValue(k)) continue; // 面板已经给过值了 if (root.style.getPropertyValue(k)) continue; const raw = (cs.getPropertyValue(k) || '').trim(); if (!raw || /^(url\(|none$)/i.test(raw)) continue; if (/^(repeating-)?(linear|radial|conic)-gradient\(/i.test(raw)) continue; if (/^var\(/i.test(raw)) continue; const norm = normalizeBg(raw); if (norm) root.style.setProperty(k, norm); } }
// ---------------------------------------------------------------- 顶栏按钮
let btn = null, gear = null, panel = null;
function pillButton(id, text, title) { const b = document.createElement('a');33 行未改动 }
function setTheme(next) { theme = next; root.setAttribute(ATTR, next); paintTheme(next); try { storeSet(KEY_THEME, next); } catch (e) {} apply(); // 先把该模式的值写上,再刷新 UI(控件要显示当前模式的值) apply(); syncButtons(); syncPanel(); }
// ---------------------------------------------------------------- 设置面板
function panelStyle() { return [ 'position:fixed', 'z-index:2147483646', 'top:64px', 'right:16px', 'width:320px', 'max-width:calc(100vw - 32px)', 'box-sizing:border-box', // 面板越加越长,小屏上会被视口截掉 —— 限高 + 自己滚
'max-height:calc(100vh - 84px)', 'overflow-y:auto', 'overscroll-behavior:contain', 'padding:14px 16px', 'border-radius:14px', 'background:var(--lg-solid, #16181c)', 'color:var(--lg-text, #e6e9ef)', 'border:1px solid rgba(128,128,128,.35)',24 行未改动 note.textContent = '下面这些按「当前模式」分别保存;切模式后会显示另一套值。'; box.appendChild(note);
for (const f of FIELDS) { // 勾选框单独一套结构:不进下面「左标签 + 右数值」的模板, // 否则会被 space-between 撑开(勾选框跑到最左、文字飞到最右)。
if (f.kind === 'check') { const wrap = document.createElement('label'); wrap.setAttribute('style', 'display:flex;align-items:center;gap:8px;margin-bottom:12px;cursor:pointer');6 行未改动 wrap.appendChild(box_); wrap.appendChild(txt); box_.addEventListener('change', function () { setPref(f, box_.checked); // 「默认收起左侧栏」改完立刻生效一次(之后刷新仍按这个默认值) if (f.key === 'sidebar') setSidebarCollapsed(box_.checked); apply(); syncPanel(); }); box.appendChild(wrap); if (f.hint) {2 行未改动 h.textContent = f.hint; box.appendChild(h); } f._input = box_; f._val = null; // 勾选框没有"右侧数值",别把标签元素当 _val, // 否则 syncPanel 里那句 _val.textContent='' 会把标签抹掉 f._val = null;
continue; }
const row = document.createElement('div');19 行未改动 (f.options || []).forEach(function (o) { const op = document.createElement('option'); op.value = o.v; op.textContent = o.label; // 下拉展开时用的是系统原生弹层,得单独给它上色,否则在暗色下是白底
op.setAttribute('style', 'background:var(--lg-solid, #16181c);color:var(--lg-text, #e6e9ef)'); input.appendChild(op); }); input.addEventListener('change', function () {11 行未改动 'font:12px/1.4 ui-monospace,monospace', ].join(';')); input.addEventListener('change', function () { const norm = normalizeBg(input.value); if (input.value.trim() && norm === null) return; // 无效输入不落库 if (input.value.trim() && norm === null) return; setPref(f, input.value.trim()); input.value = norm && norm !== 'none' ? input.value.trim() : input.value.trim(); apply(); syncPanel(); });6 行未改动 setPref(f, Number(input.value)); apply(); syncPanel(); }); } if (f.kind !== 'check') row.appendChild(input); // 勾选框已经插在标签那一行了 if (f.kind !== 'check') row.appendChild(input);
if (f.hint) { const h = document.createElement('div'); h.setAttribute('style', 'opacity:.6;font-size:11px;margin-top:4px');4 行未改动 f._input = input; f._val = val; box.appendChild(row); }
// 版本号:一眼确认"更新装上没有" const ver = document.createElement('div'); ver.setAttribute('style', 'margin-top:10px;padding-top:8px;border-top:1px solid rgba(128,128,128,.25);' + 'opacity:.55;font-size:11px;display:flex;justify-content:space-between'); ver.innerHTML = '<span>脚本 v' + VERSION + '</span>' + '<span>样式版本看 Stylus 里那份</span>'; box.appendChild(ver);
// 底部:恢复默认 / 关闭 const foot = document.createElement('div'); foot.setAttribute('style', 'display:flex;justify-content:space-between;margin-top:10px;padding-top:10px;border-top:1px solid rgba(128,128,128,.25)'); const reset = document.createElement('a'); reset.href = 'javascript:void 0';28 行未改动 } else if (f.kind === 'text') { f._input.value = (v === undefined || v === null) ? '' : String(v); f._val.textContent = (v === undefined || v === null || v === '') ? '(用 Stylus 的值)' : ''; } else { // 没设过就显示当前实际生效的值,让滑块不空着
let cur = v; if (cur === undefined || cur === null) { const cs = getComputedStyle(root).getPropertyValue(f.css).trim(); if (f.color) { const c = parseColor(cs); cur = c ? c.a : 1; // 颜色类:滑块代表 alpha cur = c ? c.a : 1; } else { cur = parseFloat(cs); } if (!isFinite(cur)) cur = f.min;54 行未改动
let timer = null; const mo = new MutationObserver(function () { if (timer) return; timer = setTimeout(function () { timer = null; tryInject(); }, 300); timer = setTimeout(function () { timer = null; tryInject(); injectSidebarToggle(); }, 300); });
/* 站点自己也会改 data-lcolor-theme(用户点了站点的 系统/浅色/暗色,或系统深浅切换)。 这种情况要【采纳】而不是抢回来 —— 否则用户在站点设置里永远切不动。 */ function watchSiteTheme() { new MutationObserver(function () { if (painting) return; const v = root.getAttribute(SITE_ATTR); if (!v || v === theme) return; if (v !== 'light' && v !== 'dark') return; theme = v; root.setAttribute(ATTR, v); if (document.body) document.body.style.colorScheme = v; apply(); syncButtons(); syncPanel(); }).observe(root, { attributes: true, attributeFilter: [SITE_ATTR] }); }
function boot() { if (savedTheme) paintTheme(savedTheme); watchSiteTheme(); apply(); // 左侧栏:按设置里的默认值决定进页面时的初始折叠状态(不写存储) setSidebarCollapsed(!!prefs.sidebar); injectSidebarToggle(); tryInject(); detectDuplicates(); mo.observe(document.documentElement, { childList: true, subtree: true }); let n = 0; const iv = setInterval(function () { apply(); // 侧栏是 Vue 渲染的,可能会被重新挂载 —— 每轮都补一次按钮 injectSidebarToggle(); if (tryInject() || ++n > 20) clearInterval(iv); }, 500); }
2 行未改动 } else { boot(); }})();
```:::
点击保存后回到洛谷界面,刷新,顶栏会出现这两个按钮。左边按钮是切换暗色和亮色的,右边是设置。6 行未改动