Luogu 侧边栏&导航栏 透明化
有一天我发现,侧边栏和导航栏无论如何都很丑,所以找 ds 做了一个插件,可以将你的背景拉到全屏,然后导航栏透明化后就等于是自动适配你的背景了
// ==UserScript==
// @name 洛谷全屏背景与导航侧边栏半透明化
// @namespace https://www.luogu.com.cn/
// @version 4.0
// @description 让洛谷主题背景图铺满全屏,并让顶部导航栏与侧边栏半透明
// @author Assistant
// @match https://www.luogu.com.cn/*
// @match https://*.luogu.com.cn/*
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
// ====== 可配置参数 ======
const OPACITY = 65; // 导航栏/侧边栏不透明度百分比(保留多少原色,建议 40~80)
const BLUR_PX = 16; // 毛玻璃模糊程度(像素)
// ========================
// 1. 注入 CSS:接管全局背景 + 半透明化
const css = `
/* ===== 让 html 和 body 接管全屏背景 ===== */
html, body {
/* 把背景色、图片、渐变应用到最外层 */
background-color: var(--theme-body-back, #ffffff) !important;
background-image: var(--theme-body-color, none), var(--theme-body-image, none) !important;
background-repeat: no-repeat, no-repeat !important;
background-size: cover, cover !important;
background-position: center, center !important;
background-attachment: fixed, fixed !important;
}
/* 隐藏洛谷原本限制在主内容区的背景伪元素,避免重复渲染 */
.theme-page::before {
display: none !important;
}
.theme-page {
background-color: transparent !important;
}
/* ===== 顶部导航栏 (.top-bar) 半透明 ===== */
.top-bar {
background-color: color-mix(
in srgb,
var(--theme-navi-back, #ffffff) ${OPACITY}%,
transparent
) !important;
backdrop-filter: blur(${BLUR_PX}px) !important;
-webkit-backdrop-filter: blur(${BLUR_PX}px) !important;
border-bottom: 1px solid rgba(128, 128, 128, 0.15) !important;
}
/* ===== 左侧边栏 (.sidebar, .lside) 半透明 ===== */
.sidebar,
.lside {
background-color: color-mix(
in srgb,
var(--theme-sidebar-back, var(--theme-navi-back, #ffffff)) ${OPACITY}%,
transparent
) !important;
backdrop-filter: blur(${BLUR_PX}px) !important;
-webkit-backdrop-filter: blur(${BLUR_PX}px) !important;
border-right: 1px solid rgba(128, 128, 128, 0.15) !important;
}
`;
if (typeof GM_addStyle !== 'undefined') {
GM_addStyle(css);
} else {
const style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
}
// 2. JavaScript:将 .theme-page 上的背景变量同步到根节点 html
function syncThemeBackground() {
const themePage = document.querySelector('.theme-page');
if (!themePage) return;
const root = document.documentElement;
// 提取截图里出现的所有背景相关变量
const properties = [
'--theme-body-back',
'--theme-body-image',
'--theme-body-image-repeat',
'--theme-body-image-size',
'--theme-body-image-position',
'--theme-body-color',
'--theme-body-color-filter',
'--theme-body-mask'
];
properties.forEach(prop => {
const val = themePage.style.getPropertyValue(prop);
if (val) {
root.style.setProperty(prop, val);
}
});
}
// 洛谷是 Vue 渲染,初始加载和切换主题时变量会变,必须监听同步
document.addEventListener('DOMContentLoaded', syncThemeBackground);
// 使用 MutationObserver 监听主容器的样式变化,实现实时生效
const observer = new MutationObserver(() => {
syncThemeBackground();
});
const domObserver = new MutationObserver(() => {
const themePage = document.querySelector('.theme-page');
if (themePage) {
observer.observe(themePage, { attributes: true, attributeFilter: ['style', 'class'] });
syncThemeBackground();
}
});
domObserver.observe(document.documentElement, { childList: true, subtree: true });
})();