洛谷题目题解通道开放提醒

· · 个人记录

  1. 安装油猴插件(Tampermonkey)后,创建新脚本并将上述代码粘贴进去
  2. 访问洛谷网站,点击油猴插件图标,选择"添加题目到监控列表"来添加你想监控的题目ID(如P14255)
  3. 你可以通过"查看/管理监控列表"来修改已添加的题目
  4. 当你访问洛谷首页时,插件会自动检查所有监控的题目
  5. 如果有题目开放了题解,会在页面右上角显示提示框,包含所有题目的状态
// ==UserScript==
// @name         洛谷题解开放提醒
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  监控指定洛谷题目是否开放题解,并在首页给出提示
// @author       豆包编程助手
// @match        https://www.luogu.com.cn/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    // 存储需要监控的题目列表
    let watchedProblems = GM_getValue('watchedProblems', []);

    // 添加菜单命令,用于管理监控列表
    GM_registerMenuCommand("添加题目到监控列表", addProblem);
    GM_registerMenuCommand("查看/管理监控列表", manageProblems);

    // 在首页显示提示
    if (window.location.pathname === '/' || window.location.pathname === '/home') {
        checkAllProblems();
    }

    // 添加题目到监控列表
    function addProblem() {
        const problemId = prompt("请输入要监控的题目ID(如P1234):");
        if (!problemId) return;

        if (!watchedProblems.includes(problemId)) {
            watchedProblems.push(problemId);
            GM_setValue('watchedProblems', watchedProblems);
            alert(`已添加 ${problemId} 到监控列表`);
        } else {
            alert(`${problemId} 已在监控列表中`);
        }
    }

    // 管理监控列表
    function manageProblems() {
        if (watchedProblems.length === 0) {
            alert("监控列表为空,点击'添加题目到监控列表'添加题目");
            return;
        }

        const problemList = watchedProblems.join('\n');
        const newList = prompt("当前监控列表(每行一个题目ID):", problemList);

        if (newList !== null) {
            // 处理新列表,过滤空行和重复项
            const newProblems = [...new Set(
                newList.split('\n')
                    .map(id => id.trim())
                    .filter(id => id)
            )];

            watchedProblems = newProblems;
            GM_setValue('watchedProblems', watchedProblems);
            alert(`已更新监控列表,共 ${watchedProblems.length} 个题目`);
        }
    }

    // 检查所有监控的题目
    function checkAllProblems() {
        if (watchedProblems.length === 0) return;

        const results = [];

        watchedProblems.forEach(problemId => {
            checkProblemSolution(problemId)
                .then(hasSolution => {
                    results.push({
                        id: problemId,
                        hasSolution: hasSolution
                    });

                    // 所有请求完成后显示结果
                    if (results.length === watchedProblems.length) {
                        showResults(results);
                    }
                })
                .catch(err => {
                    console.error(`检查 ${problemId} 时出错:`, err);
                });
        });
    }

    // 检查单个题目的题解是否开放
    function checkProblemSolution(problemId) {
        return new Promise((resolve, reject) => {
            // 构造题解页面URL
            const url = `https://www.luogu.com.cn/problem/solution/${problemId}`;

            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function(response) {
                    // 检查响应内容判断是否有题解
                    // 这里通过检查页面中是否存在题解内容区域来判断
                    const hasSolution = response.responseText.includes('"solutions":') &&
                                      !response.responseText.includes('暂无题解');
                    resolve(hasSolution);
                },
                onerror: function(err) {
                    reject(err);
                }
            });
        });
    }

    // 显示检查结果
    function showResults(results) {
        // 创建提示容器
        const notification = document.createElement('div');
        notification.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            background: #fff;
            border: 2px solid #4CAF50;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            z-index: 9999;
            max-width: 300px;
        `;

        // 添加标题
        const title = document.createElement('h3');
        title.textContent = '题解监控提醒';
        title.style.marginTop = '0';
        title.style.color = '#333';
        notification.appendChild(title);

        // 添加结果列表
        const list = document.createElement('ul');
        list.style.paddingLeft = '20px';

        results.forEach(item => {
            const li = document.createElement('li');
            li.innerHTML = `${item.id}: ${item.hasSolution ?
                '<span style="color: green;">题解已开放 ✅</span>' :
                '<span style="color: #999;">题解未开放</span>'}`;

            // 添加链接
            const link = document.createElement('a');
            link.href = `https://www.luogu.com.cn/problem/${item.id}`;
            link.target = '_blank';
            link.textContent = item.id;
            link.style.textDecoration = 'none';
            link.style.color = item.hasSolution ? '#4CAF50' : '#333';

            // 替换文本为链接
            li.replaceChild(link, li.firstChild);
            list.appendChild(li);
        });

        notification.appendChild(list);

        // 添加关闭按钮
        const closeBtn = document.createElement('button');
        closeBtn.textContent = '关闭';
        closeBtn.style.cssText = `
            margin-top: 10px;
            padding: 5px 10px;
            background: #f5f5f5;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
        `;
        closeBtn.onclick = () => notification.remove();
        notification.appendChild(closeBtn);

        // 添加到页面
        document.body.appendChild(notification);
    }
})();