// ==UserScript==
// @name 洛谷 · 深色透明与自定义背景
// @namespace local.luogu.dark-transparent
// @version 1.3.0
// @description 独立深色外观:背景链接、面板透明度、背景亮度;支持旧首页和新版题库、文章、讨论等主站页面,默认无模糊、无折射。
// @match https://www.luogu.com.cn/*
// @match https://www.luogu.com/*
// @match https://class.luogu.com.cn/*
// @run-at document-start
// @noframes
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @license MIT
// ==/UserScript==
(() => {
'use strict';
if (document.getElementById('lgt-theme-style')) return;
// ===== 默认设置:也可以在网页右下角的“外观设置”里修改 =====
const DEFAULTS = {
backgroundUrl: 'https://cdn.luogu.com.cn/upload/image_hosting/9ad67u3t.webp',
panelOpacity: 0.48, // 面板不透明度:越小越透,文字本身不变透明
contentOpacity: 0.55, // 内页不透明度:题目、文章、题库等使用更实的底色
imageVisibility: 0.70, // 背景图可见度:越大越亮
};
const KEY = 'luogu-dark-transparent-v1';
const ROOT = document.documentElement;
if (!ROOT) return;
const clamp = (n, fallback, min = 0, max = 1) => Number.isFinite(Number(n)) ? Math.max(min, Math.min(max, Number(n))) : fallback;
function validUrl(value) {
const s = String(value || '').trim();
if (!s) return '';
if (/[\r\n\x00]/.test(s)) throw new Error('图片链接不能包含换行。');
const u = new URL(s);
if (u.protocol !== 'https:' || u.username || u.password) throw new Error('请粘贴 HTTPS 图片直链,不能使用电脑文件路径。');
return u.href;
}
function clean(value) {
let backgroundUrl;
try { backgroundUrl = validUrl(value.backgroundUrl); } catch { backgroundUrl = DEFAULTS.backgroundUrl; }
return { appearanceVersion: 3, backgroundUrl, panelOpacity: clamp(value.panelOpacity, DEFAULTS.panelOpacity, 0, 1), contentOpacity: clamp(value.contentOpacity, DEFAULTS.contentOpacity, 0, 1), imageVisibility: clamp(value.imageVisibility, DEFAULTS.imageVisibility) };
}
function load() {
try {
const value = typeof GM_getValue === 'function' ? GM_getValue(KEY, null) : JSON.parse(localStorage.getItem(KEY) || 'null');
const next = { ...DEFAULTS, ...(value && typeof value === 'object' ? value : {}) };
if (value && !value.appearanceVersion) next.panelOpacity = Math.min(Number(next.panelOpacity) || DEFAULTS.panelOpacity, 0.48);
return clean(next);
} catch { return { ...DEFAULTS }; }
}
let saved = load();
const style = document.createElement('style');
style.id = 'lgt-theme-style';
ROOT.appendChild(style);
const CARDS = '.lg-article,.lg-summary,.lg-article-sub,.lg-content-table-left,.am-panel,.card,.l-card,.header-card,.feed-card,.inner-card,.float-card,.contest-card,.training-card,.training-list-card,.member-card,.info-card,.section-card,.reply-card,.paste-card,.notice-card,.list-card,.select-card,.intro-card,.vip-card,.admin-card,.filter-card,.theme-list-card,.ticket-card,.category-card,.am-comment-main,.am-comment-hd,.am-comment-bd,.table-container,.table-wrap,.filter-wrapper,.toc-wrapper,.reply-item';
const SHELLS = '#app,.main-container,.theme-page,main,.columba-content-wrap.main-content,.sidebar-container,.full-container';
const NAVS = '.lside,.rside,nav.sidebar,.lg-sidebar,.lg-header,#app-header,.top-bar,.user-nav';
const EDITORS = '.mp-editor-container,.mp-editor,.markdown-editor,.md-editor,#mp-editor,.CodeMirror,.cm-editor,.monaco-editor,.ace_editor';
const CODE = 'pre,code,.hljs,.katex,.MathJax,math,svg,canvas,.monaco-editor,.ace_editor,.CodeMirror,[contenteditable="true"]';
const POPUPS = '[role="dialog"],.am-modal-dialog,.am-dropdown-content,.popup,.dropdown,.candidates';
const SEMANTIC = '.am-badge,.tag,.badge,.test-case,.heat-map-cell,.verdict,.judge-status,.record-status,.score';
function apply(config) {
const c = clean(config);
const url = c.backgroundUrl ? `url(${JSON.stringify(c.backgroundUrl)})` : 'none';
const a = c.panelOpacity;
style.textContent = `
html:root {
color-scheme: dark !important;
--lgt-home-alpha: ${a};
--lgt-content-alpha: ${c.contentOpacity};
--lgt-alpha: var(--lgt-content-alpha);
--lgt-panel: rgba(10, 14, 20, var(--lgt-alpha));
--lgt-popup: var(--lgt-panel);
--lgt-radius: 22px;
--lgt-ink: #e4eaf2;
--lgt-muted: #aab6c6;
--lgt-line: rgba(205,220,240,.16);
--lgt-link: #70baff;
--lg-text: #e4eaf2; --lg-text-weak: #aab6c6;
--lg-panel-dark: var(--lgt-panel); --lg-panel-large-dark: var(--lgt-panel);
--lg-blur: 0px; --lg-card-blur: 0px; --lg-blur-large: 0px;
background-color: #10151d !important;
background-image: linear-gradient(rgba(8,12,19,${1-c.imageVisibility}),rgba(8,12,19,${1-c.imageVisibility})), ${url} !important;
background-position: center !important;
background-size: cover !important;
background-repeat: no-repeat !important;
background-attachment: fixed !important;
min-height: 100%;
}
html:root[data-lgt-page="home"] { --lgt-alpha: var(--lgt-home-alpha); }
/* 同时关闭卡片本体与伪元素的背景模糊;不模糊文字、图片或壁纸。 */
html body *, html body *::before, html body *::after,
html body #app *, html body #app *::before, html body #app *::after {
backdrop-filter: none !important;
-webkit-backdrop-filter: none !important;
}
html body { color-scheme: dark !important; color: var(--lgt-ink) !important; background: transparent !important; min-height:100vh; }
html body :is(${SHELLS}), html body [data-lgt-shell] {
background: transparent !important; color-scheme: dark !important;
color: var(--lgt-ink); --theme-card-background: var(--lgt-panel) !important;
}
html body .theme-page::before { display:none !important; }
html body [data-lgt-surface] {
background: var(--lgt-panel) !important; color:var(--lgt-ink);
border-color: var(--lgt-line) !important; border-radius:var(--lgt-radius) !important;
box-shadow:inset 0 1px 0 rgb(255 255 255 / 12%),0 3px 14px rgb(0 0 0 / 15%) !important;
color-scheme:dark !important;
}
html body :is(${CARDS}) { border-radius:var(--lgt-radius) !important; }
html body [data-lgt-surface="popup"] { background:var(--lgt-popup) !important; }
html body [data-lgt-clear] { background:transparent !important; box-shadow:none !important; }
html body :is([data-lgt-shell],[data-lgt-surface],[data-lgt-clear])::before,
html body :is([data-lgt-shell],[data-lgt-surface],[data-lgt-clear])::after {
background-image:none !important; background-color:transparent !important;
}
html body [data-lgt-ink] { color:var(--lgt-ink) !important; }
html body a[data-lgt-ink] { color:var(--lgt-link) !important; }
html body a:not([class]):not([style]) { color:var(--lgt-link); }
html body :is(.article-content,.solution-article,.lfe-marked,.marked) { color:var(--lgt-ink) !important; }
html body :is(.lfe-caption,.lg-small,.am-comment-meta,.caption,.update-info) { color:var(--lgt-muted); }
html body :is(input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=submit]):not([type=button]),textarea,select) {
color-scheme:dark !important; background-color:var(--lgt-panel) !important; border-radius:9px !important;
color:var(--lgt-ink) !important; border-color:var(--lgt-line) !important;
}
html body input::placeholder,html body textarea::placeholder { color:#95a4b9 !important; }
html body :is(pre,.hljs) { background-color:var(--lgt-panel) !important; border-radius:10px; color:#dce4ef !important; text-shadow:none !important; }
html body :is(.lfe-marked,.marked) :is(blockquote,details) { border-color:#407aaa; }
html body :is(hr,th,td,.row-wrap,.lg-table-row) { border-color:var(--lgt-line) !important; }
html body :is(.lg-header,#app-header,.top-bar,.user-nav,#app > .lfe-body:not(main)) {
background-color:var(--lgt-panel) !important; color:var(--lgt-ink);
}
html body :is(table,thead,tbody,tr,td,th)[data-lgt-clear] { background:transparent !important; }
html body .highcharts-background { fill:transparent !important; }
html body :is(.highcharts-axis-labels,.highcharts-axis-title,.highcharts-title) text { fill:#b4c1d2 !important; }
/* 侧栏、评论头和短评论不再漏掉;内层透明,避免多次叠加压成实心。 */
html body :is(.am-comment-hd,.am-comment-bd) { background:transparent !important; border-color:var(--lgt-line) !important; }
html body .am-comment-hd { border-radius:22px 22px 0 0 !important; }
html body .am-comment-bd { border-radius:0 0 22px 22px !important; }
html body .am-comment-main::before,html body .am-comment-main::after { display:none !important; }
html body [data-lgt-nav] { border-radius:24px !important; color:var(--lgt-ink) !important; }
html body [data-lgt-nav] :is(a,.icon,svg) { color:var(--lgt-ink) !important; }
html body button:not([role=switch]),html body .am-btn { border-radius:10px; }
html body [data-lgt-reading="article"] { padding:22px !important; box-sizing:border-box !important; }
html body [data-lgt-reading="toc"] { padding:14px !important; box-sizing:border-box !important; }
/* 写作编辑器独立铺底,不能按普通嵌套卡片清成透明。 */
html body [data-lgt-editor] {
background:rgba(10,14,20,.90) !important; color:#e4eaf2 !important;
border:1px solid #465369 !important; border-radius:12px !important;
box-shadow:none !important; color-scheme:dark !important;
}
html body [data-lgt-editor-full] { background:rgba(10,14,20,.97) !important; border-radius:0 !important; }
html body [data-lgt-editor] :is(.CodeMirror-scroll,.CodeMirror-sizer,.cm-scroller,.cm-content,.mp-editor-ground,.mp-editor-area,textarea) { background:transparent !important; color:#e4eaf2; }
html body :is(.CodeMirror-gutters,.cm-gutters,.ace_gutter) { background:#17202d !important; border-color:#47556b !important; color:#b9c9dd !important; }
html body :is(.CodeMirror-linenumber,.cm-gutterElement,.ace_gutter-cell),html body .monaco-editor .line-numbers { color:#b9c9dd !important; background:transparent !important; opacity:1 !important; }
html body :is(.CodeMirror-activeline-gutter,.cm-activeLineGutter) { background:#26394f !important; color:#fff !important; }
html body :is(.mp-editor-toolbar,.md-editor-toolbar,.editor-toolbar) { background:#17202d !important; color:#e4eaf2 !important; }
html body :is(.CodeMirror-cursor,.cm-cursor) { border-left-color:#e8f2ff !important; }
html body .CodeMirror-selected { background:#2d4c70 !important; }
html body [data-lgt-art-only] { background:transparent !important; border:0 !important; border-radius:0 !important; box-shadow:none !important; }
/* 不给文字或图片设置 opacity;不对整页使用反色滤镜。 */
`;
forceDark();
}
function setProp(el, name, value) {
if (el.style.getPropertyValue(name) !== value || el.style.getPropertyPriority(name) !== 'important') el.style.setProperty(name, value, 'important');
}
function forceDark() {
const route = /^\/(?:index\/?|index\.php)?$/.test(location.pathname) ? 'home' : 'content';
if(ROOT.getAttribute('data-lgt-page')!==route) ROOT.setAttribute('data-lgt-page',route);
if (ROOT.getAttribute('data-lcolor-theme') !== 'dark') ROOT.setAttribute('data-lcolor-theme', 'dark');
setProp(ROOT, 'color-scheme', 'dark');
if (document.body) setProp(document.body, 'color-scheme', 'dark');
}
apply(saved);
function rgb(s) {
const m = String(s).match(/^rgba?\(\s*([\d.]+)[,\s]+([\d.]+)[,\s]+([\d.]+)(?:\s*[,/]\s*([\d.]+))?\s*\)$/);
return m ? [+m[1], +m[2], +m[3], m[4] == null ? 1 : +m[4]] : null;
}
const neutral = v => v && Math.max(...v.slice(0,3))-Math.min(...v.slice(0,3)) <= 32;
function mark(el, name, value = '') {
if (el.getAttribute(name) !== value) el.setAttribute(name, value);
}
function surface(el, kind = 'panel') {
mark(el,'data-lgt-surface',kind);
// 实际面板写入内联优先级,避免组件的 scoped CSS 或后加载样式重新铺白底。
setProp(el,'background',kind === 'popup' ? 'var(--lgt-popup)' : 'var(--lgt-panel)');
setProp(el,'border-radius',el.hasAttribute('data-lgt-nav') ? '24px' : '22px');
}
function clear(el, shell = false) {
if(el.hasAttribute('data-lgt-editor')){setProp(el,'background',el.hasAttribute('data-lgt-editor-full')?'rgba(10,14,20,.97)':'rgba(10,14,20,.90)');return;}
if(el.hasAttribute('data-lgt-reading')){surface(el);return;}
mark(el, shell ? 'data-lgt-shell' : 'data-lgt-clear');
el.removeAttribute('data-lgt-surface');
setProp(el,'background','transparent');
}
function prepareArticles(scope) {
const articles = [...scope.querySelectorAll('.article-content,.solution-article')];
if (scope.matches?.('.article-content,.solution-article')) articles.push(scope);
for (const article of articles) {
const solution = article.closest('.solution-article');
if (solution) {
// 题解条目本身就是阅读面板,不能用 main 的顶层祖先替代。
mark(solution,'data-lgt-reading');
solution.removeAttribute('data-lgt-clear');
solution.removeAttribute('data-lgt-shell');
surface(solution);
solution.querySelectorAll('.article-content,.lfe-marked-wrap,.lfe-marked,.marked').forEach(e=>{if(e!==solution)clear(e);});
continue;
}
// 文章正文与目录各自铺底,不在它们的共同外壳上画一整块底板。
const reading = article.querySelector('.lfe-marked-wrap') || article.querySelector('.lfe-marked,.marked') || article;
mark(reading,'data-lgt-reading','article');
reading.removeAttribute('data-lgt-clear');
reading.removeAttribute('data-lgt-shell');
for(let p=reading.parentElement;p && p!==document.body;p=p.parentElement) clear(p,true);
surface(reading);
reading.querySelectorAll('.lfe-marked-wrap,.lfe-marked,.marked').forEach(e=>{if(e!==reading)clear(e);});
}
if(articles.length) for(const toc of document.querySelectorAll('.toc-wrapper')) {
mark(toc,'data-lgt-reading','toc');
toc.removeAttribute('data-lgt-clear');toc.removeAttribute('data-lgt-shell');
for(let p=toc.parentElement;p && p!==document.body;p=p.parentElement) clear(p,true);
surface(toc);
}
}
function prepareWidgets() {
const full=document.fullscreenElement;
for(const old of document.querySelectorAll('[data-lgt-editor-full]:not([data-lgt-editor])')) {
if(!/(?:^|[\s_-])full-?screen(?:$|[\s_-])/i.test(old.className || '')) {old.removeAttribute('data-lgt-editor-full');clear(old,true);}
}
for(const editor of document.querySelectorAll(EDITORS)) {
mark(editor,'data-lgt-editor');
editor.removeAttribute('data-lgt-clear');editor.removeAttribute('data-lgt-shell');editor.removeAttribute('data-lgt-surface');
const rect=editor.getBoundingClientRect();
let fullscreen=!!(full && (full===editor || full.contains(editor)));
let ancestor=editor;
for(let depth=0;ancestor && ancestor!==document.body && depth<5;depth++,ancestor=ancestor.parentElement){
if(/(?:^|[\s_-])full-?screen(?:$|[\s_-])/i.test(ancestor.className || '')) {
fullscreen=true;
mark(ancestor,'data-lgt-editor-full');
setProp(ancestor,'background','rgba(10,14,20,.97)');
break;
}
}
if(getComputedStyle(editor).position==='fixed' && rect.width>=innerWidth*.8 && rect.height>=innerHeight*.7)fullscreen=true;
if(fullscreen)mark(editor,'data-lgt-editor-full');else editor.removeAttribute('data-lgt-editor-full');
setProp(editor,'background',fullscreen?'rgba(10,14,20,.97)':'rgba(10,14,20,.90)');
setProp(editor,'color','#e4eaf2');
}
// 只清除大型图片弹层自身的装饰框;保留图片及页面遮罩。
for(const img of document.querySelectorAll('img')) {
const ir=img.getBoundingClientRect();
if(ir.width<280 || ir.height<200 || img.closest('.article-content,.lfe-marked,.marked'))continue;
let box=img.parentElement;
for(let i=0;box && box!==document.body && i<4;i++,box=box.parentElement){
if(box.id==='app' || box.matches('.swal2-container,.am-dimmer,.modal-mask'))break;
const br=box.getBoundingClientRect(), cs=getComputedStyle(box);
const inPopup=box.matches('.popup,.modal,.modal-content,.swal2-popup,.am-modal-dialog,[role="dialog"]')||['fixed','absolute'].includes(cs.position);
const text=box.textContent.replace(/[×✕✖]/g,'').trim();
if(inPopup && !text && box.querySelectorAll('img').length===1 && br.width<=ir.width+120 && br.height<=ir.height+120) {
for(let inner=img.parentElement;inner && inner!==box;inner=inner.parentElement){
mark(inner,'data-lgt-art-only');inner.removeAttribute('data-lgt-surface');setProp(inner,'background','transparent');setProp(inner,'box-shadow','none');setProp(inner,'border','0');
}
mark(box,'data-lgt-art-only');box.removeAttribute('data-lgt-surface');
setProp(box,'background','transparent');setProp(box,'box-shadow','none');setProp(box,'border','0');setProp(box,'border-radius','0');
}
}
}
}
function inspect(el) {
if(el.hasAttribute('data-lgt-art-only') || el.closest(EDITORS) || el.hasAttribute('data-lgt-editor-full'))return;
if (el.id?.startsWith('lgt-') || el.closest(CODE) || el.closest(SEMANTIC)) return;
if (el.hasAttribute('data-lgt-reading')) { surface(el); return; }
if (el.matches(SHELLS) || el.hasAttribute('data-lgt-shell')) { clear(el,true); return; }
const cs = getComputedStyle(el);
// 没有模糊和折射。仅撤掉面板滤镜,不改图片及作者内容里的普通 filter。
if (cs.backdropFilter && cs.backdropFilter !== 'none') setProp(el,'backdrop-filter','none');
if (cs.webkitBackdropFilter && cs.webkitBackdropFilter !== 'none') setProp(el,'-webkit-backdrop-filter','none');
const bg = rgb(cs.backgroundColor);
const hasImage = /url\(/i.test(cs.backgroundImage || '');
const popup = el.matches(POPUPS);
const nav = el.matches(NAVS);
if (nav) mark(el,'data-lgt-nav');
const known = el.matches(CARDS) || popup || nav;
const structural = /^(DIV|SECTION|ARTICLE|ASIDE|NAV|HEADER|FOOTER|FORM|UL|TABLE|THEAD|TBODY|TR|TD|TH)$/.test(el.tagName);
const rect = structural && !known ? el.getBoundingClientRect() : null;
const large = rect && ((rect.width >= 120 && rect.height >= 12) || (rect.width >= 35 && rect.height >= 100));
if (!el.hasAttribute('data-lgt-clear') && !hasImage && (known || (structural && large && neutral(bg) && bg[3] > 0.02))) {
const parentPanel = el.parentElement?.closest('[data-lgt-surface]');
if (parentPanel && !popup) clear(el);
else surface(el, popup ? 'popup' : 'panel');
}
// 只修中性深色文字。用户名、难度色、标签、公式和代码 token 保留。
if (!el.hasAttribute('data-lgt-ink')) {
const fg = rgb(cs.color);
const directText = [...el.childNodes].some(n => n.nodeType === 3 && n.textContent.trim());
if (directText && neutral(fg) && fg[3] > 0 && Math.max(...fg.slice(0,3)) < 160) {
mark(el,'data-lgt-ink');
setProp(el,'color',el.tagName === 'A' ? 'var(--lgt-link)' : 'var(--lgt-ink)');
}
}
}
// 按帧分批扫描,页面换路由或异步插入内容时只处理发生变化的区域。
let pending = new Set(), timer = 0, running = false;
function queue(node) {
const e = node?.nodeType === 1 ? node : node?.parentElement;
if (!e || e.id?.startsWith('lgt-') || e.closest?.('#lgt-settings-host')) return;
pending.add(e);
if (!timer) timer = setTimeout(flush,100);
}
async function flush() {
timer = 0;
if (running) { timer=setTimeout(flush,100); return; }
running = true;
try {
forceDark();
prepareWidgets();
const roots = [...pending].filter(e=>e.isConnected);
pending.clear();
const unique = roots.filter(e=>!roots.some(p=>p!==e && p.contains(e)));
for (const root of unique) {
prepareArticles(document); // 文章祖先可能在本次变更区域之外。
const walker = document.createTreeWalker(root,NodeFilter.SHOW_ELEMENT);
let el=root, n=0;
do {
if (el.isConnected) inspect(el);
if (++n % 160 === 0) await new Promise(resolve=>requestAnimationFrame(resolve));
el=walker.nextNode();
} while (el);
}
} finally { running=false; if (pending.size && !timer) timer=setTimeout(flush,100); }
}
let ui;
function mountUi() {
if (!document.body || document.getElementById('lgt-settings-host')) return;
const host=document.createElement('div'); host.id='lgt-settings-host';
host.style.cssText='position:fixed!important;right:18px!important;bottom:76px!important;z-index:2147483646!important;';
const shadow=host.attachShadow({mode:'open'});
shadow.innerHTML=`<style>
:host{font:14px/1.6 "Microsoft YaHei",sans-serif;color-scheme:dark;color:#e4eaf2}
*{box-sizing:border-box}button,input{font:inherit}button{cursor:pointer;color:#e4eaf2;background:#233349;border:1px solid #49617a;border-radius:7px;padding:8px 13px}
#open{box-shadow:0 2px 10px #0008;background:#1a2538}dialog{color:#e4eaf2;background:#17202c;border:1px solid #486078;border-radius:12px;width:430px;max-width:calc(100vw - 28px);padding:22px;max-height:90vh;overflow:auto}
dialog::backdrop{background:#0009}h2{font-size:20px;margin:0 0 14px}label{display:block;margin:15px 0 5px}input[type=url]{width:100%;padding:9px;color:#f1f5ff;background:#101721;border:1px solid #52657c;border-radius:5px}input[type=range]{width:100%;accent-color:#61adf5}
small{color:#b2c0d2;display:block}p{margin:8px 0}.actions{display:flex;gap:8px;flex-wrap:wrap;margin-top:18px}#save{background:#2169a7}#status{min-height:22px;color:#9bcaff;font-size:13px}output{float:right}button:focus-visible,input:focus-visible{outline:2px solid #8ac7ff;outline-offset:3px}
</style>
<button id="open" type="button" aria-haspopup="dialog">外观设置</button>
<dialog aria-labelledby="heading"><h2 id="heading">洛谷 · 深色透明</h2>
<label for="url">背景图片链接</label><input id="url" type="url" placeholder="https://… 图片直链;留空为纯色背景">
<small>粘贴能直接显示图片的 HTTPS 链接。</small>
<label for="opacity">首页面板不透明度 <output id="opacity-value"></output></label><input id="opacity" type="range" min="0" max="100" step="1"><small>向左更透明,向右更实;文字不会一起变淡。</small>
<label for="content-opacity">内页面板不透明度 <output id="content-opacity-value"></output></label><input id="content-opacity" type="range" min="0" max="100" step="1"><small>题目、题库、文章、讨论等内页使用此值,建议 65%~80%。</small>
<label for="visibility">背景图可见度 <output id="visibility-value"></output></label><input id="visibility" type="range" min="0" max="100" step="1"><small>向左压暗背景,向右更明亮。默认不模糊、不折射。</small>
<p id="status" role="status" aria-live="polite"></p>
<div class="actions"><button id="save" type="button">保存并应用</button><button id="cancel" type="button">取消</button><button id="defaults" type="button">恢复默认值</button></div></dialog>`;
document.body.appendChild(host);
const $=s=>shadow.querySelector(s), dialog=$('dialog');
function fill(c) { $('#url').value=c.backgroundUrl;$('#opacity').value=Math.round(c.panelOpacity*100);$('#content-opacity').value=Math.round(c.contentOpacity*100);$('#visibility').value=Math.round(c.imageVisibility*100);labels(); }
function labels(){ $('#content-opacity-value').textContent=$('#content-opacity').value+'%'; $('#opacity-value').textContent=$('#opacity').value+'%';$('#visibility-value').textContent=$('#visibility').value+'%'; }
function read(){return {backgroundUrl:validUrl($('#url').value),panelOpacity:$('#opacity').value/100,contentOpacity:$('#content-opacity').value/100,imageVisibility:$('#visibility').value/100};}
function preview(){ labels();try{apply(read());$('#status').textContent='预览中,点击“保存并应用”后记住设置。';}catch(e){$('#status').textContent=e.message;} }
function open(){fill(saved);$('#status').textContent='';if(!dialog.open)dialog.showModal();$('#url').focus();}
function cancel(){apply(saved);dialog.close();}
$('#open').addEventListener('click',open);
$('#cancel').addEventListener('click',cancel);
dialog.addEventListener('cancel',cancel);
for (const id of ['#opacity','#content-opacity','#visibility']) $(id).addEventListener('input',preview);
$('#url').addEventListener('change',preview);
$('#defaults').addEventListener('click',()=>{fill(DEFAULTS);preview();});
$('#save').addEventListener('click',()=>{
try {
const next=clean(read());
if(typeof GM_setValue==='function')GM_setValue(KEY,next);else localStorage.setItem(KEY,JSON.stringify(next));
saved=next;apply(saved);queue(document.body);dialog.close();
}catch(e){$('#status').textContent=e.message||'保存失败,请重试。';}
});
ui={open};
}
function start(){mountUi();queue(document.body);setTimeout(()=>queue(document.body),700);}
const observer=new MutationObserver(records=>{
for(const r of records){
if(r.target===style || r.target.getRootNode()!==document)continue;
if(r.type==='childList'){
if(r.target.tagName==='STYLE') { queue(document.body); continue; }
for(const n of r.addedNodes)if(n.nodeType===1){
if(n.tagName==='STYLE'||n.tagName==='LINK')queue(document.body);else queue(n);
}
}
else if(r.target===ROOT || r.target===document.body){forceDark();}
else queue(r.target);
}
if(document.body&&!document.getElementById('lgt-settings-host'))mountUi();
});
observer.observe(ROOT,{subtree:true,childList:true,attributes:true,attributeFilter:['style','class','data-lcolor-theme']});
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',start,{once:true});else start();
document.addEventListener('load',e=>{if(e.target.tagName==='LINK')queue(document.body);},true);
window.addEventListener('load',()=>queue(document.body),{once:true});
document.addEventListener('fullscreenchange',()=>queue(document.body));
window.addEventListener('resize',()=>queue(document.body));
window.addEventListener('popstate',()=>queue(document.body));
window.addEventListener('hashchange',()=>queue(document.body));
window.addEventListener('pageshow',()=>{forceDark();queue(document.body);});
if(typeof GM_registerMenuCommand==='function')GM_registerMenuCommand('背景 / 透明度设置',()=>{mountUi();ui?.open();});
})();剪贴板
剪贴板 dzbpp3h3
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新