跳到正文

个人记录

banana~banana!!!BANANA

15675808884a约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年9月26日
收录
2026年9月26日
更新
2026年9月28日
篇幅
约 17 分钟

版本

  1. v1当前
C++
// ==UserScript==
// @name         洛谷万物香蕉化
// @namespace    luogu-banana-world
// @version      1.0
// @description  除文字和题目图片外,把洛谷页面里的按钮、图标、头像、背景等尽量变成香蕉风格
// @match        https://www.luogu.com.cn/*
// @match        https://www.luogu.org/*
// @match        https://www.luogu.com/*
// @grant        GM_addStyle
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const BANANA = '🍌';

    // 题目相关区域选择器:这些区域里的图片不替换
    const PROBLEM_SELECTORS = [
        '.problem-content',
        '.lfe-body',
        '.markdown-body',
        '.sample',
        '.sample-test',
        '.input-output',
        '[class*="problem"]',
        '[class*="question"]',
        '[class*="statement"]',
        '[class*="description"]',
        '[class*="sample"]'
    ].join(',');

    // 判断某个图片是否在题目区域内
    function isProblemImage(img) {
        return !!img.closest(PROBLEM_SELECTORS);
    }

    // 判断元素是否像“图标 / 装饰图 / 头像 / 小图”
    function looksLikeVisualIcon(img) {
        const w = img.naturalWidth || img.width || img.offsetWidth;
        const h = img.naturalHeight || img.height || img.offsetHeight;

        if (w <= 0 || h <= 0) return false;

        // 小尺寸更像图标
        if (w <= 96 && h <= 96) return true;

        const src = (img.src || '').toLowerCase();
        const alt = (img.alt || '').toLowerCase();
        const cls = (typeof img.className === 'string') ? img.className.toLowerCase() : '';

        if (/icon|logo|avatar|badge|emoji|decoration|ui/.test(cls + ' ' + src + ' ' + alt)) {
            return true;
        }

        return false;
    }

    // 替换普通图片为香蕉
    function replaceImage(img) {
        if (img.dataset.bananaReplaced === '1') return;

        // 题目图片保留
        if (isProblemImage(img)) return;

        // 非图标类大图也尽量保留,避免破坏题面、博客图、用户上传图片
        if (!looksLikeVisualIcon(img)) return;

        const size = Math.max(img.offsetHeight, img.offsetWidth, 16);

        const span = document.createElement('span');
        span.textContent = BANANA;
        span.dataset.bananaReplaced = '1';
        span.style.display = 'inline-flex';
        span.style.alignItems = 'center';
        span.style.justifyContent = 'center';
        span.style.fontSize = `${Math.min(size, 64)}px`;
        span.style.lineHeight = '1';
        span.style.verticalAlign = 'middle';

        img.dataset.bananaReplaced = '1';
        img.replaceWith(span);
    }

    // 处理背景图:题目区域里的背景图不替换
    function replaceBackgroundImage(el) {
        if (el.dataset.bananaBgReplaced === '1') return;

        if (el.closest(PROBLEM_SELECTORS)) return;

        const style = window.getComputedStyle(el);
        const bg = style.backgroundImage || '';

        if (!bg || bg === 'none') return;
        if (!/url\(/i.test(bg)) return;

        const w = el.offsetWidth;
        const h = el.offsetHeight;

        // 只处理较小的背景装饰
        if (w <= 0 || h <= 0 || w > 128 || h > 128) return;

        el.style.backgroundImage = 'none';
        el.dataset.bananaBgReplaced = '1';

        // 如果里面没有文字内容,再放香蕉
        const hasText = el.textContent && el.textContent.trim().length > 0;
        if (!hasText) {
            el.textContent = BANANA;
            el.style.display = 'inline-flex';
            el.style.alignItems = 'center';
            el.style.justifyContent = 'center';
            el.style.fontSize = `${Math.min(Math.max(w, h), 64)}px`;
        }
    }

    // 扫描页面
    function scan(root) {
        if (!root || !root.querySelectorAll) return;

        const imgs = root.querySelectorAll('img');
        imgs.forEach(replaceImage);

        const all = root.querySelectorAll('*');
        all.forEach(replaceBackgroundImage);
    }

    // 初始扫描
    scan(document.body);

    // 监听动态变化
    const observer = new MutationObserver(mutations => {
        for (const m of mutations) {
            m.addedNodes.forEach(node => {
                if (node.nodeType === 1) {
                    scan(node);
                }
            });
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 样式:香蕉黄 + 香蕉绿 + 香蕉按钮
    GM_addStyle(`
        html, body {
            background:
                radial-gradient(circle at 20% 20%, rgba(255, 245, 157, 0.9), transparent 45%),
                radial-gradient(circle at 80% 30%, rgba(255, 235, 59, 0.85), transparent 50%),
                linear-gradient(135deg, #fff59d 0%, #ffee58 35%, #fdd835 75%, #fbc02d 100%) !important;
            background-attachment: fixed !important;
            color: #3e2723 !important;
        }

        main, .main-container, .container, #app, .content, .lg-content,
        .card, .panel, .item, .lg-card, section,
        header, nav, .navbar, .side-bar, .sidebar, .lg-sidebar,
        .menu, .dropdown, .list-group, .tab-content, .modal-content,
        .footer, footer, aside {
            background: linear-gradient(135deg, #a5d6a7 0%, #81c784 45%, #66bb6a 100%) !important;
            color: #1b3a1b !important;
            border: 1px solid #4caf50 !important;
            border-radius: 12px !important;
            box-shadow: 0 4px 14px rgba(76, 175, 80, 0.25) !important;
        }

        a {
            color: #2e7d32 !important;
        }

        a:hover {
            color: #1b5e20 !important;
            text-decoration: underline !important;
        }

        body::before {
            content: "🍌 🟡 🍌 🟢 🍌";
            position: fixed;
            top: 12px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 22px;
            letter-spacing: 10px;
            opacity: 0.9;
            pointer-events: none;
            z-index: 9999;
            text-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
        }

        .banana-button {
            background: linear-gradient(135deg, #fff176 0%, #ffee58 45%, #fdd835 100%) !important;
            color: #3e2723 !important;
            border: 2px solid #f9a825 !important;
            border-radius: 999px !important;
            padding: 6px 18px !important;
            box-shadow:
                inset 0 0 0 1px rgba(255, 255, 255, 0.45),
                0 2px 6px rgba(249, 168, 37, 0.45) !important;
            font-weight: 600 !important;
            cursor: pointer !important;
            transition: transform 0.15s ease, box-shadow 0.15s ease !important;
        }

        .banana-button:hover {
            transform: translateY(-1px) !important;
            box-shadow:
                inset 0 0 0 1px rgba(255, 255, 255, 0.55),
                0 4px 10px rgba(249, 168, 37, 0.55) !important;
        }

        .banana-button:active {
            transform: translateY(0) !important;
        }

        .banana-button::before {
            content: "🍌 ";
            margin-right: 4px;
        }
    `);

    // 按钮香蕉化
    function isButton(el) {
        if (!(el instanceof HTMLElement)) return false;

        const tag = el.tagName.toLowerCase();

        if (tag === 'button') return true;

        if (tag === 'input') {
            const type = (el.type || '').toLowerCase();
            return ['button', 'submit', 'reset'].includes(type);
        }

        if (el.getAttribute('role') === 'button') return true;

        const cls = (typeof el.className === 'string') ? el.className : '';
        if (/\b(btn|button|lbtn|am-btn|el-button|submit)/i.test(cls)) {
            return true;
        }

        return false;
    }

    function applyBananaButton(el) {
        if (el.dataset.bananaButton === '1') return;

        el.classList.add('banana-button');
        el.dataset.bananaButton = '1';
    }

    function scanButtons(root) {
        if (!root || !root.querySelectorAll) return;

        const candidates = root.querySelectorAll(
            'button, input[type="button"], input[type="submit"], input[type="reset"], [role="button"], a, span, div'
        );

        candidates.forEach(el => {
            if (isButton(el)) {
                applyBananaButton(el);
            }
        });
    }

    scanButtons(document.body);

    const buttonObserver = new MutationObserver(mutations => {
        for (const m of mutations) {
            m.addedNodes.forEach(node => {
                if (node.nodeType === 1) {
                    scanButtons(node);
                }
            });
        }
    });

    buttonObserver.observe(document.body, {
        childList: true,
        subtree: true
    });
})();
原文