跳到正文

科技·工程

洛谷高斯模糊美化

BaoHouxun约 18 分钟
原文

这是 2026年5月16日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月17日
收录
2026年5月17日
更新
2026年5月22日
篇幅
约 18 分钟
获赞
2

版本

  1. v10当前
  2. v9
  3. v8
  4. v7
更早的 6 个版本
  1. v6
  2. v5
  3. v4
  4. v3
  5. v2
  6. v1

前言

个人一直比较喜欢模糊效果,尤其是在macOS和iOS中滑动页面顶栏也能模糊下方内容,于是和DeepSeek一起写了这个Tampermonkey脚本,能够给洛谷添加高斯模糊效果。

功能

本脚本做了如下美化:

  • 顶栏、侧边栏模糊效果
  • 部分弹出菜单模糊效果
  • 弹窗圆角美化
  • 弹窗背景高斯模糊效果

截图

顶栏及侧边栏模糊。 顶栏及侧边栏

标签选择框模糊。 标签选择

弹窗圆角及模糊。 弹窗

使用

现在浏览器中安装Tampermonkey。然后点击Tampermonkey扩展图标,在弹出的菜单中点击“添加新脚本”,在编辑页面中删掉原来的内容,将以下内容粘贴进去,然后按下Ctrl + S保存,刷新原有页面即可生效。

脚本
JavaScript
// ==UserScript==
// @name         洛谷高斯模糊美化
// @namespace    https://www.luogu.com.cn/user/1816684
// @version      1.0
// @description  (首个发布版)为洛谷顶栏、侧边栏、各类弹窗添加毛玻璃效果,修复toast提示遮罩闪现问题,庆祝弹窗保持透明。
// @match        *://www.luogu.com.cn/*
// @match        *://www.luogu.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 注入全局样式
    const style = document.createElement('style');
    style.textContent = `
        /* 顶栏毛玻璃效果 */
        .top-bar {
            background: rgba(255, 255, 255, 0.65) !important;
            backdrop-filter: blur(10px) !important;
            -webkit-backdrop-filter: blur(10px) !important;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08) !important;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05) !important;
        }

        /* 左侧导航侧边栏(普通状态) */
        .sidebar.lside {
            background: rgba(255, 255, 255, 0.7) !important;
            backdrop-filter: blur(10px) !important;
            -webkit-backdrop-filter: blur(10px) !important;
            border-right: 1px solid rgba(0, 0, 0, 0.06) !important;
        }

        /* 左侧导航侧边栏(强制折叠状态) */
        .lside.drawer {
            background: rgba(255, 255, 255, 0.7) !important;
            backdrop-filter: blur(10px) !important;
            -webkit-backdrop-filter: blur(10px) !important;
            border-right: 1px solid rgba(0, 0, 0, 0.06) !important;
        }

        /* 右侧用户侧边栏 */
        .user-nav.rside {
            background: rgba(255, 255, 255, 0.7) !important;
            backdrop-filter: blur(10px) !important;
            -webkit-backdrop-filter: blur(10px) !important;
            border-left: 1px solid rgba(0, 0, 0, 0.06) !important;
        }

        /* ===== 弹窗背景遮罩 - 默认透明,仅对非 toast 容器动态添加黑色背景 ===== */
        .swal2-container,
        .am-modal-overlay,
        .cs-dialog-container,
        .modal-backdrop,
        .v--modal-overlay,
        .el-overlay,
        [class*="backdrop"][style*="fixed"],
        [class*="overlay"][style*="fixed"] {
            backdrop-filter: none !important;
            -webkit-backdrop-filter: none !important;
        }

        /* 模态弹窗遮罩背景(黑色半透明)由 JS 动态添加,避免首次闪现 */

        /* ===== 弹窗本体(白色半透明毛玻璃)统一效果 ===== */
        .swal2-popup,
        .l-card.container.type-burger,
        .cover-upload,
        .dropdown,
        #lg-alert .am-modal-dialog,
        .cs-dialog {
            background: rgba(255, 255, 255, 0.85) !important;
            backdrop-filter: blur(12px) !important;
            -webkit-backdrop-filter: blur(12px) !important;
            border: 1px solid rgba(255, 255, 255, 0.2) !important;
            border-radius: 16px !important;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
        }

        /* 修复 SweetAlert2 图标内白色圆环错位 */
        .swal2-success-circular-line-left,
        .swal2-success-fix,
        .swal2-success-circular-line-right,
        .swal2-error-circular-line-left,
        .swal2-error-fix,
        .swal2-error-circular-line-right {
            background: transparent !important;
        }

        /* 所有弹窗内文字清晰度优化 */
        .l-card.container.type-burger *,
        .cover-upload *,
        .dropdown *,
        .swal2-popup *,
        #lg-alert .am-modal-dialog *,
        .cs-dialog *,
        .mp-dialog-header *,
        .mp-dialog-body * {
            text-shadow: 0 0 1px rgba(255, 255, 255, 0.6);
        }

        /* 针对某些弹窗内部的子元素背景透明 */
        .dropdown ul, .dropdown li,
        #lg-alert .am-modal-hd, #lg-alert .am-modal-bd, #lg-alert .am-modal-footer,
        .cs-dialog-header, .cs-dialog-item, .cs-dialog-submit-area, .cs-close-container {
            background: transparent !important;
        }

        /* 按钮样式微调(半透背景) */
        #lg-alert .am-modal-btn,
        .cs-dialog-button {
            background: rgba(255, 255, 255, 0.2) !important;
            border-radius: 8px !important;
            transition: all 0.2s ease !important;
        }
        #lg-alert .am-modal-btn:hover,
        .cs-dialog-button:hover {
            background: rgba(255, 255, 255, 0.4) !important;
        }

        /* 插入图片弹窗毛玻璃效果 */
        .mp-dialog-header,
        .mp-dialog-body {
            background: rgba(255, 255, 255, 0.85) !important;
            backdrop-filter: blur(12px) !important;
            -webkit-backdrop-filter: blur(12px) !important;
        }
        .mp-dialog-header:first-child {
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
        }
        .mp-dialog-body:last-child {
            border-bottom-left-radius: 8px;
            border-bottom-right-radius: 8px;
        }
        .mp-dialog-input input {
            background: rgba(255, 255, 255, 0.5) !important;
            border: 1px solid rgba(0, 0, 0, 0.1) !important;
        }
        .mp-dialog-button {
            background: rgba(255, 255, 255, 0.2) !important;
            border-radius: 4px !important;
        }
        .mp-dialog-button:hover {
            background: rgba(255, 255, 255, 0.4) !important;
        }

        /* ----- 专门处理“强制收缩左侧导航栏”下拉菜单 ----- */
        .dropdown:has(ul.setting-dropdown) {
            background: transparent !important;
            backdrop-filter: none !important;
            border: none !important;
            border-radius: 0 !important;
            box-shadow: none !important;
        }
        ul.setting-dropdown {
            background: rgba(255, 255, 255, 0.85) !important;
            backdrop-filter: blur(12px) !important;
            -webkit-backdrop-filter: blur(12px) !important;
            border: 1px solid rgba(255, 255, 255, 0.2) !important;
            border-radius: 12px !important;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1) !important;
            padding: 4px 0 !important;
        }
        ul.setting-dropdown * {
            text-shadow: 0 0 1px rgba(255, 255, 255, 0.6);
        }

        /* ===== 特殊规则:排除通过题目时的庆祝弹窗(保持透明,无圆角,无模糊)===== */
        .swal2-popup:has(.swal2-image[src*="ac-congrats"]) {
            background: transparent !important;
            backdrop-filter: none !important;
            -webkit-backdrop-filter: none !important;
            border: none !important;
            border-radius: 0 !important;
            box-shadow: none !important;
        }
        .swal2-popup:has(.swal2-image[src*="ac-congrats"]) * {
            text-shadow: none !important;
        }

        /* 滚动条视觉优化 */
        .sidebar.lside::-webkit-scrollbar,
        .user-nav.rside::-webkit-scrollbar,
        .lside.drawer::-webkit-scrollbar {
            width: 6px;
            background: rgba(0,0,0,0.05);
        }
        .sidebar.lside::-webkit-scrollbar-thumb,
        .user-nav.rside::-webkit-scrollbar-thumb,
        .lside.drawer::-webkit-scrollbar-thumb {
            background: rgba(0,0,0,0.2);
            border-radius: 3px;
        }

        /* 全局文字阴影优化 */
        .top-bar .left, .top-bar .right, .top-bar .breadcrumb, .top-bar .user-nav,
        .sidebar.lside, .lside.drawer, .user-nav.rside {
            text-shadow: 0 0 1px rgba(255, 255, 255, 0.5);
        }
    `;
    document.head.appendChild(style);

    // 动态修正 toast 容器的遮罩背景(解决首次闪现问题)
    function fixToastContainer(container) {
        if (!container) return;
        // 只处理包含 .swal2-toast 的容器
        if (container.querySelector('.swal2-toast')) {
            container.style.background = 'transparent';
            container.style.backdropFilter = 'none';
        } else {
            // 模态弹窗容器应用黑色半透明背景
            container.style.background = 'rgba(0, 0, 0, 0.4)';
            container.style.backdropFilter = 'none';
        }
    }

    // 监听新添加的 .swal2-container 元素
    const observer = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (node.nodeType === 1 && node.classList && node.classList.contains('swal2-container')) {
                    fixToastContainer(node);
                }
                // 也可能添加到子节点中
                if (node.nodeType === 1 && node.querySelectorAll) {
                    node.querySelectorAll('.swal2-container').forEach(fixToastContainer);
                }
            }
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // 页面加载后处理已存在的容器
    document.querySelectorAll('.swal2-container').forEach(fixToastContainer);
})();

注意事项

由于使用了 backdrop-filter 和 :has() 选择器等较新的CSS特性。建议使用最新版浏览器。如果通过题目弹窗也有模糊,则说明你的浏览器不支持 :has(),不是脚本的问题。

结语

欢迎大家使用,如果有问题请在评论区反馈。

我和DeepSeek的对话