// ==UserScript==
// @name BigBallOfMud Luogu - Ultra Optimized
// @namespace bigballofmud-luogu
// @version 20261008.02
// @updateURL https://raw.githubusercontent.com/Stars-Twinkle/BigBallOfMud-Luogu/main/bigballofmud-luogu-switch.user.js
// @downloadURL https://raw.githubusercontent.com/Stars-Twinkle/BigBallOfMud-Luogu/main/bigballofmud-luogu-switch.user.js
// @homepageURL https://github.com/Stars-Twinkle/BigBallOfMud-Luogu
// @description BigBallOfMud Luogu 极致优化版:保留主题切换、背景同步、网校适配等功能,尽量减少无意义的 DOM 查询与定时任务。
// @author acerkaio
// @license CC BY-NC-SA
// @match https://www.luogu.com.cn/*
// @match https://class.luogu.com.cn/*
// @run-at document-start
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function () {
'use strict';
/* =========================================================
配置
========================================================= */
var CONFIG = {
showToggle: true,
/*
* 对比度守卫彻底关闭。
*
* 原版即使 contrastGuard:false,
* 仍然会创建整个 body 的 MutationObserver。
*
* 本版完全不启动它。
*/
contrastGuard: false,
switchFallbackMs: 120,
/*
* 低频任务。
*
* 这些功能都不是实时功能,
* 没必要几百毫秒检查一次。
*/
buttonInterval: 5000,
backgroundInterval: 8000,
articleInterval: 5000,
schoolInterval: 5000,
themeEditorInterval: 3000,
/*
* 0 = 完全关闭 Tooltip 扫描。
*
* Tooltip 是一个非常不值得持续扫描的东西。
*/
tooltipInterval: 0,
debug: false
};
var MODE_KEY = 'sl-theme-mode';
var BG_CACHE_KEY = 'sl-theme-bg-image';
var MODES = [
'auto',
'dark',
'light'
];
var LABELS = {
auto: '配色:跟随系统',
dark: '配色:深色',
light: '配色:浅色'
};
var ICONS = {
auto:
'<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true">' +
'<circle cx="12" cy="12" r="8.5" fill="none" stroke="currentColor" stroke-width="1.8"/>' +
'<path d="M12 3.5a8.5 8.5 0 0 0 0 17z" fill="currentColor"/>' +
'</svg>',
dark:
'<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true">' +
'<path d="M20 14.6A8.6 8.6 0 0 1 9.4 4A7.2 7.2 0 1 0 20 14.6z" ' +
'fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>' +
'</svg>',
light:
'<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true">' +
'<circle cx="12" cy="12" r="4.2" fill="none" stroke="currentColor" stroke-width="1.8"/>' +
'<g stroke="currentColor" stroke-width="1.8" stroke-linecap="round">' +
'<path d="M12 2.4v2.6M12 19v2.6M2.4 12h2.6M19 12h2.6"/>' +
'<path d="M5.2 5.2l1.9 1.9M16.9 16.9l1.9 1.9"/>' +
'<path d="M18.8 5.2l-1.9 1.9M7.1 16.9l-1.9 1.9"/>' +
'</g>' +
'</svg>'
};
var root = document.documentElement;
var isMain =
location.hostname === 'www.luogu.com.cn';
var isSchool =
location.hostname === 'class.luogu.com.cn';
/* =========================================================
一、主题模式
========================================================= */
function getMode() {
try {
var value = localStorage.getItem(MODE_KEY);
if (MODES.indexOf(value) >= 0) {
return value;
}
} catch (e) {}
return 'auto';
}
function saveMode(mode) {
try {
localStorage.setItem(MODE_KEY, mode);
} catch (e) {}
}
function applyMode(mode) {
if (!root) {
root = document.documentElement;
}
if (!root) {
return;
}
if (mode === 'auto') {
root.removeAttribute('data-sl-theme');
try {
root.style.removeProperty('color-scheme');
} catch (e) {}
} else {
root.setAttribute(
'data-sl-theme',
mode
);
try {
root.style.setProperty(
'color-scheme',
mode
);
} catch (e) {}
}
}
function isDarkNow() {
if (!root) {
root = document.documentElement;
}
if (!root) {
return false;
}
var mode =
root.getAttribute(
'data-sl-theme'
);
if (mode === 'dark') {
return true;
}
if (mode === 'light') {
return false;
}
try {
return window.matchMedia(
'(prefers-color-scheme: dark)'
).matches;
} catch (e) {
return false;
}
}
function switchDurationMs() {
try {
if (!root) {
root = document.documentElement;
}
if (!root) {
return CONFIG.switchFallbackMs;
}
var value =
getComputedStyle(root)
.getPropertyValue(
'--sl-switch-duration'
)
.trim();
if (!value) {
return CONFIG.switchFallbackMs;
}
var number = parseFloat(value);
if (isNaN(number)) {
return CONFIG.switchFallbackMs;
}
if (value.indexOf('ms') >= 0) {
return number;
}
return number * 1000;
} catch (e) {
return CONFIG.switchFallbackMs;
}
}
function switchMode(callback) {
if (!root) {
root = document.documentElement;
}
if (!root) {
if (callback) {
callback();
}
return;
}
var duration =
switchDurationMs();
root.classList.add(
'sl-theme-switching'
);
if (callback) {
callback();
}
window.setTimeout(
function () {
root.classList.remove(
'sl-theme-switching'
);
},
duration
);
}
/* =========================================================
二、主题按钮
========================================================= */
function paintButton(button, mode) {
if (!button) {
return;
}
button.innerHTML =
ICONS[mode] || ICONS.auto;
button.title =
LABELS[mode] +
'(点击切换)';
button.setAttribute(
'aria-label',
LABELS[mode]
);
button.setAttribute(
'data-mode',
mode
);
}
function makeButton() {
var button =
document.createElement('button');
button.type = 'button';
button.className =
'sl-theme-toggle';
button.setAttribute(
'data-sl-theme-toggle',
'1'
);
button.style.cssText =
[
'display:inline-flex',
'align-items:center',
'justify-content:center',
'flex:0 0 auto',
'align-self:center',
'width:2rem',
'height:2rem',
'margin:0 .3rem',
'padding:0',
'background:transparent',
'border:1px solid currentColor',
'border-radius:99px',
'color:inherit',
'cursor:pointer',
'opacity:.75',
'transition:opacity .2s ease'
].join(';');
button.addEventListener(
'mouseenter',
function () {
button.style.opacity = '1';
}
);
button.addEventListener(
'mouseleave',
function () {
button.style.opacity = '.75';
}
);
button.addEventListener(
'click',
function (event) {
event.preventDefault();
event.stopPropagation();
var current =
getMode();
var index =
MODES.indexOf(current);
var next =
MODES[
(index + 1) %
MODES.length
];
saveMode(next);
switchMode(
function () {
applyMode(next);
paintButton(
button,
next
);
}
);
}
);
paintButton(
button,
getMode()
);
return button;
}
/* =========================================================
三、主站按钮位置
========================================================= */
function findMainButtonSpot(
bar,
button
) {
var links =
bar.querySelectorAll(
'a[href]'
);
var hit = null;
for (
var i = 0;
i < links.length;
i++
) {
var link = links[i];
if (
link === button ||
link.hasAttribute(
'data-sl-theme-toggle'
)
) {
continue;
}
var href =
link.getAttribute(
'href'
) || '';
if (
/(^|\/)(chat|notification)(\/|$|\?|#)/
.test(href)
) {
hit = link;
}
}
if (
hit &&
hit.parentElement
) {
return {
parent: hit.parentElement,
before: hit.nextSibling
};
}
var icon =
bar.querySelector(
'.fa-bell, .fa-envelope'
);
if (icon) {
var host =
icon.closest(
'a, button'
) || icon;
if (
host &&
host.parentElement
) {
return {
parent:
host.parentElement,
before:
host.nextSibling
};
}
}
var avatar =
bar.querySelector(
'.avatar'
);
if (avatar) {
var box =
avatar.closest('span') ||
avatar;
if (
box &&
box.parentElement
) {
return {
parent:
box.parentElement,
before:
box
};
}
}
return {
parent: bar,
before: null
};
}
/* =========================================================
四、网校按钮位置
========================================================= */
function findSchoolButtonSpot() {
var nav =
document.querySelector(
'nav.lfe-body'
);
if (nav) {
var learn =
nav.querySelector(
'a[href$="/learn"]'
);
if (
learn &&
learn.parentElement
) {
return {
parent:
learn.parentElement,
before:
learn.nextSibling,
layout: 'side'
};
}
return {
parent: nav,
before: null,
layout: 'side'
};
}
var userNav =
document.querySelector(
'nav.user-nav'
);
if (userNav) {
var service =
userNav.querySelector(
'a[href$="/service"]'
);
if (
service &&
service.parentElement
) {
return {
parent:
service.parentElement,
before: service,
layout: 'top'
};
}
return {
parent: userNav,
before:
userNav.firstChild,
layout: 'top'
};
}
return null;
}
/* =========================================================
五、确保按钮存在
========================================================= */
function ensureButton() {
if (!CONFIG.showToggle) {
return true;
}
/* -------------------------
网校
------------------------- */
if (isSchool) {
var schoolSpot =
findSchoolButtonSpot();
if (!schoolSpot) {
return false;
}
var button =
document.querySelector(
'[data-sl-theme-toggle]'
);
if (!button) {
button =
makeButton();
button.className =
'sl-theme-toggle ' +
'sl-theme-toggle-school';
}
if (
schoolSpot.layout ===
'side'
) {
button.style.display =
'flex';
button.style.margin =
'.5rem auto';
button.style.width =
'2.2rem';
button.style.height =
'2.2rem';
} else {
button.style.display =
'inline-flex';
button.style.margin =
'0 .35rem';
button.style.width =
'2rem';
button.style.height =
'2rem';
}
if (
button.parentElement !==
schoolSpot.parent ||
button.nextSibling !==
schoolSpot.before
) {
schoolSpot.parent.insertBefore(
button,
schoolSpot.before
);
}
var mode =
getMode();
if (
button.getAttribute(
'data-mode'
) !== mode
) {
paintButton(
button,
mode
);
}
return true;
}
/* -------------------------
主站
------------------------- */
if (!isMain) {
return true;
}
var bar =
document.querySelector(
'.top-bar'
);
if (!bar) {
return false;
}
var mainButton =
bar.querySelector(
'[data-sl-theme-toggle]'
);
if (!mainButton) {
mainButton =
makeButton();
}
var spot =
findMainButtonSpot(
bar,
mainButton
);
if (
mainButton.parentElement !==
spot.parent ||
mainButton.nextSibling !==
spot.before
) {
spot.parent.insertBefore(
mainButton,
spot.before
);
}
var currentMode =
getMode();
if (
mainButton.getAttribute(
'data-mode'
) !== currentMode
) {
paintButton(
mainButton,
currentMode
);
}
return true;
}
/* =========================================================
六、按钮低频检查
========================================================= */
function startButtonCheck() {
if (
!CONFIG.showToggle ||
(!isMain && !isSchool)
) {
return;
}
/*
* 第一次立即尝试。
*/
if (ensureButton()) {
return;
}
/*
* 只有第一次没找到时才创建定时器。
*
* 找到之后立即销毁。
*/
var timer =
window.setInterval(
function () {
if (ensureButton()) {
window.clearInterval(
timer
);
}
},
CONFIG.buttonInterval
);
}
/* =========================================================
七、网校内联主题色清理
========================================================= */
var SCHOOL_STRIP_SELECTOR = [
'nav.lfe-body',
'.main-container',
'main.wrapped.lfe-body',
'div.wrapped.lfe-body'
].join(',');
function stripInlineTheme() {
if (!isSchool) {
return;
}
try {
var nodes =
document.querySelectorAll(
SCHOOL_STRIP_SELECTOR
);
for (
var i = 0;
i < nodes.length;
i++
) {
var node =
nodes[i];
if (!node.style) {
continue;
}
node.style.removeProperty(
'background-color'
);
node.style.removeProperty(
'background-image'
);
node.style.removeProperty(
'color'
);
}
} catch (e) {}
}
/* =========================================================
八、背景图同步
========================================================= */
var BG_FALLBACK_LIGHT =
'url("https://cdn.luogu.com.cn/images/bg/fe/luogu4-bg-l.jpg")';
var BG_FALLBACK_DARK =
'url("https://cdn.luogu.com.cn/images/bg/fe/luogu4-bg-d.jpg")';
function bgCacheGet() {
try {
if (
typeof GM_getValue ===
'function'
) {
return GM_getValue(
BG_CACHE_KEY,
''
) || '';
}
} catch (e) {}
try {
return localStorage.getItem(
BG_CACHE_KEY
) || '';
} catch (e) {
return '';
}
}
function bgCacheSet(value) {
try {
if (
typeof GM_setValue ===
'function'
) {
GM_setValue(
BG_CACHE_KEY,
value
);
return;
}
} catch (e) {}
try {
localStorage.setItem(
BG_CACHE_KEY,
value
);
} catch (e) {}
}
function syncThemeBackground() {
try {
if (!root) {
return;
}
/*
* 如果 Stylus 已经指定背景,
* 脚本完全不干预。
*/
var styleValue =
getComputedStyle(root)
.getPropertyValue(
'--sl-bg-image'
)
.trim();
if (
styleValue &&
styleValue !== 'none'
) {
root.style.removeProperty(
'--sl-bg-active'
);
return;
}
var url = '';
var useDefault = false;
if (isMain) {
var page =
document.querySelector(
'.theme-page'
);
if (page) {
var raw =
String(
page.getAttribute(
'style'
) || ''
);
var match =
/--theme-body-image:\s*url\((['"]?)(.*?)\1\)/
.exec(raw);
if (!match) {
var computed =
getComputedStyle(
page
).getPropertyValue(
'--theme-body-image'
);
match =
/url\((['"]?)(.*?)\1\)/
.exec(
computed || ''
);
}
if (
match &&
match[2]
) {
url =
match[2];
bgCacheSet(
url
);
} else {
bgCacheSet('');
useDefault = true;
}
} else {
url =
bgCacheGet();
}
} else {
url =
bgCacheGet();
}
var wanted =
url
? 'url("' +
url +
'")'
: useDefault
? (
isDarkNow()
? BG_FALLBACK_DARK
: BG_FALLBACK_LIGHT
)
: '';
if (!wanted) {
root.style.removeProperty(
'--sl-bg-active'
);
return;
}
if (
root.style.getPropertyValue(
'--sl-bg-active'
) !== wanted
) {
root.style.setProperty(
'--sl-bg-active',
wanted
);
}
} catch (e) {}
}
/* =========================================================
九、主题编辑器
========================================================= */
var THEME_HIDE_PREFIX = [
'卡片毛玻璃',
'渐变角度',
'中景亮度',
'中景淡出',
'图片重复',
'图片尺寸',
'图片位置',
'背景底色',
'中景颜色',
'文字颜色',
'亮色主体'
];
var THEME_RENAME = {
'中景图片': '背景图'
};
function tweakThemeEditor() {
if (
!/\/theme(\/|$)/
.test(
location.pathname
)
) {
return;
}
try {
var labels =
document.querySelectorAll(
'.lfe-caption,' +
'.lfe-h3,' +
'label,' +
'.image-label > span'
);
for (
var i = 0;
i < labels.length;
i++
) {
var element =
labels[i];
var text =
(
element.textContent ||
''
).trim();
/*
* 中景图片 → 背景图
*/
if (
THEME_RENAME[text] &&
element.children.length === 0
) {
element.textContent =
THEME_RENAME[text];
}
var hide = false;
for (
var p = 0;
p <
THEME_HIDE_PREFIX.length;
p++
) {
if (
text.indexOf(
THEME_HIDE_PREFIX[p]
) === 0
) {
hide = true;
break;
}
}
if (!hide) {
continue;
}
var row =
element;
if (
!element.classList.contains(
'lfe-h3'
)
) {
var current =
element.parentElement;
while (
current &&
current !==
document.body
) {
if (
current.querySelectorAll(
'.lfe-caption'
).length <= 1
) {
row = current;
break;
}
current =
current.parentElement;
}
}
/*
* 卡片毛玻璃:
* 强制选择标准。
*/
if (
text.indexOf(
'卡片毛玻璃'
) === 0
) {
var radioLabels =
row.querySelectorAll(
'label'
);
for (
var k = 0;
k <
radioLabels.length;
k++
) {
var radioLabel =
radioLabels[k];
if (
(
radioLabel.textContent ||
''
).indexOf(
'标准'
) < 0
) {
continue;
}
var radio =
radioLabel.querySelector(
'input[type="radio"]'
);
var checked =
radio
? radio.checked
: /checked|active|selected/
.test(
radioLabel.className
);
if (!checked) {
try {
radioLabel.click();
} catch (e) {}
}
break;
}
}
if (
row &&
row.style.display !==
'none'
) {
row.style.display =
'none';
}
}
} catch (e) {}
}
/* =========================================================
十、深色文章标题修复
========================================================= */
var DARK_ARTICLE_SELECTOR =
'.l-card .row.title > a';
function fixDarkArticleText() {
if (!isMain) {
return;
}
/*
* 浅色模式不需要做任何扫描。
*
* 这是一个重要优化。
*/
if (!isDarkNow()) {
return;
}
try {
var links =
document.querySelectorAll(
DARK_ARTICLE_SELECTOR
);
if (!links.length) {
return;
}
var wanted =
'#f7fafc';
try {
var value =
getComputedStyle(
root
).getPropertyValue(
'--lg-text'
).trim();
if (value) {
wanted = value;
}
} catch (e) {}
for (
var i = 0;
i < links.length;
i++
) {
var link =
links[i];
var inline =
link.style.color ||
'';
if (
!link.dataset.slOrigColor &&
inline
) {
link.dataset.slOrigColor =
inline;
}
if (
link.style.color !==
wanted
) {
link.style.color =
wanted;
}
}
} catch (e) {}
}
/* =========================================================
十一、Tooltip
========================================================= */
function hoistTooltips() {
/*
* 极致优化版默认关闭。
*/
if (
CONFIG.tooltipInterval <= 0
) {
return;
}
try {
var tips =
document.querySelectorAll(
'.tooltip, .lfe-tooltip'
);
for (
var i = 0;
i < tips.length;
i++
) {
var tip =
tips[i];
if (
tip.parentElement ===
document.body
) {
continue;
}
if (
tip.dataset.slHoisted
) {
continue;
}
var style =
getComputedStyle(
tip
);
if (
style.position !==
'fixed' &&
style.position !==
'absolute'
) {
continue;
}
try {
document.body.appendChild(
tip
);
tip.dataset.slHoisted =
'1';
} catch (e) {}
}
} catch (e) {}
}
/* =========================================================
十二、系统主题变化
========================================================= */
function installColorSchemeListener() {
try {
var media =
window.matchMedia(
'(prefers-color-scheme: dark)'
);
var handler =
function () {
/*
* 只有 auto 模式才需要处理。
*/
if (
getMode() !==
'auto'
) {
return;
}
applyMode(
'auto'
);
/*
* 系统主题切换时,
* 深色才需要修文章标题。
*/
if (
isMain &&
isDarkNow()
) {
fixDarkArticleText();
}
};
if (
media.addEventListener
) {
media.addEventListener(
'change',
handler
);
} else if (
media.addListener
) {
media.addListener(
'change',
handler
);
}
} catch (e) {}
}
/* =========================================================
十三、定时任务
========================================================= */
function startBackgroundTimer() {
syncThemeBackground();
window.setInterval(
syncThemeBackground,
CONFIG.backgroundInterval
);
}
function startArticleTimer() {
if (!isMain) {
return;
}
fixDarkArticleText();
window.setInterval(
fixDarkArticleText,
CONFIG.articleInterval
);
}
function startSchoolTimer() {
if (!isSchool) {
return;
}
stripInlineTheme();
window.setInterval(
stripInlineTheme,
CONFIG.schoolInterval
);
}
function startTooltipTimer() {
/*
* tooltipInterval = 0 时完全不启动。
*/
if (
CONFIG.tooltipInterval <= 0 ||
!isMain
) {
return;
}
hoistTooltips();
window.setInterval(
hoistTooltips,
CONFIG.tooltipInterval
);
}
/* =========================================================
十四、主题编辑器 MutationObserver
========================================================= */
function startThemeEditorObserver() {
if (
!isMain ||
!/\/theme(\/|$)/
.test(
location.pathname
)
) {
return;
}
tweakThemeEditor();
/*
* 只有主题编辑器页面允许 MutationObserver。
*
* 普通洛谷页面:
* 0 个 body MutationObserver。
*/
if (
!window.MutationObserver
) {
return;
}
var pending = false;
var observer =
new MutationObserver(
function () {
if (pending) {
return;
}
pending = true;
window.setTimeout(
function () {
pending = false;
tweakThemeEditor();
},
300
);
}
);
observer.observe(
document.documentElement,
{
childList: true,
subtree: true
}
);
}
/* =========================================================
十五、初始化
========================================================= */
function init() {
root =
document.documentElement;
if (!root) {
return;
}
/*
* 第一次主题设置。
*/
applyMode(
getMode()
);
/*
* 按钮。
*
* 找不到才开启 5 秒一次的检查。
* 找到后立即停止。
*/
startButtonCheck();
/*
* 背景。
*/
startBackgroundTimer();
/*
* 深色文章标题。
*/
startArticleTimer();
/*
* 网校。
*/
startSchoolTimer();
/*
* 主题编辑器。
*/
startThemeEditorObserver();
/*
* Tooltip。
*
* 默认关闭。
*/
startTooltipTimer();
/*
* 系统主题变化。
*/
installColorSchemeListener();
/*
* DOMContentLoaded:
* 只补一次,不创建长期监听。
*/
if (
document.readyState ===
'loading'
) {
document.addEventListener(
'DOMContentLoaded',
function () {
ensureButton();
syncThemeBackground();
if (isMain) {
fixDarkArticleText();
}
if (isSchool) {
stripInlineTheme();
}
if (
isMain &&
/\/theme(\/|$)/
.test(
location.pathname
)
) {
tweakThemeEditor();
}
},
{
once: true
}
);
}
/*
* load:
* 再补一次。
*/
window.addEventListener(
'load',
function () {
ensureButton();
syncThemeBackground();
if (isMain) {
fixDarkArticleText();
}
if (isSchool) {
stripInlineTheme();
}
},
{
once: true
}
);
}
/* =========================================================
十六、调试接口
========================================================= */
window.__slDebug =
function () {
return {
mode:
getMode(),
attr:
root &&
root.getAttribute(
'data-sl-theme'
),
isDark:
isDarkNow(),
switchDurationMs:
switchDurationMs(),
contrastGuard:
CONFIG.contrastGuard,
intervals: {
button:
CONFIG.buttonInterval,
background:
CONFIG.backgroundInterval,
article:
CONFIG.articleInterval,
school:
CONFIG.schoolInterval,
themeEditor:
CONFIG.themeEditorInterval,
tooltip:
CONFIG.tooltipInterval
}
};
};
/* =========================================================
十七、启动
========================================================= */
if (
document.documentElement
) {
init();
} else {
/*
* document-start 极早阶段,
* <html> 还没出现时只监听 document 的
* childList,不监听整个 body。
*/
var rootObserver =
new MutationObserver(
function () {
if (
document.documentElement
) {
rootObserver.disconnect();
init();
}
}
);
rootObserver.observe(
document,
{
childList: true
}
);
}
})();剪贴板
剪贴板 n2chn8sp
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新