版本比较
洛谷美化样式
正文
增加 46 行删除 8 行
35 行未改动36373839404140414244434445464710 行未改动585960616362636465666727 行未改动95969798999910010110210355 行未改动159160161162163164165166167168169170166171172168173174175176177178179180181182183184185186187188189190191275 行未改动全部展开467468469470453471472473474475255 行未改动全部展开73173273373473573673773873974074174274374474574674774874975075175275375475575675775815 行未改动7747757767777787797807817826 行未改动35 行未改动
把下面的代码复制进去。
:::info[code]```// ==UserScript==```// ==UserScript==// @name 洛谷主题 亮/暗 切换 + 站内设置// @namespace local.lotletos.luogu-theme-toggle// @version 1.0.1792286247// @version 1.0.1794666590// @description 洛谷主题的亮/暗切换按钮 + 站内设置面板(背景图、卡片/侧栏不透明度、过渡时长)// @author lotletos// @match https://*.luogu.com.cn/*// @run-at document-start10 行未改动 window.__lgThemeScriptLoaded = true;
const KEY_THEME = 'lg-theme'; const KEY_PREFS = 'lg-prefs'; const ATTR = 'data-lg-theme'; const ATTR = 'data-lg-theme'; // 本样式用的历史属性 const SITE_ATTR = 'data-lcolor-theme'; // 站点原生主题(2026-11 上线) const root = document.documentElement;
const HAS_GM = typeof GM_getValue === 'function' && typeof GM_setValue === 'function';
27 行未改动 } } migrateToGM();
const VERSION = '1.5.1'; const VERSION = '1.6.0';
function detectDuplicates() { const n = document.querySelectorAll('[id="lg-theme-toggle"]').length; if (n > 1 && !sessionStorage.getItem('lg-dup-warned')) {55 行未改动 ];
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; if (saved === 'light' || saved === 'dark') { theme = saved; savedTheme = saved; } } catch (e) { } root.setAttribute(ATTR, theme);
/* 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() {275 行未改动全部展开 }
function setTheme(next) { theme = next; root.setAttribute(ATTR, next); paintTheme(next); try { storeSet(KEY_THEME, next); } catch (e) {} apply(); syncButtons(); syncPanel();255 行未改动全部展开 if (timer) return; 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();15 行未改动 boot(); }})();
```:::
点击保存后回到洛谷界面,刷新,顶栏会出现这两个按钮。左边按钮是切换暗色和亮色的,右边是设置。6 行未改动