LeetCode 灵神题单状态标记

请求用户所有题目的状态,匹配HTML页面上相应的题目超链接进行展示,适用于灵神的讨论帖题单场景(也可用于其他题单)

Versione datata 08/05/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         LeetCode 灵神题单状态标记
// @namespace    https://github.com/qk-antares
// @version      0.1
// @description  请求用户所有题目的状态,匹配HTML页面上相应的题目超链接进行展示,适用于灵神的讨论帖题单场景(也可用于其他题单)
// @author       qk-antares
// @match        https://leetcode.cn/discuss/post/*
// @grant        GM_xmlhttpRequest
// @connect      leetcode.cn
// @icon         data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNzQ2NjcxNzM5NTU1IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIi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DEtNDIuOTQtMTAuMTR6TTU0NCA1ODguMzZsNDcuOTIgMTAzLjgyLTc3Ljg3LTM4Ljkzek04ODQuNDcgODk2TDYzNy4wNSAzNTkuOTJhMzIgMzIgMCAwIDAtNTgtMC4ybC0wLjEgMC4yLTEyMi4xNyAyNjQuNjktNDYuNzMtMjMuMzZMNjA4IDE3Mi4zNiA5NDIgODk2eiIgZmlsbD0iI0ZGN0E1OCIgcC1pZD0iNjcwMyI+PC9wYXRoPjwvc3ZnPg==
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    console.log("🚀 [题单状态标记] 脚本启动");

    function fetchAllProblemStatuses() {
        return new Promise((resolve) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: "https://leetcode.cn/api/problems/all/",
                onload: function (res) {
                    try {
                        const json = JSON.parse(res.responseText);
                        const map = new Map();
                        json.stat_status_pairs.forEach(item => {
                            const slug = item.stat.question__title_slug;
                            const status = item.status; // "ac", "notac", or null
                            map.set(slug, status);
                        });
                        console.log(`✅ 共获取 ${map.size} 道题目的状态`);
                        resolve(map);
                    } catch (e) {
                        console.error("❌ 解析题目状态失败", e);
                        resolve(new Map());
                    }
                },
                onerror: function (err) {
                    console.error("❌ 请求题目状态失败", err);
                    resolve(new Map());
                }
            });
        });
    }

    function getStatusIcon(status) {
        switch (status) {
            case 'ac': return '✅';
            case 'notac': return '❌';
            case null: return '🕓';
            default: return '❓';
        }
    }

    function updatePageWithStatus(map) {
        const links = document.querySelectorAll("ul li > a[href*='/problems/']");
        console.log(`🔍 开始处理 ${links.length} 个题目超链接`);
        links.forEach(link => {
            const match = link.href.match(/problems\/([^/]+)\//);
            if (!match) return;

            const slug = match[1];
            const status = map.get(slug) ?? null;
            const icon = getStatusIcon(status);

            const span = document.createElement('span');
            span.textContent = icon + ' ';
            link.parentNode.insertBefore(span, link);
        });
    }

    // 启动
    fetchAllProblemStatuses().then(statusMap => {
        updatePageWithStatus(statusMap);
    });
})();