跳到正文

闲话

洛谷美化油猴插件

DanseSolo约 247 分钟
原文

这是 2026年9月12日 存档的第 6 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年4月18日
收录
2026年4月21日
更新
2026年11月25日
篇幅
约 247 分钟

版本

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

隐藏题号 & 难度。

INFO
JavaScript
// ==UserScript==
// @name         Luogu 题号+难度 整合工具
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  题号切换+难度隐藏 整合版,主页面隐藏难度按钮,题库页不干扰
// @author       LTTXiaochuan
// @match        https://www.luogu.com.cn/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // ====================== 工具函数:页面判断 ======================
    const path = location.pathname;
    const searchParams = new URLSearchParams(location.search);

    // 1. 仅匹配:/problem/Pxxxx 或 /contest/xxx/problem/xxx,排除 /problem/list 等题库页
    const isProblemDetailPage =
        (path.startsWith('/problem/') && !path.includes('/list')) ||
        (path.includes('/contest/') && path.includes('/problem/'));

    // 2. 判断是否为洛谷主页面(根路径 / 或 /?xxx)
    const isHomePage = path === '/' && searchParams.get('pid') === null;

    // ====================== 第一部分:题号切换功能(仅题目详情页执行) ======================
    if (isProblemDetailPage) {
        // 默认隐藏
        let mode = GM_getValue('pid_mode', 'hidden');

        // 核心样式
        const css1 = `
        #pidBtn {
            position: fixed;
            top: 12px;
            right: 12px;
            z-index: 999999;
            background: rgba(0,0,0,0.6);
            color: #fff !important;
            padding: 6px 8px;
            font-size: 12px;
            border-radius: 6px;
            cursor: move;
            user-select: none;
            box-shadow: 0 2px 6px rgba(0,0,0,.4);
            min-width: 120px;
            text-align: center;
            font-family: 'Segoe UI',微软雅黑,sans-serif;
        }
        #pidBtn small { opacity: .8; display:block; font-size:11px; margin-top:2px; color: #fff !important; }
        /* 隐藏题号 */
        body.hidePid .breadcrumb a[href*="P"] { display:none !important; }
        /* 打印只隐藏按钮,不破坏页面 */
        @media print { #pidBtn { display:none !important; } }
        `;
        const style1 = document.createElement('style');
        style1.textContent = css1;
        document.head.appendChild(style1);

        // 执行功能 + 侧栏暴力隐藏
        function run() {
            document.body.classList.toggle('hidePid', mode === 'hidden');

            if(mode === 'hidden'){
                const all = document.getElementsByTagName('*');
                for(let el of all){
                    if(el.textContent.trim() === '题目编号'){
                        const val = el.nextElementSibling || el.parentElement.lastElementChild;
                        if(val && val.textContent.startsWith('P')) val.style.display = 'none';
                    }
                }
            }else{
                const all = document.getElementsByTagName('*');
                for(let el of all){
                    if(el.textContent.trim() === '题目编号'){
                        const val = el.nextElementSibling || el.parentElement.lastElementChild;
                        if(val) val.style.display = '';
                    }
                }
            }

            // 主标题原版逻辑
            const h1 = document.querySelector('h1');
            if(h1){
                if(mode === 'hidden'){ h1._o = h1._o || h1.textContent; h1.textContent = 'LoveFurina'; }
                else { h1._o && (h1.textContent = h1._o); }
            }
            // 文字修改为:隐藏题号/显示题号 + (点击切换)
            const label = mode === 'hidden' ? '隐藏题号' : '显示题号';
            document.getElementById('pidBtn').innerHTML = `<strong>${label}</strong><small>(点击切换)</small>`;
        }

        // 创建按钮
        function btn() {
            const b = document.createElement('div');
            b.id = 'pidBtn';
            const x=GM_getValue('x'), y=GM_getValue('y');
            if(x!=null&&y!=null){
                b.style.left=x+'px';
                b.style.top=y+'px';
                b.style.right='auto';
            }else{
                b.style.bottom = "65px";
                b.style.right = "12px";
                b.style.top = "auto";
            }

            let ox,oy,d=false;
            let isDragged = false;

            b.onmousedown = (e)=>{
                d=true;
                isDragged = false;
                ox=e.clientX-b.offsetLeft;
                oy=e.clientY-b.offsetTop;
                b.style.opacity=0.6;
            };

            document.onmousemove = (e)=>{
                if(!d)return;
                isDragged = true;
                b.style.left=Math.max(0,Math.min(innerWidth-b.offsetWidth,e.clientX-ox))+'px';
                b.style.top=Math.max(0,Math.min(innerHeight-b.offsetHeight,e.clientY-oy))+'px';
            };

            document.onmouseup = ()=>{
                if(!d)return;
                d=false;
                b.style.opacity=1;
                GM_setValue('x',b.offsetLeft);
                GM_setValue('y',b.offsetTop);
            };

            b.onclick = ()=>{
                if(!isDragged){
                    mode=mode==='hidden'?'show':'hidden';
                    GM_setValue('pid_mode',mode);
                    run();
                }
            };

            document.body.appendChild(b);
            run();
        }

        // 标签页标题(仅题目页执行)
        const targetTitle = "芙芙超可爱~";
        function setFurinaTitle() {
            if (document.title !== targetTitle) {
                document.title = targetTitle;
            }
        }
        setInterval(setFurinaTitle, 300);

        // 极速响应:轮询间隔50ms(仅题目页执行)
        setInterval(() => {
            const h1 = document.querySelector('h1');
            if(h1){
                if(mode === 'hidden'){
                    h1._o = h1._o || h1.textContent;
                    h1.textContent = 'LoveFurina';
                } else {
                    h1._o && (h1.textContent = h1._o);
                }
            }
        }, 50);

        // 极速响应:题号隐藏(仅题目页执行)
        setInterval(() => {
            if(mode === 'hidden'){
                const all = document.getElementsByTagName('*');
                for(let el of all){
                    if(el.textContent.trim() === '题目编号'){
                        const val = el.nextElementSibling || el.parentElement.lastElementChild;
                        if(val && val.textContent.startsWith('P')) val.style.display = 'none';
                    }
                }
            }
        }, 50);

        // 启动题号功能(仅题目页执行)
        window.addEventListener('load', ()=>{ btn(); setTimeout(run,300); setTimeout(run,800); });
    }

    // ====================== 第二部分:难度隐藏功能(主页面除外,完全不变) ======================
    if (!isHomePage) { // 主页面不执行难度功能
        const DEFAULT_MODE = GM_getValue('lg_diff_mode', 'hover');
        const selectors = [
            '.l-flex-info-row a[href*="problem/list?difficulty"] span',
            '.difficulty span span',
            '.difficulty span.lfe-caption'
        ];

        const styleId = 'tm-luogu-diff-style';
        const css2 = `
        .tm-diff-hidden, .tm-diff-hover { transition: opacity .15s ease !important; }
        .tm-diff-hidden { opacity: 0 !important; }
        .tm-diff-hover { opacity: 0 !important; }
        .tm-diff-hover:hover { opacity: 1 !important; }
        #tm-luogu-diff-toggle {
            position: fixed;
            bottom: 12px;
            right: 12px;
            z-index: 999999;
            background: rgba(0,0,0,0.6);
            color: #fff !important;
            padding: 6px 8px;
            font-size: 12px;
            border-radius: 6px;
            cursor: move;
            user-select: none;
            box-shadow: 0 2px 6px rgba(0,0,0,.4);
        }
        #tm-luogu-diff-toggle small { opacity: .8; display:block; font-size:11px; margin-top:2px; color: #fff !important; }
        `;

        function injectStyle() {
            if (document.getElementById(styleId)) return;
            const s = document.createElement('style');
            s.id = styleId;
            s.textContent = css2;
            document.head.appendChild(s);
        }

        let currentMode = DEFAULT_MODE;
        function applyToElement(el, mode) {
            el.classList.remove('tm-diff-hidden', 'tm-diff-hover');
            if (mode === 'hidden') el.classList.add('tm-diff-hidden');
            else if (mode === 'hover') el.classList.add('tm-diff-hover');
        }

        function applyModeToAll(mode) {
            const combined = selectors.join(',');
            document.querySelectorAll(combined).forEach(el => applyToElement(el, mode));
        }

        let mo = null;
        function startObserver() {
            if (mo) return;
            mo = new MutationObserver(muts => {
                if (muts.some(m => m.addedNodes && m.addedNodes.length)) {
                    applyModeToAll(currentMode);
                }
            });
            mo.observe(document.body, { childList: true, subtree: true });
        }

        function saveMode(mode) {
            currentMode = mode;
            GM_setValue('lg_diff_mode', mode);
            updateToggleText();
            applyModeToAll(mode);
        }

        function nextMode(m) {
            if (m === 'show') return 'hover';
            if (m === 'hover') return 'hidden';
            return 'show';
        }

        function createToggle() {
            if (document.getElementById('tm-luogu-diff-toggle')) return;
            const btn = document.createElement('div');
            btn.id = 'tm-luogu-diff-toggle';
            btn.title = '点击切换难度显示模式';
            btn.style.minWidth = '120px';
            btn.style.textAlign = 'center';
            btn.style.fontFamily = 'Segoe UI,微软雅黑,sans-serif';

            const savedX = GM_getValue('lg_diff_x', null);
            const savedY = GM_getValue('lg_diff_y', null);
            if (savedX !== null && savedY !== null) {
                btn.style.left = savedX + 'px';
                btn.style.top = savedY + 'px';
                btn.style.right = 'auto';
            }

            btn.addEventListener('click', (e) => {
                if (btn._dragging) return;
                saveMode(nextMode(currentMode));
            });

            makeDraggable(btn);
            document.body.appendChild(btn);
            updateToggleText();
        }

        function updateToggleText() {
            const btn = document.getElementById('tm-luogu-diff-toggle');
            if (!btn) return;
            const label = (currentMode === 'show') ? '显示难度' : (currentMode === 'hover') ? '悬停显示难度' : '隐藏难度';
            btn.innerHTML = ` <strong> ${label} </strong> <small> (点击切换) </small> `;
        }

        // 拖动逻辑
        function makeDraggable(el) {
            let offsetX, offsetY, isDown = false;

            el.addEventListener('mousedown', (e) => {
                isDown = true;
                el._dragging = false;
                offsetX = e.clientX - el.offsetLeft;
                offsetY = e.clientY - el.offsetTop;
                el.style.opacity = "0.6";
                document.addEventListener('mousemove', move);
                document.addEventListener('mouseup', up);
            });

            function move(e) {
                if (!isDown) return;
                el._dragging = true;
                let newLeft = e.clientX - offsetX;
                let newTop = e.clientY - offsetY;

                const maxLeft = window.innerWidth - el.offsetWidth;
                const maxTop = window.innerHeight - el.offsetHeight;
                newLeft = Math.max(0, Math.min(maxLeft, newLeft));
                newTop = Math.max(0, Math.min(maxTop, newTop));

                el.style.left = newLeft + 'px';
                el.style.top = newTop + 'px';
                el.style.right = 'auto';
            }

            function up() {
                if (!isDown) return;
                isDown = false;
                el.style.opacity = "1";
                document.removeEventListener('mousemove', move);
                document.removeEventListener('mouseup', up);
                GM_setValue('lg_diff_x', el.offsetLeft);
                GM_setValue('lg_diff_y', el.offsetTop);
                setTimeout(() => { el._dragging = false; }, 50);
            }
        }

        function init() {
            injectStyle();
            createToggle();
            applyModeToAll(currentMode);
            startObserver();
            window.addEventListener('load', () => setTimeout(() => applyModeToAll(currentMode), 300));
        }

        // 启动难度功能
        init();
    }
})();

预览题目显示难度 & 标签。

INFO
JavaScript
// ==UserScript==
// @name         洛谷题目难度着色+标签速览
// @namespace    https://www.luogu.com.cn/
// @version      2.2
// @description  自动着色洛谷题目编号,悬停显示标签,双击跳转题目;
// @author       Assistant
// @match        https://www.luogu.com.cn/*
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_openInTab
// @connect      cdn.luogu.com.cn
// @require      https://cdn.jsdelivr.net/npm/pako@2.1.0/dist/pako.min.js
// ==/UserScript==

(function() {
    'use strict';

    // 难度颜色映射
    const DIFFICULTY_COLORS = {
        0: '#808080', // 灰色
        1: '#e74c3c', // 红色
        2: '#e67e22', // 橙色
        3: '#f1c40f', // 黄色
        4: '#2ecc71', // 绿色
        5: '#00bcd4', // 青色
        6: '#2196F3', // 蓝色
        7: '#9c3dcf', // 紫色
        8: '#000000'  // 黑色
    };

    const DATA_URL = 'https://cdn.luogu.com.cn/problemset-open/latest.ndjson.gz';
    const DB_NAME = 'LuoguProblemDB';
    const DB_VERSION = 1;
    const STORE_NAME = 'problems';
    const MIN_UPDATE_INTERVAL = 3 * 24 * 60 * 60 * 1000; // 3天(毫秒)

    // 题目编号正则,支持 P, B, CF, SP, AT, UVA 等
    const PID_PATTERN = /\b(?:P|B|CF|SP|AT(?:_[A-Za-z0-9]+)?|UVA)\d+[A-Za-z0-9]*\b/g;

    let problemDataMap = new Map();   // pid -> { difficulty, title, tags, type }
    let processedTextNodes = new WeakSet();
    let processedLinks = new WeakSet();
    let observer = null;
    let tooltip = null;

    // ---------- 提示框 ----------
    function createTooltip() {
        if (tooltip) return;
        tooltip = document.createElement('div');
        tooltip.id = 'luogu-problem-tooltip';
        tooltip.style.cssText = `
            position: fixed;
            z-index: 9999999;
            background: rgba(30, 30, 30, 0.95);
            color: #fff;
            padding: 8px 12px;
            border-radius: 6px;
            font-size: 12px;
            max-width: 300px;
            word-wrap: break-word;
            pointer-events: none;
            display: none;
            box-shadow: 0 2px 8px rgba(0,0,0,0.3);
        `;
        document.body.appendChild(tooltip);
    }

    function showTooltip(x, y, content) {
        createTooltip();
        tooltip.innerHTML = content;
        tooltip.style.display = 'block';
        const rect = tooltip.getBoundingClientRect();
        let left = x + 10;
        let top = y + 10;
        if (left + rect.width > window.innerWidth - 5) left = x - rect.width - 10;
        if (top + rect.height > window.innerHeight - 5) top = y - rect.height - 10;
        tooltip.style.left = left + 'px';
        tooltip.style.top = top + 'px';
    }

    function hideTooltip() {
        if (tooltip) tooltip.style.display = 'none';
    }

    // ---------- 状态提示 ----------
    function showStatusMessage(text, isError = false) {
        const existing = document.getElementById('luogu-problem-status');
        if (existing) existing.remove();
        const div = document.createElement('div');
        div.id = 'luogu-problem-status';
        div.style.cssText = `
            position: fixed; top: 20px; right: 20px;
            background: ${isError ? '#f44336' : '#2196F3'};
            color: white; padding: 8px 16px;
            border-radius: 6px; z-index: 999999;
            font-size: 14px; box-shadow: 0 2px 8px rgba(0,0,0,0.2);
            max-width: 300px; word-wrap: break-word;
        `;
        div.textContent = text;
        document.body.appendChild(div);
        if (!isError) {
            setTimeout(() => { if (div.parentNode) div.remove(); }, 5000);
        }
    }

    // ---------- IndexedDB 操作 ----------
    function openDB() {
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(DB_NAME, DB_VERSION);
            request.onupgradeneeded = (e) => {
                const db = e.target.result;
                if (!db.objectStoreNames.contains(STORE_NAME)) {
                    db.createObjectStore(STORE_NAME, { keyPath: 'pid' });
                }
            };
            request.onsuccess = () => resolve(request.result);
            request.onerror = () => reject(request.error);
        });
    }

    async function getProblemCount() {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(STORE_NAME, 'readonly');
            const store = tx.objectStore(STORE_NAME);
            const req = store.count();
            req.onsuccess = () => resolve(req.result);
            req.onerror = () => reject(req.error);
        });
    }

    async function clearProblems() {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(STORE_NAME, 'readwrite');
            const store = tx.objectStore(STORE_NAME);
            const req = store.clear();
            req.onsuccess = () => resolve();
            req.onerror = () => reject(req.error);
        });
    }

    async function bulkInsert(problems) {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(STORE_NAME, 'readwrite');
            const store = tx.objectStore(STORE_NAME);
            for (const p of problems) {
                store.put(p);
            }
            tx.oncomplete = () => resolve();
            tx.onerror = () => reject(tx.error);
        });
    }

    async function loadProblemDataMap() {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(STORE_NAME, 'readonly');
            const store = tx.objectStore(STORE_NAME);
            const req = store.getAll();
            req.onsuccess = () => {
                const map = new Map();
                req.result.forEach(item => {
                    map.set(item.pid, {
                        difficulty: item.difficulty,
                        title: item.title || '',
                        tags: item.tags || [],
                        type: item.type || ''
                    });
                });
                resolve(map);
            };
            req.onerror = () => reject(req.error);
        });
    }

    // ---------- 下载题库数据 ----------
    async function downloadAndStoreData(onProgress) {
        if (typeof pako === 'undefined') {
            throw new Error('pako 库未加载,请刷新页面重试。');
        }
        await clearProblems();
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: DATA_URL,
                responseType: 'arraybuffer',
                onload: async (res) => {
                    try {
                        const compressed = new Uint8Array(res.response);
                        const decompressed = pako.ungzip(compressed, { to: 'string' });
                        const lines = decompressed.split('\n').filter(line => line.trim() !== '');
                        const total = lines.length;
                        const batchSize = 1000;
                        let imported = 0;

                        const importBatch = async (start) => {
                            const end = Math.min(start + batchSize, total);
                            const batch = [];
                            for (let i = start; i < end; i++) {
                                try {
                                    const obj = JSON.parse(lines[i]);
                                    batch.push({
                                        pid: obj.pid,
                                        title: obj.title,
                                        difficulty: obj.difficulty,
                                        type: obj.type,
                                        tags: obj.tags || []
                                    });
                                } catch (e) {}
                            }
                            if (batch.length > 0) {
                                await bulkInsert(batch);
                                imported += batch.length;
                                if (onProgress) onProgress(imported, total);
                            }
                            if (end < total) {
                                await importBatch(end);
                            } else {
                                resolve();
                            }
                        };

                        await importBatch(0);
                    } catch (e) {
                        reject(e);
                    }
                },
                onerror: (err) => reject(err)
            });
        });
    }

    // ---------- 工具函数:打开题目页面(新增) ----------
    function openProblemPage(pid) {
        const url = `https://www.luogu.com.cn/problem/${pid}`;
        if (typeof GM_openInTab === 'function') {
            GM_openInTab(url, { active: true, insert: true });
        } else {
            window.open(url, '_blank');
        }
    }

    // ---------- DOM 着色与事件绑定 ----------
    function colorizeNode(node) {
        if (!node) return;
        if (node.nodeType === Node.TEXT_NODE) {
            processTextNode(node);
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            if (node.tagName === 'SCRIPT' || node.tagName === 'STYLE' || node.tagName === 'NOSCRIPT') return;
            if (node.classList && node.classList.contains('lg-diff-color')) return;

            // 处理指向题目的链接
            if (node.tagName === 'A' && node.getAttribute('href')?.includes('/problem/')) {
                processProblemLink(node);
                return;
            }

            const childNodes = node.childNodes;
            for (let i = 0; i < childNodes.length; i++) {
                colorizeNode(childNodes[i]);
            }
        }
    }

    function processTextNode(node) {
        if (processedTextNodes.has(node)) return;
        const text = node.textContent;
        if (!text) return;

        PID_PATTERN.lastIndex = 0;
        const replacements = [];
        let match;
        while ((match = PID_PATTERN.exec(text)) !== null) {
            const pid = match[0];
            const problemData = problemDataMap.get(pid);
            if (problemData) {
                replacements.push({
                    start: match.index,
                    end: PID_PATTERN.lastIndex,
                    pid: pid,
                    difficulty: problemData.difficulty,
                    tags: problemData.tags,
                    title: problemData.title
                });
            }
        }

        if (replacements.length === 0) {
            processedTextNodes.add(node);
            return;
        }

        const fragment = document.createDocumentFragment();
        let lastIndex = 0;
        for (const r of replacements) {
            if (r.start > lastIndex) {
                fragment.appendChild(document.createTextNode(text.substring(lastIndex, r.start)));
            }
            const span = document.createElement('span');
            span.className = 'lg-diff-color';
            span.style.color = DIFFICULTY_COLORS[r.difficulty] || '#808080';
            span.style.fontWeight = 'bold';
            span.textContent = r.pid;
            span.style.cursor = 'pointer';

            span.addEventListener('mouseenter', (e) => {
                const tags = r.tags && r.tags.length ? r.tags.join(', ') : '暂无标签';
                const title = r.title ? `<strong>${r.pid} ${r.title}</strong>` : `<strong>${r.pid}</strong>`;
                showTooltip(e.clientX, e.clientY, `${title}<br>标签:${tags}`);
            });
            span.addEventListener('mousemove', (e) => {
                if (tooltip && tooltip.style.display === 'block') {
                    showTooltip(e.clientX, e.clientY, tooltip.innerHTML);
                }
            });
            span.addEventListener('mouseleave', hideTooltip);

            // 双击打开题目页面(使用新函数)
            span.addEventListener('dblclick', (e) => {
                e.preventDefault();
                e.stopImmediatePropagation();
                openProblemPage(r.pid);
            });

            fragment.appendChild(span);
            lastIndex = r.end;
        }
        if (lastIndex < text.length) {
            fragment.appendChild(document.createTextNode(text.substring(lastIndex)));
        }

        node.parentNode.replaceChild(fragment, node);
        processedTextNodes.add(node);
    }

    function processProblemLink(a) {
        if (processedLinks.has(a)) return;
        const href = a.getAttribute('href');
        const match = href && href.match(/\/problem\/([^/?]+)/);
        if (!match) return;
        const pid = decodeURIComponent(match[1]);
        const data = problemDataMap.get(pid);

        processedLinks.add(a);

        if (data) {
            const color = DIFFICULTY_COLORS[data.difficulty] || '#808080';
            a.style.setProperty('color', color, 'important');
            a.style.fontWeight = 'bold';
        }
        a.style.cursor = 'pointer';

        // 阻止单击默认跳转,让双击事件能够生效
        a.addEventListener('click', (e) => {
            e.preventDefault();
        });

        a.addEventListener('mouseenter', (e) => {
            const tags = data && data.tags && data.tags.length ? data.tags.join(', ') : '暂无标签';
            const title = data && data.title ? `<strong>${pid} ${data.title}</strong>` : `<strong>${pid}</strong>`;
            showTooltip(e.clientX, e.clientY, `${title}<br>标签:${tags}`);
        });
        a.addEventListener('mousemove', (e) => {
            if (tooltip && tooltip.style.display === 'block') {
                showTooltip(e.clientX, e.clientY, tooltip.innerHTML);
            }
        });
        a.addEventListener('mouseleave', hideTooltip);

        // 双击打开新页面(使用新函数)
        a.addEventListener('dblclick', (e) => {
            e.preventDefault();
            e.stopImmediatePropagation();
            openProblemPage(pid);
        });
    }

    function colorizeExistingDOM() {
        colorizeNode(document.body);
    }

    function startObserver() {
        if (observer) observer.disconnect();
        observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    colorizeNode(node);
                });
            });
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    // ---------- 初始化 ----------
    async function init() {
        try {
            const lastUpdate = GM_getValue('last_update_time', 0);
            const now = Date.now();

            let count = await getProblemCount();

            if (count === 0) {
                showStatusMessage('正在下载洛谷题库数据,首次使用可能需要几十秒...');
                console.log('[洛谷题目着色] 无本地题库,开始下载...');
                await downloadAndStoreData((current, total) => {
                    if (current % 5000 === 0 || current === total) {
                        showStatusMessage(`题库下载中: ${current}/${total}`);
                    }
                });
                GM_setValue('last_update_time', Date.now());
                showStatusMessage('题库下载完成,正在着色...');
                console.log('[洛谷题目着色] 下载完成');
            } else if (now - lastUpdate >= MIN_UPDATE_INTERVAL) {
                showStatusMessage('题库数据已超过3天未更新,正在更新...');
                console.log('[洛谷题目着色] 超过3天,开始更新题库...');
                await downloadAndStoreData((current, total) => {
                    if (current % 5000 === 0 || current === total) {
                        showStatusMessage(`题库更新中: ${current}/${total}`);
                    }
                });
                GM_setValue('last_update_time', Date.now());
                showStatusMessage('题库更新完成,正在着色...');
            } else {
                console.log('[洛谷题目着色] 题库数据在3天内已更新过,使用本地缓存');
            }

            problemDataMap = await loadProblemDataMap();
            console.log(`[洛谷题目着色] 已加载 ${problemDataMap.size} 条题目数据`);
            colorizeExistingDOM();
            startObserver();
            showStatusMessage('题目难度着色与标签速览已启用', false);
        } catch (err) {
            console.error('[洛谷题目着色] 初始化失败:', err);
            showStatusMessage('题库下载失败,请检查网络后刷新重试', true);
            startObserver();
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();

预测等级分(貌似停工?)

INFO
JavaScript
// ==UserScript==
// @name         洛谷等级分预测
// @namespace    https://www.luogu.com.cn/user/1523280
// @version      3.1
// @description  预测你的等级分曲线和比赛状况!
// @author       a_small_OIer & Sakument_tree
// @match        https://www.luogu.com.cn/contest/*
// @match        https://www.luogu.com.cn/user/*
// @grant        GM_xmlhttpRequest
// @connect      luogu.ac.cn
// @require      https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js
// @run-at       document-end
// ==/UserScript==
(function (){
  'use strict';
  /**
   * 通用 GM_xmlhttpRequest Promise 封装
   * @param {string} url - 请求地址
   * @returns {Promise<object>} 解析后的 JSON 对象
   */
  function gmRequest(url) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        method: 'GET',
        url: url,
        onload: function (resp) {
          try {
            resolve(JSON.parse(resp.responseText));
          } catch (e) {
            reject(e);
          }
        },
        onerror: reject
      });
    });
  }
  function isoToUnix(iso) {
    return new Date(iso).getTime() / 1000;
  }
  function formatTime(ts) {
    const d = new Date((ts + 8 * 3600) * 1000);
    const year = d.getUTCFullYear();
    const month = String(d.getUTCMonth() + 1).padStart(2, '0');
    const day = String(d.getUTCDate()).padStart(2, '0');
    const hours = String(d.getUTCHours()).padStart(2, '0');
    const minutes = String(d.getUTCMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  }
  const path = window.location.pathname;
  if (path.match(/^\/contest\/\d+/)) {
    (function () {
      const contestId = window.location.pathname.match(/\/contest\/(\d+)/)?.[1];
      if (!contestId) {
        console.warn('未找到比赛 ID');
        return;
      }
      // -------- 参数配置 --------
      const CACHE_KEY = 'luogu_contest_pred_cache';
      const PAGE_SIZE = 100;
      function getCacheTTL(mode) {
        return mode === 'official' ? 15 * 24 * 60 * 60 * 1000 : 2 * 60 * 60 * 1000;
      }
      let ratingMode = null;
      let dataMap = {};
      let isAllFetched = false;
      let isLoading = false;
      let initialized = false;
      let isUnrated = false;
      let initLock = false;
      let domCheckTimer = null;
      // -------- 缓存 --------
      function getCache() {
        try {
          const raw = localStorage.getItem(CACHE_KEY);
          return raw ? JSON.parse(raw) : {};
        } catch { return {}; }
      }
      function setCache(cache) {
        try {
          localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
        } catch { }
      }
      function getRowBackgroundColor(row) {
        const userDiv = row.querySelector('.user');
        if (userDiv) {
          const bg = userDiv.style.backgroundColor;
          if (bg) return bg;
        }
        return null;
      }
      function fetchContestPredictions(page = 1) {
        const url = `https://luogu.ac.cn/api/v1/contest/${contestId}?page=${page}&page_size=${PAGE_SIZE}`;
        return gmRequest(url);
      }
      function removePredictionUI() {
        const headerRow = document.querySelector('.header-wrap .header');
        if(headerRow){
          const oldHeader = headerRow.querySelector('.header-container[data-prediction-header]');
          if(oldHeader)
            oldHeader.remove();
        }
        const rows = document.querySelectorAll('.row-wrap .row');
        rows.forEach(row => {
          const cell = row.querySelector('.problem[data-prediction-cell]');
          if (cell) cell.remove();
        });
      }
      function removeFooter() {
        const footer = document.querySelector('#prediction-footer');
        if (footer) footer.remove();
      }
      let height = -1;
      function setCellFullHeight(cell, row) {
        if(!cell || !row)
          return;
        if(height === -1){
          height = row.clientHeight;
        }
        if(height > 0){
          cell.style.height = height + 'px';
          cell.style.minHeight = height + 'px';
          cell.style.boxSizing = 'border-box';
        }else{
          cell.style.height = '100%';
          cell.style.minHeight = '100%';
          cell.style.alignSelf = 'stretch';
        }
      }
      function fillCurrentRows() {
        if (!window.location.hash.includes('scoreboard')) return;
        if (isUnrated) {
          removePredictionUI();
          removeFooter();
          return;
        }
        if (fail === 2) {
          removePredictionUI();
          updateFooter();
          return;
        }
        const rows = document.querySelectorAll('.row-wrap .row');
        if (rows.length === 0) return;
        ensureHeader();
        rows.forEach(row => {
          let cell = row.querySelector('.problem[data-prediction-cell]');
          if (!cell) {
            cell = document.createElement('div');
            cell.className = 'problem';
            cell.dataset.predictionCell = 'true';
            cell.style.cssText = 'flex: 0 0 80px; display: flex; flex-direction: column; justify-content: center; align-items: center;';
            row.appendChild(cell);
          }
          setCellFullHeight(cell, row);
          const bgColor = getRowBackgroundColor(row);
          if (bgColor) {
            cell.style.backgroundColor = bgColor;
          } else {
            cell.style.backgroundColor = '';
          }
          const uid = getUidFromRow(row);
          if (!uid) {
            cell.innerHTML = '';
            const scoreDiv = document.createElement('div');
            scoreDiv.className = 'td-score';
            scoreDiv.textContent = '-';
            cell.appendChild(scoreDiv);
            const runtimeDiv = document.createElement('div');
            runtimeDiv.className = 'td-runtime';
            runtimeDiv.textContent = '';
            cell.appendChild(runtimeDiv);
            return;
          }
          const info = dataMap[uid];
          if (!info) {
            cell.innerHTML = '';
            const scoreDiv = document.createElement('div');
            scoreDiv.className = 'td-score';
            const placeholder = document.createElement('span');
            placeholder.textContent = '获取中...';
            placeholder.style.color = 'rgba(0,0,0,0.55)';
            placeholder.style.fontSize = '13px';
            scoreDiv.appendChild(placeholder);
            cell.appendChild(scoreDiv);
            const runtimeDiv = document.createElement('div');
            runtimeDiv.className = 'td-runtime';
            runtimeDiv.textContent = '\u00A0';
            cell.appendChild(runtimeDiv);
            return;
          }
          const delta = info.delta;
          const newRating = info.newRating;
          const warnings = info.warnings || [];
          cell.innerHTML = '';
          const scoreDiv = document.createElement('div');
          scoreDiv.className = 'td-score';
          const deltaSpan = document.createElement('span');
          deltaSpan.style.fontWeight = 'bold';
          if (delta === null || delta === undefined) {
            deltaSpan.textContent = '-';
            deltaSpan.style.color = 'rgba(0,0,0,0.55)';
          } else if (delta > 0) {
            deltaSpan.style.color = 'rgb(82, 196, 26)';
            deltaSpan.textContent = `+${delta}`;
          } else if (delta < 0) {
            deltaSpan.style.color = 'rgb(231, 76, 60)';
            deltaSpan.textContent = `${delta}`;
          } else {
            deltaSpan.style.color = 'rgba(0,0,0,0.55)';
            deltaSpan.textContent = '±0';
          }
          scoreDiv.appendChild(deltaSpan);
          if (warnings.length > 0) {
            const warnIcon = document.createElement('span');
            warnIcon.textContent = ' ⓘ';
            warnIcon.style.cursor = 'help';
            warnIcon.style.fontSize = '14px';
            warnIcon.style.color = 'rgba(0,0,0,0.55)';
            warnIcon.title = warnings.join('\n');
            scoreDiv.appendChild(warnIcon);
          }
          cell.appendChild(scoreDiv);
          const runtimeDiv = document.createElement('div');
          runtimeDiv.className = 'td-runtime';
          if (newRating !== null && newRating !== undefined) {
            runtimeDiv.textContent = `新: ${newRating}`;
            runtimeDiv.style.color = 'rgba(0,0,0,0.55)';
            runtimeDiv.style.fontSize = '12px';
          } else {
            runtimeDiv.textContent = '\u00A0';
          }
          cell.appendChild(runtimeDiv);
        });
        updateFooter();
      }
      function getUidFromRow(row) {
        const link = row.querySelector('.user a[href^="/user/"]');
        if (link) {
          const match = link.href.match(/\/user\/(\d+)/);
          return match ? match[1] : null;
        }
        return null;
      }
      let fail = 0;
      async function loadData() {
        if (isLoading || isUnrated || fail === 2) return;
        if (isAllFetched) {
          fillCurrentRows();
          return;
        }
        isLoading = true;
        fail = 0;
        try{
          const firstPageData = await fetchContestPredictions(1);
          if (!firstPageData || !firstPageData.contest) {
            throw new Error('无法获取比赛信息');
          }
          const currentRatingMode = firstPageData.contest.rating_mode || null;
          console.log(`[等级分预测] 当前 rating_mode: ${currentRatingMode}`);
          if (currentRatingMode === 'unrated') {
            ratingMode = currentRatingMode;
            isUnrated = true;
            removePredictionUI();
            isLoading = false;
            return;
          }
          const cache = getCache();
          const now = Date.now();
          const cachedEntry = cache[contestId];
          if (cachedEntry) {
            const cachedMode = cachedEntry.ratingMode;
            const cachedTime = cachedEntry.timestamp;
            const ttl = getCacheTTL(cachedMode);
            if (cachedMode === currentRatingMode && (now - cachedTime < ttl)) {
              console.log('[等级分预测] 缓存有效,直接使用');
              ratingMode = cachedMode;
              isUnrated = (ratingMode === 'unrated');
              const cachedData = cachedEntry.data || [];
              cachedData.forEach(item => {
                dataMap[String(item.uid)] = {
                  delta: item.delta,
                  newRating: item.rating,
                  warnings: item.warnings || []
                };
              });
              isAllFetched = true;
              isLoading = false;
              fillCurrentRows();
              return;
            } else {
              console.log('[等级分预测] 缓存无效,重新获取');
            }
          }
          console.log('[等级分预测] 开始分页获取数据...');
          ratingMode = currentRatingMode;
          isUnrated = false;
          const firstItems = firstPageData.items || [];
          firstItems.forEach(item => {
            dataMap[String(item.uid)] = {
              delta: item.delta,
              newRating: item.rating,
              warnings: item.warnings || []
            };
          });
          ensureHeader();
          fillCurrentRows();
          let total = firstPageData.total || 0;
          let page = 2;
          while (true) {
            if (Object.keys(dataMap).length >= total) break;
            const data = await fetchContestPredictions(page);
            const items = data.items || [];
            if (items.length === 0) break;
            items.forEach(item => {
              dataMap[String(item.uid)] = {
                delta: item.delta,
                newRating: item.rating,
                warnings: item.warnings || []
              };
            });
            fillCurrentRows();
            total = data.total || total;
            page++;
          }
          const allData = Object.keys(dataMap).map(uid => ({
            uid: parseInt(uid),
            delta: dataMap[uid].delta,
            rating: dataMap[uid].newRating,
            warnings: dataMap[uid].warnings
          }));
          cache[contestId] = {
            data: allData,
            ratingMode: ratingMode,
            timestamp: Date.now()
          };
          setCache(cache);
          isAllFetched = true;
          console.log('[等级分预测] 全部数据获取完成,已缓存');
        } catch (e) {
          const cache = getCache();
          const cachedEntry = cache[contestId];
          if (cachedEntry && cachedEntry.data) {
            console.warn(`[等级分预测] 加载数据失败:${e}\n使用过期缓存作为降级`);
            fail = 1;
            const cachedData = cachedEntry.data || [];
            cachedData.forEach(item => {
              dataMap[String(item.uid)] = {
                delta: item.delta,
                newRating: item.rating,
                warnings: item.warnings || []
              };
            });
            isAllFetched = true;
            fillCurrentRows();
          } else {
            console.error(`[等级分预测] 加载数据失败:${e}\n无缓存可用`);
            fail = 2;
          }
        } finally {
          isLoading = false;
          fillCurrentRows();
        }
      }
      function ensureHeader() {
        if (isUnrated) {
          const headerRow = document.querySelector('.header-wrap .header');
          if (headerRow) {
            const oldHeader = headerRow.querySelector('.header-container[data-prediction-header]');
            if (oldHeader) oldHeader.remove();
          }
          return;
        }
        const headerRow = document.querySelector('.header-wrap .header');
        if (!headerRow) return;
        const oldHeader = headerRow.querySelector('.header-container[data-prediction-header]');
        if (oldHeader) oldHeader.remove();
        let headerHtml = '';
        let titleText = '';
        if (ratingMode === 'prediction') {
          headerHtml = '<span>Δ</span><span style="color:rgba(0,0,0,0.55);font-size:11px;display:block;">(预测)</span>';
          titleText = '等级分变动(预测结果,仅供参考)';
        } else if (ratingMode === 'official') {
          headerHtml = '<span>Δ</span>';
          titleText = '等级分变动(正式结果)';
        } else {
          headerHtml = '<span>Δ</span>';
          titleText = '等级分变动';
        }
        const newHeader = document.createElement('div');
        newHeader.className = 'header-container';
        newHeader.dataset.predictionHeader = 'true';
        newHeader.style.cssText = 'flex: 0 0 80px; text-align: center; display: flex; flex-direction: column; justify-content: center; cursor: help;';
        newHeader.title = titleText;
        newHeader.innerHTML = headerHtml;
        headerRow.appendChild(newHeader);
      }
      function updateFooter() {
        if (isUnrated) {
          const footer = document.querySelector('#prediction-footer');
          if (footer) footer.remove();
          return;
        }
        let footer = document.querySelector('#prediction-footer');
        if (!footer) {
          const container = document.querySelector('.l-card') || document.querySelector('main');
          if (!container) return;
          footer = document.createElement('div');
          footer.id = 'prediction-footer';
          footer.style.cssText = 'padding: 8px 16px; font-size: 12px; color: #999; text-align: right; border-top: 1px solid #eee; margin-top: 8px;';
          container.appendChild(footer);
        }
        if (fail === 2) {
          footer.textContent = '等级分预测:获取数据失败';
          return;
        }
        const cache = getCache();
        const entry = cache[contestId];
        if (entry) {
          const d = new Date(entry.timestamp);
          const timeStr = d.toLocaleString('zh-CN', { hour12: false });
          const modeStr = entry.ratingMode ? ` (${entry.ratingMode})` : '';
          const ttl = getCacheTTL(entry.ratingMode);
          const ttlHours = Math.round(ttl / (60 * 60 * 1000));
          const ttlDisplay = ttlHours >= 24 ? `${Math.round(ttlHours / 24)} 天` : `${ttlHours} 小时`;
          footer.textContent = `等级分预测:数据最后更新于 ${timeStr}${modeStr}`;
          if (fail === 1) {
            footer.textContent += '(缓存刷新失败)';
          } else {
            footer.textContent += `(缓存 ${ttlDisplay})`;
          }
          footer.textContent += '  数据来源:洛谷档案馆 luogu.ac.cn';
        } else {
          footer.textContent = '等级分预测:暂无缓存数据';
        }
      }
      async function init() {
        if (initialized) {
          if (isAllFetched) fillCurrentRows();
          else loadData();
          return;
        }
        initialized = true;
        if (!document.querySelector('.row-wrap .row')) {
          await new Promise(resolve => {
            const observer = new MutationObserver(() => {
              if (document.querySelector('.row-wrap .row')) {
                observer.disconnect();
                resolve();
              }
            });
            observer.observe(document.body, { childList: true, subtree: true });
            setTimeout(() => {
              observer.disconnect();
              resolve();
            }, 5000);
          });
        }
        setupObserver();
        await loadData();
      }
      function setupObserver() {
        const target = document.querySelector('.row-wrap') || document.querySelector('main');
        if (!target) return;
        let timer = null;
        const observer = new MutationObserver(() => {
          clearTimeout(timer);
          timer = setTimeout(() => {
            if (isUnrated) {
              removePredictionUI();
              return;
            }
            if (isAllFetched || isLoading) {
              fillCurrentRows();
            } else {
              loadData();
            }
          }, 500);
        });
        observer.observe(target, {
          childList: true,
          subtree: true,
          attributes: false,
          characterData: false
        });
      }
      function triggerInit() {
        if (!window.location.hash.includes('scoreboard')) return;
        if (initLock) return;
        initLock = true;
        clearTimeout(domCheckTimer);
        domCheckTimer = setTimeout(() => {
          if (document.querySelector('.row-wrap .row')) {
            if (!initialized) {
              init();
            } else {
              if (isAllFetched) fillCurrentRows();
              else loadData();
            }
          } else {
            const observer = new MutationObserver((mutations, obs) => {
              if (document.querySelector('.row-wrap .row')) {
                obs.disconnect();
                if (!initialized) {
                  init();
                } else {
                  if (isAllFetched) fillCurrentRows();
                  else loadData();
                }
              }
            });
            observer.observe(document.body, {
              childList: true,
              subtree: true
            });
            setTimeout(() => {
              observer.disconnect();
            }, 10000);
          }
          initLock = false;
        }, 300);
      }
      function startWatching() {
        window.addEventListener('hashchange', triggerInit);
        window.addEventListener('popstate', triggerInit);
        const targetNode = document.querySelector('#app') || document.querySelector('main') || document.body;
        const observer = new MutationObserver(() => {
          clearTimeout(window._domCheckTimer);
          window._domCheckTimer = setTimeout(() => {
            triggerInit();
          }, 200);
        });
        observer.observe(targetNode, {
          childList: true,
          subtree: true
        });
        setInterval(() => {
          if (window.location.hash.includes('scoreboard')) {
            triggerInit();
          }
        }, 2000);
      }
      startWatching();
      if (window.location.hash.includes('scoreboard')) {
        setTimeout(triggerInit, 500);
      }
    })();
  }
  else if (path.match(/^\/user\/\d+/)) {
    (function () {
      let lastUid = null;
      let lastPath = null;
      function waitForElement(selector, timeout = 5000) {
        return new Promise((resolve, reject) => {
          const start = Date.now();
          const check = () => {
            const el = document.querySelector(selector);
            if (el) resolve(el);
            else if (Date.now() - start > timeout) reject(new Error(`元素 ${selector} 加载超时`));
            else setTimeout(check, 200);
          };
          check();
        });
      }
      function getUid() {
        const match = window.location.pathname.match(/\/user\/(\d+)/);
        return match ? match[1] : null;
      }
      function parseHistoryData() {
        const contextEl = document.getElementById('lentille-context');
        if (!contextEl) throw new Error('未找到 #lentille-context');
        const raw = contextEl.textContent;
        const parsed = JSON.parse(raw);
        const elo = parsed?.data?.elo;
        return elo;
      }
      function fetchPredictions(uid) {
        const url = `https://luogu.ac.cn/api/v1/user/${uid}/rating-predictions`;
        return gmRequest(url);
      }
      function buildFullData(history, predictions) {
        const sortedHistory = history.slice().sort((a, b) => a.time - b.time).map(item => ({
          ...item,
          previousRating: item.rating - (item.prevDiff || 0)
        }));
        const rawItems = predictions?.items || [];
        const validItems = rawItems.filter(item => {
          const warnings = item.warnings || [];
          return !warnings.some(w => w.includes('赛前等级分不低于本场等级分阈值'));
        });
        const predData = validItems.map(item => ({
          isPredicted: true,
          rating: item.predicted_rating,
          time: isoToUnix(item.end_time),
          latest: false,
          contest: {
            id: item.contest_id,
            startTime: isoToUnix(item.start_time),
            endTime: isoToUnix(item.end_time),
            name: item.contest_name
          },
          userCount: 0,
          prevDiff: item.predicted_delta,
          previous: {
            rating: item.predicted_rating - item.predicted_delta,
            time: null,
            latest: false,
            contest: null,
            userCount: 0,
            prevDiff: null
          },
          previousRating: item.predicted_rating - item.predicted_delta,
          warnings: item.warnings || []
        }));
        const full = [...sortedHistory, ...predData];
        full.sort((a, b) => {
          if (a.time !== b.time) return a.time - b.time;
          return (a.previousRating || 0) - (b.previousRating || 0);
        });
        if (full.length > 0 && full[0].isPredicted) {
          full[0].isFirst = true;
        } else {
          let firstHistoricalFound = false;
          for (const item of full) {
            if (!item.isPredicted && !firstHistoricalFound) {
              item.isFirst = true;
              firstHistoricalFound = true;
              break;
            }
          }
        }
        const firstPredIdx = full.findIndex(d => d.isPredicted);
        if (firstPredIdx > 0) {
          const prevItem = full[firstPredIdx - 1];
          if (prevItem && !prevItem.isPredicted) {
            const placeholder = {
              ...prevItem,
              isPredicted: true,
              isPlaceholder: true,
            };
            full.splice(firstPredIdx, 0, placeholder);
          }
        }
        return full;
      }
      function gatPageType() {
        const path = window.location.pathname;
        const match = path.match(/^\/user\/(\d+)/);
        if (!match) return '';
        const rest = path.substring(match[0].length);
        if (rest === '' || rest === '/') return 'profile';
        if (/^\/user\/\d+\/practice/.test(window.location.pathname)) return 'practice';
        return '';
      }
      function prepareContainer(pagetype) {
        let existingCard = null;
        const cardById = document.querySelector('div.l-card:has(#rating-chart)');
        if (cardById) {
          existingCard = cardById;
        } else {
          const cards = document.querySelectorAll('div.l-card');
          for (const card of cards) {
            const header = card.querySelector('.header h3.lfe-h3');
            if (header && header.textContent.trim() === '比赛等级分趋势图') {
              existingCard = card;
              break;
            }
          }
        }
        if (existingCard) {
          const header = existingCard.querySelector('.header');
          while (existingCard.firstChild) {
            existingCard.removeChild(existingCard.firstChild);
          }
          if (header) existingCard.appendChild(header);
          const chartDiv = document.createElement('div');
          chartDiv.id = 'rating-chart';
          chartDiv.style.cssText = 'width: 100%; height: 320px;';
          existingCard.appendChild(chartDiv);
          return chartDiv;
        }
        const card = document.createElement('div');
        card.className = 'l-card';
        card.style.marginBottom = '20px';
        card.setAttribute('data-v-176b97b3', '');
        card.setAttribute('data-v-d3b68fa4', '');
        card.setAttribute('data-v-4ad5148e', '');
        card.setAttribute('data-v-754e1ea4-s', '');
        const header = document.createElement('div');
        header.className = 'header';
        header.setAttribute('data-v-03592857', '');
        const h3 = document.createElement('h3');
        h3.className = 'lfe-h3';
        h3.textContent = '比赛等级分趋势图';
        h3.setAttribute('data-v-03592857', '');
        header.appendChild(h3);
        card.appendChild(header);
        const span = document.createElement('span');
        span.className = 'lfe-caption';
        span.textContent = '选中记录后可打开比赛页面';
        span.setAttribute('data-v-03592857', '');
        header.appendChild(span);
        card.appendChild(header);
        const chartDiv = document.createElement('div');
        chartDiv.id = 'rating-chart';
        chartDiv.setAttribute('data-v-03592857', '');
        chartDiv.style.cssText = 'width: 100%; height: 320px;';
        card.appendChild(chartDiv);
        let inserted = false;
        if (pagetype === 'profile') {
          const allCards = document.querySelectorAll('.l-card');
          let refCard = null;
          for (const c of allCards) {
            const h = c.querySelector('.header h3');
            if (h && h.textContent.trim().includes('做题趋势热度图')) {
              refCard = c;
              break;
            }
          }
          if (refCard) {
            refCard.insertAdjacentElement('afterend', card);
            inserted = true;
          }
        } else if (pagetype === 'practice') {
          const container = document.querySelector('.user-main') || document.body;
          let targetCard = null;
          const allCards = container.querySelectorAll('.l-card');
          for (const c of allCards) {
            const emptyBlock = c.querySelector('.empty-block');
            if (emptyBlock) {
              const h3 = emptyBlock.querySelector('h3.title');
              if (h3 && h3.textContent.trim() === '该用户设置了完全隐私保护,无法查看练习记录') {
                targetCard = c;
                break;
              }
            }
            const directH3 = c.querySelector('h3.lfe-h3');
            if (directH3 && directH3.textContent.trim() === '尝试过的题目') {
              targetCard = c;
              break;
            }
          }
          if (targetCard) {
            targetCard.insertAdjacentElement('beforebegin', card);
          }
          inserted = true;
        }
        if (!inserted) return null;
        return chartDiv;
      }
      function renderChart(container, data) {
        if (!container) return;
        const myChart = echarts.init(container);
        const historicalData = data.filter(d => !d.isPredicted);
        const historicalMax = historicalData.length > 0 ? Math.max(...historicalData.map(d => d.rating)) : 0;
        const predictedData = data.filter(d => d.isPredicted && !d.isPlaceholder);
        const predictedMax = predictedData.length > 0 ? Math.max(...predictedData.map(d => d.rating)) : -Infinity;
        const showPredictedMax = predictedMax >= historicalMax;
        data.forEach(item => {
          if (!item.isPredicted) {
            item.isHistoricalMax = (item.rating === historicalMax);
          } else {
            item.isPredictedMax = (item.rating === predictedMax && showPredictedMax);
          }
        });
        const historical = data.filter(d => !d.isPredicted);
        const predictedFull = data.filter(d => d.isPredicted);
        const maxRating = Math.max(...data.map(d => d.rating));
        const yMax = Math.ceil(maxRating / 400) * 400 || 400;
        function buildSeriesItems(arr, isPredictedSeries = false) {
          return arr.map(item => {
            const base = {
              value: [item.time * 1000, item.rating],
              symbol: item.isPlaceholder ? 'none' : 'circle',
              symbolSize: 5,
              rawInfo: item
            };
            if (isPredictedSeries) {
              if (item.isPlaceholder) {
                base.itemStyle = { color: 'transparent', borderColor: 'transparent' };
              } else if (item.isPredictedMax) {
                base.itemStyle = {
                  color: 'rgba(255,255,255,0.4)',
                  borderColor: 'rgba(240,112,7,0.6)',
                  borderWidth: 2
                };
              } else {
                base.itemStyle = {
                  color: 'rgba(255,255,255,0.4)',
                  borderColor: 'rgba(93,173,226,0.6)',
                  borderWidth: 1.5
                };
              }
            } else {
              if (item.isHistoricalMax) {
                base.itemStyle = {
                  color: '#fff',
                  borderColor: '#e74c3c',
                  borderWidth: 2
                };
              } else {
                base.itemStyle = {
                  color: '#fff',
                  borderColor: '#5dade2',
                  borderWidth: 2
                };
              }
            }
            return base;
          });
        }
        const option = {
          grid: {
            left: 0,
            right: 15,
            top: 25,
            bottom: 20,
            containLabel: true
          },
          xAxis: {
            type: 'time',
            axisLine: { lineStyle: { color: '#ccc' } },
            axisLabel: {
              color: '#555',
              fontSize: 12,
              formatter: function (value) {
                const d = new Date(value);
                return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0');
              }
            },
            splitLine: { show: false }
          },
          yAxis: {
            type: 'value',
            min: 0,
            max: yMax,
            interval: 400,
            axisLine: { show: false },
            axisLabel: { color: '#555', fontSize: 12 },
            splitLine: { show: true, lineStyle: { color: '#e0e0e0', type: 'solid' } }
          },
          tooltip: {
            trigger: 'item',
            backgroundColor: 'rgba(42,42,42,0.95)',
            borderColor: 'transparent',
            borderRadius: 4,
            padding: [12, 16],
            textStyle: { color: '#fff' },
            formatter: function (params) {
              const item = params.data.rawInfo;
              if (!item || item.isPlaceholder) return '';
              const isPred = item.isPredicted || false;
              const Prefix = isPred ? '[预测] ' : '';
              const start = formatTime(item.contest.startTime);
              const end = formatTime(item.contest.endTime);
              let ratingDisplay = '';
              if (item.isFirst) {
                ratingDisplay = `等级分:<span style="color:#fff; font-weight:bold;">${item.rating}</span>`;
              } else {
                let prevRating = 0, diff = 0;
                if (item.previous && item.previous.rating !== undefined) {
                  prevRating = item.previous.rating;
                  diff = item.prevDiff || 0;
                } else {
                  prevRating = item.rating - (item.prevDiff || 0);
                  diff = item.prevDiff || 0;
                }
                let diffColor, diffDisplay;
                if (diff === 0) {
                  diffColor = '#888';
                  diffDisplay = '±0';
                } else if (diff > 0) {
                  diffColor = '#4caf50';
                  diffDisplay = '+' + diff;
                } else {
                  diffColor = '#e74c3c';
                  diffDisplay = diff;
                }
                ratingDisplay = `
                  等级分:<span style="color:#fff; font-weight:bold;">${prevRating}</span>
                  <span style="color:${diffColor}; font-weight:bold;"> ${diffDisplay}</span>
                  = <span style="color:#fff; font-weight:bold;">${item.rating}</span>
                `;
              }
              let extraText = '';
              if (item.isHistoricalMax) {
                extraText += '<div style="font-style:italic; color:#fff; margin-top:4px;">达成历史最高</div>';
              }
              if (item.isPredictedMax) {
                extraText += '<div style="font-style:italic; color:#fff; margin-top:4px;">预计达成历史最高</div>';
              }
              if (item.warnings && item.warnings.length > 0) {
                for (const warn of item.warnings) {
                  extraText += `<div style="color:rgb(170,170,170); margin-top:2px;">ⓘ ${warn}</div>`;
                }
              }
              return `
                <div style="font-size:14px; font-weight:bold; margin-bottom:4px;">${item.contest.name}</div>
                <div style="font-size:12px; color:#a0a0a0; margin-bottom:6px;">${start} ~ ${end}</div>
                <div style="font-size:14px;">${Prefix}${ratingDisplay}</div>
                ${extraText}
              `;
            }
          },
          series: [{
            name: '历史比赛',
            type: 'line',
            data: buildSeriesItems(historical, false),
            smooth: false,
            lineStyle: { type: 'solid', color: '#5dade2', width: 2.5 },
            animationDuration: 1500,
            animationEasing: 'cubicOut'
          }, {
            name: '预测比赛',
            type: 'line',
            data: buildSeriesItems(predictedFull, true),
            smooth: false,
            lineStyle: {
              type: 'dashed',
              color: '#5dade2',
              width: 2.5,
              dash: [8, 6],
              opacity: 0.6
            },
            animationDuration: 1500,
            animationEasing: 'cubicOut'
          }]
        };
        myChart.setOption(option);
        myChart.on('click', function (params) {
          const item = params.data?.rawInfo;
          if (!item || item.isPlaceholder) return;
          const contestId = item.contest?.id;
          if (contestId) {
            window.open(`https://www.luogu.com.cn/contest/${contestId}`, '_blank');
          }
        });
        const resizeHandler = () => myChart.resize();
        window.addEventListener('resize', resizeHandler);
        const observer = new ResizeObserver(resizeHandler);
        observer.observe(container);
      }
      async function main() {
        try {
          const pageType = gatPageType();
          if (pageType !== 'profile' && pageType !== 'practice') return;
          const uid = getUid();
          if (!uid) {
            console.warn('[等级分预测] 未找到用户 ID');
            return;
          }
          let history;
          try {
            history = parseHistoryData();
          } catch (e) {
            console.error('[等级分预测] 解析历史数据失败', e);
            return;
          }
          let predictions = null;
          let fetchError = false;
          try {
            predictions = await fetchPredictions(uid);
          } catch (e) {
            console.warn('[等级分预测] 获取预测数据失败,只显示历史比赛数据', e);
            fetchError = true;
            predictions = { items: [] };
          }
          const fullData = buildFullData(history, predictions);
          if (fullData.length === 0) {
            console.log('[等级分预测] 无数据可展示');
            return;
          }
          const chartDiv = prepareContainer(pageType);
          if (!chartDiv) {
            console.error('[等级分预测] 无法创建或找到卡片容器');
            return;
          }
          await new Promise(resolve => setTimeout(resolve, 100));
          renderChart(chartDiv, fullData);
          const card = chartDiv.parentNode;
          if (!card) return;
          const hasPrediction = fullData.some(d => d.isPredicted && !d.isPlaceholder);
          let showFooter = false;
          let footerText = '';
          if (fetchError) {
            showFooter = true;
            footerText = '获取预测数据失败,只显示历史比赛数据';
          } else if (hasPrediction) {
            showFooter = true;
            footerText = '虚线部分为预测,不代表最终等级分变动<br>数据来源:洛谷档案馆 luogu.ac.cn';
          }
          const oldFooter = card.querySelector('.rating-footer');
          if (oldFooter) oldFooter.remove();
          if (showFooter) {
            const footer = document.createElement('div');
            footer.className = 'rating-footer';
            footer.style.cssText = `
              margin-top: 8px;
              font-size: 12px;
              color: #999;
              text-align: right;
              border-top: 1px solid #f0f0f0;
              padding: 8px 20px 0 20px;
            `;
            footer.innerHTML = `
              <span style="display:inline-block; background:#eee; border-radius:50%; width:16px; height:16px; line-height:16px; text-align:center; color:#666; font-weight:bold; margin-right:4px;">!</span>
              ${footerText}
            `;
            card.appendChild(footer);
          }
        } catch (error) {
          console.error('[等级分预测] 脚本运行出错:', error);
        }
      }
      function checkAndRun() {
        const uid = getUid();
        const path = window.location.pathname;
        if (uid && (uid !== lastUid || path !== lastPath)) {
          lastUid = uid;
          lastPath = path;
          main();
        }
      }
      function init() {
        checkAndRun();
        window.addEventListener('popstate', checkAndRun);
        const originalPush = history.pushState;
        const originalReplace = history.replaceState;
        history.pushState = function () {
          originalPush.apply(this, arguments);
          checkAndRun();
        };
        history.replaceState = function () {
          originalReplace.apply(this, arguments);
          checkAndRun();
        };
      }
      if (document.readyState === 'complete') {
        init();
      } else {
        window.addEventListener('load', init);
      }
    })();
  }
})();

随机跳题优化

INFO
JavaScript
/*
MIT License

Copyright (c) 2026 karsl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// ==UserScript==
// @name         洛谷随机跳题(离线)
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  筛选题库、难度、标签后随机跳题,使用离线题库
// @author       karsl
// @match        https://www.luogu.com.cn/
// @icon         https://www.luogu.com.cn/favicon.ico
// @grant        GM_xmlhttpRequest
// @connect      www.luogu.com.cn
// @connect      cdn.luogu.com.cn
// @require      https://cdn.jsdelivr.net/npm/pako@2.1.0/dist/pako.min.js
// ==/UserScript==

(function() {
    'use strict';

    const DB_NAME = 'LuoguOfflineDB';
    const DB_VERSION = 4;
    const STORE_NAME = 'problems';
    const METADATA_STORE = 'metadata';
    const USER_STATUS_STORE = 'user_status';
    const DATA_URL = 'https://cdn.luogu.com.cn/problemset-open/latest.ndjson.gz';

    const DIFFICULTY_MAP = {
        0: '暂未评定',
        1: '入门',
        2: '普及-',
        3: '普及',
        4: '普及+/提高-',
        5: '提高',
        6: '提高+/省选-',
        7: '省选/NOI-',
        8: 'NOI/NOI+/CTS'
    };

    const TYPE_MAP = {
        '洛谷': 'luogu',
        '主题库': 'P',
        '入门与面试': 'B',
        'Codeforces': 'CF',
        'SPOJ': 'SP',
        'AtCoder': 'AT',
        'UVa': 'UVA'
    };
    const TYPE_KEYS = Object.keys(TYPE_MAP);

    const dimensionMaps = {
        algorithm: {
            '语言入门': '-2',
            '字符串': '2',
            '后缀自动机 SAM': '101',
            '字典树 Trie': '300',
            'AC 自动机': '301',
            'KMP 算法': '302',
            '后缀数组 SA': '303',
            '后缀树': '304',
            '有限状态自动机': '305',
            '回文自动机 PAM': '325',
            'Manacher 算法': '329',
            '顺序结构': '353',
            '分支结构': '354',
            '循环结构': '355',
            '数组': '356',
            '字符串(入门)': '357',
            '结构体': '358',
            '函数与递归': '359',
            'Lyndon 分解': '419',
            'Z 函数': '474',
            '动态规划 DP': '3',
            '搜索': '4',
            '广度优先搜索 BFS': '126',
            '深度优先搜索 DFS': '127',
            '剪枝': '128',
            '记忆化搜索': '129',
            '启发式搜索': '130',
            '迭代加深搜索': '131',
            '启发式迭代加深搜索 IDA*': '132',
            'Dancing Links': '133',
            '爬山算法 Local search': '134',
            '模拟退火': '135',
            '背包 DP': '139',
            '数位 DP': '141',
            '区间 DP': '144',
            '树形 DP': '152',
            '轮廓线 DP': '323',
            '线性 DP': '444',
            '状压 DP': '464',
            '后缀平衡树': '496',
            '数学': '5',
            '图论': '6',
            'Kruskal 重构树': '68',
            '网络流': '79',
            '信息论': '122',
            '随机调整': '136',
            '遗传算法': '137',
            '图论建模': '155',
            '图遍历': '158',
            '拓扑排序': '159',
            '最短路': '160',
            '生成树': '166',
            '平面图': '172',
            '最小环': '173',
            'A*  算法': '263',
            '折半搜索 meet in the middle': '380',
            '梯度下降法': '396',
            '拉格朗日乘数法': '412',
            '拉格朗日插值法': '417',
            '单位根反演': '480',
            '负权环': '174',
            '连通块': '175',
            '2-SAT': '176',
            '平面图欧拉公式': '177',
            '强连通分量': '179',
            'Tarjan': '180',
            '双连通分量': '181',
            '欧拉回路': '182',
            '差分约束': '185',
            '仙人掌': '186',
            '二分图': '187',
            '一般图的最大匹配': '189',
            '上下界网络流': '197',
            '最小割': '198',
            '费用流': '204',
            '圆方树': '350',
            '弦图': '450',
            'Floyd 算法': '476',
            '广义串并联图': '530',
            'Hall 定理': '541',
            '计算几何': '8',
            '树形数据结构': '11',
            '线段树': '42',
            '并查集': '47',
            '平衡树': '50',
            '堆': '51',
            '树状数组': '53',
            'cdq 分治': '100',
            '可并堆': '215',
            '动态树 LCT': '229',
            '三维计算几何': '283',
            '向量': '286',
            '凸包': '291',
            '叉积': '292',
            '线段相交': '293',
            '半平面交': '295',
            '旋转卡壳': '299',
            '极角排序': '449',
            '平面几何': '482',
            '闵可夫斯基和 Minkowski sum': '525',
            '博弈论': '13',
            '线性数据结构': '44',
            '单调队列': '56',
            '颜色段均摊(珂朵莉树 ODT)': '65',
            '树套树': '232',
            '可持久化线段树': '233',
            '可持久化': '234',
            '前缀和': '254',
            '栈': '287',
            '队列': '288',
            '博弈树': '311',
            '整体二分': '316',
            'K-D Tree': '321',
            'Nim 积': '370',
            '李超线段树': '377',
            '吉司机线段树 segment tree beats': '378',
            '线段树合并': '379',
            'SG 函数': '445',
            '二区间合并(猫树分治)': '531',
            'KTT / Kinetic Tournament Tree': '539',
            '原根': '66',
            '多项式': '69',
            '数论': '72',
            '素数判断': '239',
            '最大公约数 gcd': '241',
            '扩展欧几里德算法': '242',
            '不定方程': '243',
            '进制': '244',
            '分块': '289',
            'ST 表': '290',
            '快速傅里叶变换 FFT': '313',
            '快速数论变换 NTT': '324',
            '快速沃尔什变换 FWT': '326',
            '快速莫比乌斯变换 FMT': '327',
            '差分': '330',
            '链表': '360',
            '单调栈': '385',
            'Berlekamp-Massey(BM) 算法': '391',
            '集合幂级数,子集卷积': '416',
            '哈希表': '473',
            '模拟': '1',
            '贪心': '7',
            '基础算法': '110',
            '中国剩余定理 CRT': '250',
            '莫比乌斯反演': '251',
            '逆元': '276',
            'Lucas 定理': '322',
            '类欧几里得算法': '388',
            '调和级数': '411',
            '欧拉降幂': '415',
            'Stern-Brocot 树': '423',
            '整除分块': '448',
            'Dirichlet 卷积': '451',
            '大步小步算法 BSGS': '452',
            '二次剩余': '453',
            'Bézout 定理': '455',
            '杜教筛': '461',
            '欧拉函数': '462',
            '线性筛法': '475',
            '亚线性快速求和算法': '540',
            '递推': '12',
            '倍增': '43',
            '二分': '45',
            '递归': '54',
            '枚举': '111',
            '分治': '112',
            '排序': '113',
            '动态规划优化': '146',
            '优先队列': '148',
            '矩阵加速': '149',
            '斜率优化': '150',
            '状态合并': '151',
            '凸完全单调性(wqs 二分)': '153',
            '四边形不等式': '154',
            'DP 套 DP': '435',
            '动态 DP': '443',
            '决策单调性': '463',
            'STL': '504',
            '整体转移': '507',
            '斜率维护技巧 slope trick': '508',
            '点分治': '49',
            '树上启发式合并': '55',
            '树的遍历': '208',
            '最近公共祖先 LCA': '211',
            '树的直径': '213',
            '树链剖分': '228',
            '树论': '230',
            '群论': '246',
            '置换': '247',
            'Pólya 定理': '248',
            '虚树': '249',
            '组合数学': '252',
            '排列组合': '253',
            '二项式定理': '255',
            '康托展开': '256',
            '基环树': '320',
            '动态树分治': '382',
            'Prüfer 序列': '410',
            '全局平衡二叉树': '472',
            '树的重心': '483',
            'LGV 引理': '63',
            '矩阵树定理': '64',
            '矩阵运算': '71',
            '鸽笼原理': '258',
            '容斥原理': '259',
            'Fibonacci 数列': '260',
            'Catalan 数': '261',
            'Stirling 数': '262',
            '生成函数': '264',
            '概率论': '266',
            '期望': '270',
            '线性代数': '271',
            '矩阵乘法': '272',
            '线性递推': '273',
            '高斯消元': '274',
            'Dilworth 定理': '364',
            '拉格朗日反演': '372',
            '杨表': '387',
            '随机游走 Markov Chain': '457',
            '鞅的停时定理': '458',
            '暴力数据结构': '9',
            '高精度': '10',
            '莫队': '41',
            '三分': '67',
            '离散化': '78',
            '霍夫曼树': '214',
            '哈希 hashing': '235',
            '线性基': '277',
            '微积分': '278',
            '导数': '280',
            '积分': '281',
            '定积分': '282',
            '级数': '284',
            '扫描线': '298',
            '其它技巧': '308',
            '随机化': '309',
            '位运算': '314',
            '构造': '318',
            '行列式': '454',
            '特征值': '466',
            '分数规划': '202',
            '线性规划': '265',
            '双指针 two-pointer': '345',
            'Ad-hoc': '365',
            '笛卡尔树': '368',
            '拟阵': '369',
            '根号分治': '371',
            '模拟费用流': '373',
            '分散层叠': '374',
            '均摊分析': '375',
            '分类讨论': '376',
            '近似算法': '413',
            '线段树分治': '446',
            '离线处理': '447',
            'bitset': '465',
            '组合优化': '467',
            '整数规划': '468',
            '原始对偶': '470',
            '启发式合并': '477',
            '反悔贪心': '524',
            '最大流最小割定理': '471',
            '保序回归': '484'
        },
        source: {
            '各省省选': '48',
            '集训队互测': '52',
            '福建省历届夏令营': '70',
            'NOI': '77',
            'NOIP 普及组': '82',
            'NOIP 提高组': '83',
            'APIO': '85',
            'NOI 导刊': '99',
            'CTT(清华集训/北大集训)': '331',
            '网络流与线性规划 24 题': '332',
            'CSP-S 提高级': '342',
            'CSP-J 入门级': '343',
            'NOI Online': '347',
            'Ynoi': '348',
            'NOI 系列赛事': '426',
            '经典套题': '427',
            '国际知名赛事': '428',
            'WC': '459',
            'CTSC/CTS': '460',
            '模板题': '523',
            'USACO': '46',
            'POI(波兰)': '57',
            'IOI': '102',
            'CCO(加拿大)': '115',
            'CCC(加拿大)': '116',
            'CEOI(中欧)': '117',
            'eJOI(欧洲)': '118',
            'COCI(克罗地亚)': '333',
            'BalticOI(波罗的海)': '334',
            'JOI(日本)': '336',
            'AGM': '346',
            'PA(波兰)': '389',
            'ROI(俄罗斯)': '393',
            'EGOI(欧洲/女生)': '394',
            'NOISG(新加坡)': '436',
            'NordicOI(北欧)': '437',
            'BalkanOI(巴尔干半岛)': '439',
            'KOI(韩国)': '440',
            'RMI(罗马尼亚)': '441',
            'COI(克罗地亚)': '478',
            '洛谷原创': '81',
            'ICPC': '335',
            '洛谷月赛': '337',
            '语言月赛': '386',
            '洛谷比赛': '429',
            '大学竞赛': '430',
            'ROIR(俄罗斯)': '479',
            'INOI(伊朗)': '505',
            'UOI(乌克兰)': '506',
            'JOISC/JOIST(日本)': '512',
            'COTS(克罗地亚)': '513',
            'Google Code Jam': '514',
            'Moscow Olympiad': '515',
            'PO(瑞典)': '518',
            'PAIO': '519',
            'MCC/MCO(马来西亚)': '533',
            'KTSC(韩国)': '535',
            'IATI(保加利亚/东欧)': '537',
            'Google Kick Start': '538',
            '入门赛': '542',
            '蓝桥杯国赛': '361',
            '蓝桥杯省赛': '363',
            '省赛/邀请赛': '381',
            '传智杯': '383',
            'THUPC': '390',
            'GESP': '409',
            '其他竞赛': '431',
            'THUSC': '432',
            '高校校赛': '434',
            'THUWC': '438',
            'CSP-X 小学组': '442',
            'Code+': '485',
            '梦熊比赛': '486',
            '信息与未来': '487',
            '科创活动': '488',
            'BCSP-X': '489',
            '小学活动': '492',
            '初中活动': '493',
            'CSPro': '516',
            'CCPC': '526',
            '科大国创杯': '494',
            '蓝桥杯青少年组': '495'
        },
        time: {
            '1996': '344',
            '1997': '14',
            '1998': '15',
            '1999': '16',
            '2000': '17',
            '2001': '18',
            '2002': '19',
            '2003': '20',
            '2004': '21',
            '2005': '22',
            '2006': '23',
            '2007': '24',
            '2008': '25',
            '2009': '26',
            '2010': '27',
            '2011': '28',
            '2012': '29',
            '2013': '30',
            '2014': '31',
            '2015': '32',
            '2016': '33',
            '2017': '34',
            '2018': '35',
            '2019': '36',
            '2020': '37',
            '2021': '58',
            '2022': '59',
            '2023': '60',
            '2024': '61',
            '2025': '62',
            '2026': '338',
            '2027': '339',
            '2028': '340',
            '2029': '367',
            '2077': '341',
            '2078': '362',
            '2079': '424'
        },
        region: {
            '重庆': '38',
            '四川': '39',
            '河南': '40',
            '浙江': '88',
            '上海': '89',
            '福建': '90',
            '江苏': '91',
            '安徽': '92',
            '湖南': '93',
            '北京': '94',
            '河北': '95',
            '广东': '96',
            '山东': '97',
            '吉林': '98',
            '山西': '114',
            '江西': '161',
            '贵州': '162',
            '广西': '163',
            '陕西': '164',
            '国内省市': '497',
            '辽宁': '167',
            '云南': '168',
            '天津': '328',
            '湖北': '397',
            '黑龙江': '398',
            '海南': '399',
            '甘肃': '400',
            '青海': '401',
            '台湾': '402',
            '内蒙古': '403',
            '西藏': '404',
            '宁夏': '405',
            '新疆': '406',
            '香港': '407',
            '澳门': '408',
            '济南': '420',
            '南京': '421',
            '青岛': '422',
            '国内赛站': '498',
            'EC Final': '509',
            '国际赛区': '499',
            'NERC/NEERC': '500',
            'SEERC': '501',
            'CERC': '502',
            'NWRRC': '503',
            'WF': '510',
            'NAC': '511',
            '杭州': '520',
            '昆明': '521',
            '西安': '522',
            '哈尔滨': '527',
            '首尔': '528',
            '横浜': '529',
            'SWERC': '532',
            '成都': '534',
            '雅加达': '536'
        },
        special: {
            '交互题': '103',
            '提交答案': '104',
            'Special Judge': '107',
            'O2优化': '108',
            '通信题': '351'
        }
    };

    const DIMENSION_NAMES = {
        algorithm: '算法',
        source: '来源',
        time: '时间',
        region: '区域',
        special: '特殊题目'
    };
    const DIMENSION_KEYS = ['algorithm', 'source', 'time', 'region', 'special'];

    const STORAGE_KEY = 'luogu_random_jump_settings';
    let selectedTypes = new Set();
    let selectedDifficulties = new Set();
    let selectedTags = {
        algorithm: new Set(),
        source: new Set(),
        time: new Set(),
        region: new Set(),
        special: new Set()
    };
    let filterAccepted = false;
    let filterSubmitted = false;

    let userUid = null;
    let acceptedSet = new Set();
    let submittedSet = new Set();
    let userStatusReady = false;

    function loadSettings() {
        try {
            const raw = localStorage.getItem(STORAGE_KEY);
            if (!raw) return;
            const data = JSON.parse(raw);
            if (data.types) selectedTypes = new Set(data.types);
            if (data.difficulties) selectedDifficulties = new Set(data.difficulties);
            if (data.tags) {
                DIMENSION_KEYS.forEach(key => {
                    if (data.tags[key] && Array.isArray(data.tags[key])) {
                        selectedTags[key] = new Set(data.tags[key]);
                    }
                });
            }
            if (typeof data.filterAccepted === 'boolean') filterAccepted = data.filterAccepted;
            if (typeof data.filterSubmitted === 'boolean') filterSubmitted = data.filterSubmitted;
        } catch (e) {
            console.warn('读取存储失败', e);
        }
    }

    function saveSettings() {
        const data = {
            types: Array.from(selectedTypes),
            difficulties: Array.from(selectedDifficulties),
            tags: {},
            filterAccepted: filterAccepted,
            filterSubmitted: filterSubmitted
        };
        DIMENSION_KEYS.forEach(key => {
            data.tags[key] = Array.from(selectedTags[key]);
        });
        localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
    }

    loadSettings();

    function openDB() {
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(DB_NAME, DB_VERSION);
            request.onupgradeneeded = function(e) {
                const db = e.target.result;
                if (!db.objectStoreNames.contains(STORE_NAME)) {
                    const store = db.createObjectStore(STORE_NAME, { keyPath: 'pid' });
                    store.createIndex('difficulty', 'difficulty', { unique: false });
                    store.createIndex('type', 'type', { unique: false });
                    store.createIndex('tags', 'tags', { multiEntry: true });
                }
                if (!db.objectStoreNames.contains(METADATA_STORE)) {
                    db.createObjectStore(METADATA_STORE, { keyPath: 'key' });
                }
                if (!db.objectStoreNames.contains(USER_STATUS_STORE)) {
                    db.createObjectStore(USER_STATUS_STORE, { keyPath: 'id' });
                    console.log('【洛谷随机跳题】数据库升级:创建做题记录存储');
                }
            };
            request.onsuccess = () => resolve(request.result);
            request.onerror = () => reject(request.error);
        });
    }

    function getProblemCount() {
        return openDB().then(db => {
            return new Promise((resolve, reject) => {
                const tx = db.transaction(STORE_NAME, 'readonly');
                const store = tx.objectStore(STORE_NAME);
                const countReq = store.count();
                countReq.onsuccess = () => resolve(countReq.result);
                countReq.onerror = () => reject(countReq.error);
            });
        });
    }

    function clearProblems() {
        return openDB().then(db => {
            return new Promise((resolve, reject) => {
                const tx = db.transaction(STORE_NAME, 'readwrite');
                const store = tx.objectStore(STORE_NAME);
                const req = store.clear();
                req.onsuccess = () => resolve();
                req.onerror = () => reject(req.error);
            });
        });
    }

    function bulkInsert(problems) {
        return openDB().then(db => {
            return new Promise((resolve, reject) => {
                const tx = db.transaction(STORE_NAME, 'readwrite');
                const store = tx.objectStore(STORE_NAME);
                for (const p of problems) {
                    store.put(p);
                }
                tx.oncomplete = () => resolve();
                tx.onerror = () => reject(tx.error);
            });
        });
    }

    function setMetadata(key, value) {
        return openDB().then(db => {
            return new Promise((resolve, reject) => {
                const tx = db.transaction(METADATA_STORE, 'readwrite');
                const store = tx.objectStore(METADATA_STORE);
                store.put({ key, value });
                tx.oncomplete = () => resolve();
                tx.onerror = () => reject(tx.error);
            });
        });
    }

    async function saveUserStatus(uid, acceptedPids, submittedPids) {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(USER_STATUS_STORE, 'readwrite');
            const store = tx.objectStore(USER_STATUS_STORE);
            store.put({
                id: 'user_status',
                uid,
                acceptedPids,
                submittedPids,
                timestamp: Date.now()
            });
            tx.oncomplete = () => {
                console.log('【洛谷随机跳题】做题记录已保存到 IndexedDB');
                resolve();
            };
            tx.onerror = (e) => {
                console.error('保存做题记录失败', e);
                reject(e);
            };
        });
    }

    async function loadUserStatus() {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(USER_STATUS_STORE, 'readonly');
            const store = tx.objectStore(USER_STATUS_STORE);
            const req = store.get('user_status');
            req.onsuccess = () => {
                console.log('【洛谷随机跳题】从 IndexedDB 读取做题记录:', req.result ? '成功' : '无数据');
                resolve(req.result || null);
            };
            req.onerror = (e) => {
                console.error('读取做题记录失败', e);
                reject(e);
            };
        });
    }

    function getUidFromDOM() {
        const avatarLinks = document.querySelectorAll('a[href*="/user/"]');
        for (const link of avatarLinks) {
            if (link.querySelector('img.avatar') || link.querySelector('img[alt]')) {
                const match = link.getAttribute('href').match(/\/user\/(\d+)/);
                if (match) return match[1];
            }
        }
        const avatarImg = document.querySelector('img.avatar, .user-nav img[alt]');
        if (avatarImg) {
            const parentLink = avatarImg.closest('a[href*="/user/"]');
            if (parentLink) {
                const match = parentLink.getAttribute('href').match(/\/user\/(\d+)/);
                if (match) return match[1];
            }
        }
        return null;
    }

    function waitForUid() {
        return new Promise((resolve) => {
            const uid = getUidFromDOM();
            if (uid) {
                resolve(uid);
                return;
            }
            let attempts = 0;
            const maxAttempts = 20;
            const interval = setInterval(() => {
                const uid = getUidFromDOM();
                if (uid) {
                    clearInterval(interval);
                    resolve(uid);
                } else {
                    attempts++;
                    if (attempts >= maxAttempts) {
                        clearInterval(interval);
                        resolve(null);
                    }
                }
            }, 500);
        });
    }

    function fetchPracticeData(uid) {
        return new Promise((resolve, reject) => {
            const url = `https://www.luogu.com.cn/user/${uid}/practice`;
            GM_xmlhttpRequest({
                method: 'GET',
                url: url,
                onload: function(res) {
                    try {
                        const html = res.responseText;
                        const match = html.match(/<script id="lentille-context"[^>]*>([\s\S]*?)<\/script>/);
                        if (!match) {
                            reject(new Error('无法解析个人练习页面'));
                            return;
                        }
                        const json = JSON.parse(match[1]);
                        const acceptedPids = [];
                        const submittedPids = [];
                        if (json.data && json.data.passed) {
                            json.data.passed.forEach(item => acceptedPids.push(item.pid));
                        }
                        if (json.data && json.data.submitted) {
                            json.data.submitted.forEach(item => submittedPids.push(item.pid));
                        }
                        resolve({ acceptedPids, submittedPids });
                    } catch (e) {
                        reject(e);
                    }
                },
                onerror: function(err) {
                    reject(err);
                }
            });
        });
    }

    function updateUserSets(uid, acceptedPids, submittedPids) {
        userUid = uid;
        acceptedSet = new Set(acceptedPids);
        submittedSet = new Set([...acceptedPids, ...submittedPids]);
        userStatusReady = true;
        console.log(`【洛谷随机跳题】状态已更新:AC ${acceptedSet.size} 题,尝试 ${submittedSet.size} 题`);
    }

    async function refreshUserStatus(showMsg = true) {
        const uid = getUidFromDOM();
        if (!uid) throw new Error('无法获取用户 UID');
        if (showMsg) showStatusMessage('正在刷新做题记录...');
        const { acceptedPids, submittedPids } = await fetchPracticeData(uid);
        updateUserSets(uid, acceptedPids, submittedPids);
        await saveUserStatus(uid, acceptedPids, submittedPids);
        if (showMsg) showStatusMessage('做题记录已刷新!');
        if (panelUpdateFilterUI) panelUpdateFilterUI();
    }

    async function initUserStatus(uid) {
        if (!uid) return false;

        const cached = await loadUserStatus();
        if (cached && cached.uid === uid) {
            updateUserSets(uid, cached.acceptedPids, cached.submittedPids);
            console.log('【洛谷随机跳题】已从缓存加载做题记录');
            return true;
        }

        console.log('【洛谷随机跳题】无有效缓存,尝试自动获取做题记录...');
        try {
            const { acceptedPids, submittedPids } = await fetchPracticeData(uid);
            updateUserSets(uid, acceptedPids, submittedPids);
            await saveUserStatus(uid, acceptedPids, submittedPids);
            return true;
        } catch (e) {
            console.warn('自动获取做题记录失败', e);
            acceptedSet = new Set();
            submittedSet = new Set();
            userStatusReady = false;
            return false;
        }
    }

    async function getRandomProblem(filters) {
        const db = await openDB();
        return new Promise((resolve, reject) => {
            const tx = db.transaction(STORE_NAME, 'readonly');
            const store = tx.objectStore(STORE_NAME);
            const req = store.getAll();
            req.onsuccess = function() {
                let results = req.result;
                const totalBefore = results.length;

                if (filters.difficulties && filters.difficulties.size > 0) {
                    results = results.filter(p => filters.difficulties.has(p.difficulty));
                }
                if (filters.types && filters.types.size > 0) {
                    results = results.filter(p => filters.types.has(p.type));
                }
                if (filters.tags) {
                    const tagNames = [];
                    DIMENSION_KEYS.forEach(key => {
                        const ids = filters.tags[key] || new Set();
                        const map = dimensionMaps[key] || {};
                        for (const id of ids) {
                            for (const [name, val] of Object.entries(map)) {
                                if (val === id) {
                                    tagNames.push(name);
                                    break;
                                }
                            }
                        }
                    });
                    if (tagNames.length > 0) {
                        results = results.filter(p => {
                            if (!p.tags || p.tags.length === 0) return false;
                            return p.tags.some(t => tagNames.includes(t));
                        });
                    }
                }

                if (filters.filterAccepted && userStatusReady) {
                    results = results.filter(p => !acceptedSet.has(p.pid));
                }
                if (filters.filterSubmitted && userStatusReady) {
                    results = results.filter(p => !submittedSet.has(p.pid));
                }

                console.log(`【洛谷随机跳题】筛选前 ${totalBefore} 题 → 筛选后 ${results.length} 题`,
                    filters.filterAccepted ? `(排除AC ${acceptedSet.size})` : '',
                    filters.filterSubmitted ? `(排除尝试 ${submittedSet.size})` : '');

                if (results.length === 0) {
                    resolve(null);
                } else {
                    const random = results[Math.floor(Math.random() * results.length)];
                    resolve(random);
                }
            };
            req.onerror = () => reject(req.error);
        });
    }

    let panelUpdateFilterUI = null;

    function createFloatingPanel(button) {
        const panel = document.createElement('div');
        panel.style.cssText = `
            position: absolute;
            bottom: calc(100% + 10px);
            right: 0;
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0,0,0,0.08);
            padding: 16px 18px;
            width: 300px;
            max-height: 480px;
            overflow-y: auto;
            border: 1px solid #eee;
            z-index: 10000;
            display: none;
            font-size: 13px;
            line-height: 1.5;
            color: #333;
        `;

        const typeTitle = document.createElement('div');
        typeTitle.textContent = '题库';
        typeTitle.style.cssText = 'font-weight:600; font-size:14px; margin-bottom:8px; color:#111;';
        panel.appendChild(typeTitle);

        const typeContainer = document.createElement('div');
        typeContainer.style.cssText = 'display:grid; grid-template-columns:1fr 1fr; gap:4px 10px; margin-bottom:16px;';
        TYPE_KEYS.forEach(displayName => {
            const typeValue = TYPE_MAP[displayName];
            const labelEl = document.createElement('label');
            labelEl.style.cssText = 'display:flex; align-items:center; padding:3px 6px; cursor:pointer; border-radius:6px; transition:background 0.15s;';
            labelEl.onmouseenter = () => { labelEl.style.background = '#f5f7fa'; };
            labelEl.onmouseleave = () => { labelEl.style.background = 'transparent'; };

            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.value = typeValue;
            checkbox.style.cssText = 'margin-right:6px; width:15px; height:15px; accent-color:#4f8ef7; cursor:pointer;';
            checkbox.checked = selectedTypes.has(typeValue);
            checkbox.addEventListener('change', function() {
                const val = this.value;
                if (this.checked) selectedTypes.add(val);
                else selectedTypes.delete(val);
                saveSettings();
            });

            const span = document.createElement('span');
            span.textContent = displayName;
            span.style.fontSize = '12px';

            labelEl.appendChild(checkbox);
            labelEl.appendChild(span);
            typeContainer.appendChild(labelEl);
        });
        panel.appendChild(typeContainer);

        const diffTitle = document.createElement('div');
        diffTitle.textContent = '难度';
        diffTitle.style.cssText = 'font-weight:600; font-size:14px; margin-bottom:8px; color:#111;';
        panel.appendChild(diffTitle);

        const diffContainer = document.createElement('div');
        diffContainer.style.cssText = 'display:grid; grid-template-columns:1fr 1fr; gap:4px 10px; margin-bottom:16px;';
        Object.entries(DIFFICULTY_MAP).forEach(([value, label]) => {
            const labelEl = document.createElement('label');
            labelEl.style.cssText = 'display:flex; align-items:center; padding:3px 6px; cursor:pointer; border-radius:6px; transition:background 0.15s;';
            labelEl.onmouseenter = () => { labelEl.style.background = '#f5f7fa'; };
            labelEl.onmouseleave = () => { labelEl.style.background = 'transparent'; };

            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.value = value;
            checkbox.style.cssText = 'margin-right:6px; width:15px; height:15px; accent-color:#4f8ef7; cursor:pointer;';
            checkbox.checked = selectedDifficulties.has(parseInt(value));
            checkbox.addEventListener('change', function() {
                const val = parseInt(this.value);
                if (this.checked) selectedDifficulties.add(val);
                else selectedDifficulties.delete(val);
                saveSettings();
            });

            const span = document.createElement('span');
            span.textContent = label;
            span.style.fontSize = '12px';

            labelEl.appendChild(checkbox);
            labelEl.appendChild(span);
            diffContainer.appendChild(labelEl);
        });
        panel.appendChild(diffContainer);

        const filterTitle = document.createElement('div');
        filterTitle.textContent = '筛选';
        filterTitle.style.cssText = 'font-weight:600; font-size:14px; margin-bottom:8px; color:#111;';
        panel.appendChild(filterTitle);

        const filterContainer = document.createElement('div');
        filterContainer.style.cssText = 'display:flex; flex-direction:column; gap:6px; margin-bottom:16px;';

        const tip = document.createElement('div');
        tip.className = 'filter-status-tip';
        tip.style.cssText = 'font-size:11px; color:#999; margin-top:-4px; margin-bottom:4px;';
        filterContainer.appendChild(tip);

        const stats = document.createElement('div');
        stats.className = 'filter-stats';
        stats.style.cssText = 'font-size:11px; color:#666; margin-bottom:4px;';
        filterContainer.appendChild(stats);

        function createFilterCheckbox(text, getter, setter) {
            const label = document.createElement('label');
            label.className = 'filter-checkbox';
            label.style.cssText = 'display:flex; align-items:center; padding:3px 6px; cursor:pointer; border-radius:6px; transition:background 0.15s;';
            label.onmouseenter = () => { label.style.background = '#f5f7fa'; };
            label.onmouseleave = () => { label.style.background = 'transparent'; };

            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.style.cssText = 'margin-right:6px; width:15px; height:15px; accent-color:#4f8ef7; cursor:pointer;';
            checkbox.checked = getter();
            checkbox.addEventListener('change', function() {
                setter(this.checked);
                saveSettings();
            });

            const span = document.createElement('span');
            span.textContent = text;
            span.style.fontSize = '12px';

            label.appendChild(checkbox);
            label.appendChild(span);
            return { label, checkbox };
        }

        const cb1 = createFilterCheckbox('仅显示未 AC', () => filterAccepted, val => { filterAccepted = val; });
        const cb2 = createFilterCheckbox('仅显示未尝试', () => filterSubmitted, val => { filterSubmitted = val; });
        filterContainer.appendChild(cb1.label);
        filterContainer.appendChild(cb2.label);

        function updateFilterUI() {
            const enabled = userUid !== null && userStatusReady;
            tip.textContent = enabled ? '(已登录,筛选可用)' : '(请先点击下方“刷新做题记录”按钮登录/刷新)';
            stats.textContent = enabled
                ? `已缓存 AC ${acceptedSet.size} 题,尝试 ${submittedSet.size} 题`
                : '暂无做题记录';

            [cb1, cb2].forEach(cb => {
                cb.checkbox.disabled = !enabled;
                if (!enabled) {
                    cb.checkbox.checked = false;
                    if (cb === cb1) filterAccepted = false;
                    if (cb === cb2) filterSubmitted = false;
                }
                cb.label.style.opacity = enabled ? '1' : '0.5';
                cb.label.style.cursor = enabled ? 'pointer' : 'not-allowed';
            });
        }
        updateFilterUI();
        panelUpdateFilterUI = updateFilterUI;

        panel.appendChild(filterContainer);

        const actionContainer = document.createElement('div');
        actionContainer.style.cssText = 'display:flex; gap:8px; margin-bottom:16px; flex-wrap:wrap;';

        const refreshBtn = document.createElement('button');
        refreshBtn.textContent = '刷新做题记录';
        refreshBtn.style.cssText = `
            flex:1; padding:4px 8px; font-size:12px; border:1px solid #ccc; border-radius:4px;
            background:#f8f8f8; cursor:pointer; transition:background 0.15s;
        `;
        refreshBtn.onmouseenter = () => { refreshBtn.style.background = '#e8e8e8'; };
        refreshBtn.onmouseleave = () => { refreshBtn.style.background = '#f8f8f8'; };
        refreshBtn.onclick = async function(e) {
            e.stopPropagation();
            try {
                await refreshUserStatus(true);
            } catch (err) {
                console.error(err);
                showStatusMessage('刷新失败:' + err.message, true);
            }
        };
        actionContainer.appendChild(refreshBtn);

        const updateBtn = document.createElement('button');
        updateBtn.textContent = '更新题库';
        updateBtn.style.cssText = `
            flex:1; padding:4px 8px; font-size:12px; border:1px solid #ccc; border-radius:4px;
            background:#f8f8f8; cursor:pointer; transition:background 0.15s;
        `;
        updateBtn.onmouseenter = () => { updateBtn.style.background = '#e8e8e8'; };
        updateBtn.onmouseleave = () => { updateBtn.style.background = '#f8f8f8'; };
        updateBtn.onclick = function(e) {
            e.stopPropagation();
            if (confirm('确定要重新下载并更新题库吗?这将覆盖本地缓存。')) {
                showStatusMessage('正在更新题库...');
                downloadAndImportData((current, total) => {
                    showStatusMessage(`更新进度: ${current}/${total}`);
                }).then(() => {
                    showStatusMessage('题库更新完成!');
                }).catch(err => {
                    console.error(err);
                    showStatusMessage('更新失败,请检查网络。', true);
                });
            }
        };
        actionContainer.appendChild(updateBtn);

        panel.appendChild(actionContainer);

        DIMENSION_KEYS.forEach(dimKey => {
            const dimName = DIMENSION_NAMES[dimKey] || dimKey;
            const map = dimensionMaps[dimKey] || {};
            const tagSet = selectedTags[dimKey];

            const section = document.createElement('div');
            section.style.cssText = 'margin-bottom:14px;';

            const header = document.createElement('div');
            header.textContent = dimName;
            header.style.cssText = 'font-weight:600; font-size:13px; color:#222; margin-bottom:4px;';
            section.appendChild(header);

            const selectedContainer = document.createElement('div');
            selectedContainer.style.cssText = 'display:flex; flex-wrap:wrap; gap:4px; margin-bottom:4px;';
            section.appendChild(selectedContainer);

            function updateSelectedDisplay() {
                selectedContainer.innerHTML = '';
                if (tagSet.size === 0) {
                    selectedContainer.style.display = 'none';
                    return;
                }
                selectedContainer.style.display = 'flex';
                tagSet.forEach(tagId => {
                    let name = null;
                    for (const [n, id] of Object.entries(map)) {
                        if (id === tagId) { name = n; break; }
                    }
                    const displayName = name || `ID:${tagId}`;
                    const tagEl = document.createElement('span');
                    tagEl.style.cssText = `
                        display:inline-flex; align-items:center;
                        background:#eef3ff; color:#2b5cd9;
                        padding:2px 10px; border-radius:14px;
                        font-size:11px; gap:4px;
                    `;
                    const nameSpan = document.createElement('span');
                    nameSpan.textContent = displayName;
                    const removeBtn = document.createElement('span');
                    removeBtn.textContent = '×';
                    removeBtn.style.cssText = 'cursor:pointer; font-weight:700; color:#7a8bb0; font-size:14px;';
                    removeBtn.onclick = (e) => {
                        e.stopPropagation();
                        tagSet.delete(tagId);
                        updateSelectedDisplay();
                        saveSettings();
                    };
                    tagEl.appendChild(nameSpan);
                    tagEl.appendChild(removeBtn);
                    selectedContainer.appendChild(tagEl);
                });
            }
            updateSelectedDisplay();

            const wrapper = document.createElement('div');
            wrapper.style.cssText = 'position:relative;';

            const input = document.createElement('input');
            input.type = 'text';
            input.placeholder = `搜索 ${dimName}`;
            input.style.cssText = `
                width:100%; padding:4px 10px;
                border:1px solid #ddd; border-radius:6px;
                font-size:12px; outline:none;
                transition:border 0.15s;
                box-sizing:border-box;
            `;
            input.onfocus = () => { input.style.borderColor = '#4f8ef7'; };
            input.onblur = () => { input.style.borderColor = '#ddd'; };
            wrapper.appendChild(input);

            const suggestionBox = document.createElement('div');
            suggestionBox.style.cssText = `
                position:absolute; top:100%; left:0; right:0;
                background:#fff; border:1px solid #ddd; border-top:none;
                border-radius:0 0 6px 6px;
                max-height:120px; overflow-y:auto;
                display:none; z-index:10001;
                box-shadow:0 4px 12px rgba(0,0,0,0.05);
            `;
            wrapper.appendChild(suggestionBox);

            input.addEventListener('input', function() {
                const query = this.value.trim().toLowerCase();
                if (!query) { suggestionBox.style.display = 'none'; return; }
                const matches = [];
                for (const [name, id] of Object.entries(map)) {
                    if (name.toLowerCase().includes(query)) {
                        if (!matches.some(m => m.id === id)) {
                            matches.push({ id, name });
                        }
                    }
                }
                if (matches.length === 0) { suggestionBox.style.display = 'none'; return; }
                suggestionBox.innerHTML = '';
                matches.slice(0, 10).forEach(({ id, name }) => {
                    const item = document.createElement('div');
                    item.style.cssText = `
                        padding:3px 10px; cursor:pointer; font-size:12px;
                        border-bottom:1px solid #f5f5f5;
                        display:flex; justify-content:space-between;
                        transition:background 0.1s;
                    `;
                    item.onmouseenter = () => { item.style.background = '#f5f7fa'; };
                    item.onmouseleave = () => { item.style.background = 'transparent'; };
                    item.addEventListener('mousedown', function(e) { e.preventDefault(); });
                    item.onclick = () => {
                        if (!tagSet.has(id)) {
                            tagSet.add(id);
                            updateSelectedDisplay();
                            saveSettings();
                        }
                        input.value = '';
                        suggestionBox.style.display = 'none';
                    };
                    const nameSpan = document.createElement('span');
                    nameSpan.textContent = name;
                    const idSpan = document.createElement('span');
                    idSpan.textContent = `ID:${id}`;
                    idSpan.style.cssText = 'color:#aaa; font-size:10px;';
                    item.appendChild(nameSpan);
                    item.appendChild(idSpan);
                    suggestionBox.appendChild(item);
                });
                suggestionBox.style.display = 'block';
            });

            input.addEventListener('blur', function() {
                setTimeout(() => { suggestionBox.style.display = 'none'; }, 200);
            });

            section.appendChild(wrapper);

            const clearBtn = document.createElement('button');
            clearBtn.textContent = '清空';
            clearBtn.style.cssText = `
                font-size:11px; color:#e55c5c; background:none; border:none;
                cursor:pointer; padding:2px 6px; margin-top:4px;
                border-radius:4px; transition:background 0.15s;
            `;
            clearBtn.onmouseenter = () => { clearBtn.style.background = '#fef0f0'; };
            clearBtn.onmouseleave = () => { clearBtn.style.background = 'transparent'; };
            clearBtn.onclick = () => {
                tagSet.clear();
                updateSelectedDisplay();
                saveSettings();
            };
            section.appendChild(clearBtn);

            panel.appendChild(section);
        });

        let hoverTimeout = null;
        button.addEventListener('mouseenter', function() {
            clearTimeout(hoverTimeout);
            panel.style.display = 'block';
            requestAnimationFrame(() => {
                const rect = button.getBoundingClientRect();
                const panelRect = panel.getBoundingClientRect();
                if (rect.top - panelRect.height < 10) {
                    panel.style.bottom = 'auto';
                    panel.style.top = 'calc(100% + 10px)';
                } else {
                    panel.style.bottom = 'calc(100% + 10px)';
                    panel.style.top = 'auto';
                }
            });
        });

        button.addEventListener('mouseleave', function(e) {
            hoverTimeout = setTimeout(() => {
                const related = e.relatedTarget;
                if (related && (panel.contains(related) || button.contains(related))) {
                    return;
                }
                panel.style.display = 'none';
            }, 200);
        });

        panel.addEventListener('mouseenter', function() {
            clearTimeout(hoverTimeout);
            panel.style.display = 'block';
        });

        panel.addEventListener('mouseleave', function(e) {
            const related = e.relatedTarget;
            if (related && (panel.contains(related) || button.contains(related))) {
                return;
            }
            panel.style.display = 'none';
        });

        const parent = button.parentNode;
        parent.style.position = 'relative';
        parent.appendChild(panel);

        return panel;
    }

    async function startRandomJump() {
        if ((filterAccepted || filterSubmitted) && !userStatusReady) {
            if (confirm('您启用了 AC/尝试筛选,但尚未刷新做题记录。是否立即刷新?')) {
                try {
                    await refreshUserStatus(true);
                } catch (err) {
                    console.error(err);
                    showStatusMessage('获取失败,请手动点击“刷新做题记录”按钮。', true);
                    return;
                }
            } else {
                const tempAccepted = filterAccepted;
                const tempSubmitted = filterSubmitted;
                filterAccepted = false;
                filterSubmitted = false;
                await doRandomJump();
                filterAccepted = tempAccepted;
                filterSubmitted = tempSubmitted;
                return;
            }
        }
        await doRandomJump();
    }

    async function doRandomJump() {
        let diffArray;
        if (selectedDifficulties.size === 0) {
            diffArray = Object.keys(DIFFICULTY_MAP).map(Number);
        } else {
            diffArray = Array.from(selectedDifficulties);
        }
        let typeArray;
        if (selectedTypes.size === 0) {
            typeArray = Object.values(TYPE_MAP);
        } else {
            typeArray = Array.from(selectedTypes);
        }

        const filters = {
            difficulties: new Set(diffArray),
            types: new Set(typeArray),
            tags: selectedTags,
            filterAccepted: filterAccepted && userStatusReady,
            filterSubmitted: filterSubmitted && userStatusReady
        };

        try {
            const problem = await getRandomProblem(filters);
            if (!problem) {
                alert('没有找到符合条件的题目,请调整筛选条件。');
                return;
            }
            window.open(`https://www.luogu.com.cn/problem/${problem.pid}`, '_blank');
        } catch (err) {
            console.error(err);
            alert('查询失败,请重试。');
        }
    }

    function showStatusMessage(text, isError = false) {
        const existing = document.getElementById('luogu-offline-status');
        if (existing) existing.remove();
        const div = document.createElement('div');
        div.id = 'luogu-offline-status';
        div.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            background: ${isError ? '#f44336' : '#2196F3'};
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            z-index: 99999;
            font-size: 14px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.2);
            max-width: 300px;
            word-wrap: break-word;
        `;
        div.textContent = text;
        document.body.appendChild(div);
        if (!isError) {
            setTimeout(() => { if (div.parentNode) div.remove(); }, 5000);
        }
    }

    function downloadAndImportData(progressCallback) {
        return new Promise((resolve, reject) => {
            if (typeof pako === 'undefined') {
                reject(new Error('pako 库未加载,请检查网络或刷新页面重试。'));
                return;
            }
            clearProblems().then(() => {
                GM_xmlhttpRequest({
                    method: 'GET',
                    url: DATA_URL,
                    responseType: 'arraybuffer',
                    onload: function(res) {
                        try {
                            const compressed = new Uint8Array(res.response);
                            const decompressed = pako.ungzip(compressed, { to: 'string' });
                            const lines = decompressed.split('\n').filter(line => line.trim() !== '');
                            const total = lines.length;
                            const batchSize = 1000;
                            let imported = 0;

                            function importBatch(start) {
                                const end = Math.min(start + batchSize, total);
                                const batch = [];
                                for (let i = start; i < end; i++) {
                                    try {
                                        const obj = JSON.parse(lines[i]);
                                        batch.push({
                                            pid: obj.pid,
                                            title: obj.title,
                                            difficulty: obj.difficulty,
                                            type: obj.type,
                                            tags: obj.tags || []
                                        });
                                    } catch (e) { /* ignore */ }
                                }
                                if (batch.length === 0) {
                                    if (end < total) importBatch(end);
                                    else resolve();
                                    return;
                                }
                                bulkInsert(batch).then(() => {
                                    imported += batch.length;
                                    if (progressCallback) progressCallback(imported, total);
                                    if (end < total) {
                                        importBatch(end);
                                    } else {
                                        setMetadata('data_version', new Date().toISOString()).then(() => resolve());
                                    }
                                }).catch(err => reject(err));
                            }

                            importBatch(0);
                        } catch (e) {
                            reject(e);
                        }
                    },
                    onerror: function(err) {
                        reject(err);
                    }
                });
            }).catch(err => reject(err));
        });
    }

    async function init() {
        if (typeof pako === 'undefined') {
            console.warn('pako 库尚未加载,等待...');
            const checkPako = setInterval(() => {
                if (typeof pako !== 'undefined') {
                    clearInterval(checkPako);
                    initCore();
                }
            }, 500);
            return;
        }
        await initCore();
    }

    async function initCore() {
        setupUI();
        console.log('【洛谷随机跳题】UI 已创建,等待用户状态初始化...');

        waitForUid().then(async (uid) => {
            if (uid) {
                const success = await initUserStatus(uid);
                if (success) {
                    console.log('【洛谷随机跳题】做题记录已就绪');
                    if (panelUpdateFilterUI) panelUpdateFilterUI();
                } else {
                    console.log('【洛谷随机跳题】做题记录未就绪,您可手动点击“刷新做题记录”');
                }
            } else {
                console.log('【洛谷随机跳题】未能获取用户 UID,筛选功能暂不可用');
            }
        });
    }

    function setupUI() {
        const btn = document.querySelector('[name="gotorandom"]');
        if (!btn) {
            console.warn('未找到随机跳题按钮');
            return;
        }

        const parent = btn.parentNode;
        const newBtn = document.createElement('button');
        newBtn.className = btn.className;
        newBtn.textContent = btn.textContent;
        newBtn.style.cssText = btn.style.cssText;
        for (const attr of btn.attributes) {
            if (attr.name !== 'name') {
                newBtn.setAttribute(attr.name, attr.value);
            }
        }
        newBtn.setAttribute('name', 'gotorandom-custom');
        parent.replaceChild(newBtn, btn);

        try {
            createFloatingPanel(newBtn);
        } catch (e) {
            console.error('创建面板失败:', e);
            showStatusMessage('UI 初始化失败,请刷新页面重试。', true);
            return;
        }

        newBtn.addEventListener('click', async function(e) {
            e.preventDefault();
            try {
                const count = await getProblemCount();
                if (count === 0) {
                    showStatusMessage('正在下载题库数据,请稍候...');
                    await downloadAndImportData((current, total) => {
                        showStatusMessage(`导入进度: ${current}/${total}`);
                    });
                    showStatusMessage('题库数据导入完成!');
                }
                await startRandomJump();
            } catch (err) {
                console.error(err);
                showStatusMessage('发生错误,请刷新重试。', true);
            }
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();