跳到正文

科技·工程

洛谷灌水清理器

AutumnMoon约 55 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月18日
篇幅
约 55 分钟

版本

  1. v2当前
  2. v1

准备工作

首先,你需要安装 Microsoft Edge 浏览器或 夸克浏览器。

然后,安装暴力猴.

该插件能够实现的功能

  • 隐藏主页讨论区
  • 隐藏主页犇犇(不管开没开学术模式都发不了犇犇)
  • 隐藏权限更变记录
  • 隐藏能够设置学术模式的开关
  • 隐藏主页的关注、粉丝
  • 隐藏左侧栏的应用
  • 隐藏左侧栏的讨论区
  • 隐藏右上角的私信+铃铛

源码

代码
// ==UserScript==
// @name         洛谷综合清理器
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  综合删除洛谷的犇犇、铃铛、私信、讨论等元素,保护重要功能
// @match        http://www.luogu.com.cn/*
// @match        https://www.luogu.com.cn/*
// @author       Sweet_2013
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    console.log('洛谷综合清理器启动');

    // 配置:父容器选择器,直接删除整个父容器
    const parentContainerConfig = {
        // 通知铃铛 - 删除整个图标按钮容器
        bell: [
            'a[href="/user/notification"]',
            'svg[data-icon="bell"]',
            '.icon-btn[href*="notification"]'
        ],

        // 私信/邮件 - 删除整个图标按钮容器
        message: [
            'a[href="/chat"]',
            'svg[data-icon="envelope"]',
            '.icon-btn[href*="chat"]'
        ],

        // 应用按钮 - 删除整个弹出按钮容器
        app: [
            '.popup-button',
            'svg[data-icon="angles-right"]'
        ],

        // 讨论 - 删除整个讨论链接容器
        discuss: [
            'a[href="/discuss"]',
            'svg[data-icon="comments"]',
            '.icon-btn[href*="discuss"]',
            'a[href="/discuss"] .text' // 新增:讨论文本
        ],

        // 犇犇相关 - 直接删除整个犇犇容器
        feed: [
            '#feed-content',
            '#feed-submit',
            '#emo-lst',
            '.feed-selector',
            '.feed-item', // 新增:犇犇项
            '[class*="feed"]' // 新增:所有包含feed的类
        ],

        // 用户信息 - 只删除关注和粉丝,保护提交数和通过数
        user: [
            'a[href*="/following"]',
            'a[href*="/follower"]'
        ],

        // 设置相关 - 删除学术模式和权限记录
        settings: [
            '#LRadio-1',
            '#LRadio-2'
        ],

        // 个人主页白色栏 - 关注和动态
        profileTabs: [
            'a[href*="following"]',
            'a[href*="follower"]',
            '.item[href*="feed"]',
            '.tab[href*="feed"]'
        ]
    };

    // 配置:需要保护的重要元素
    const protectedConfig = {
        // 用户统计数据 - 保护提交数和通过数
        userStats: [
            '[class*="score"]', // 等级分
            '[class*="accepted"]', // 通过数
            '[class*="pass"]', // 通过率
            '[class*="rank"]', // 排名
            '[class*="submit"]' // 提交数 - 现在保护提交数
        ],

        // 左侧栏重要功能
        sidebarFunctions: [
            'a[href="/"]', // 回到主页
            'a[href*="/problem"]', // 题库
            'a[href*="/contest"]', // 比赛
            'a[href*="/training"]', // 训练
            'a[href*="/record"]', // 评测记录
            'a[href*="/article"]', // 专栏
            'a[href*="/blog"]' // 博客
        ],

        // 题目页面重要信息
        problemInfo: [
            '.problem-info', // 题目信息容器
            '.info-item', // 信息项
            '.statics', // 统计信息
            '.data-range', // 数据范围
            '.memory-limit', // 空间限制
            '.time-limit', // 时间限制
            '.sample', // 样例
            '.input', // 输入
            '.output', // 输出
            '.problem-content' // 题目内容
        ]
    };

    // 检查是否在编辑器页面
    function isEditorPage() {
        return window.location.href.includes('/article/edit') ||
               window.location.href.includes('/blog/edit') ||
               document.querySelector('.editor-container') ||
               document.querySelector('.markdown-editor') ||
               document.querySelector('[class*="editor"]') ||
               document.querySelector('#title') && document.querySelector('#content');
    }

    // 检查是否在专栏页面
    function isArticlePage() {
        return window.location.href.includes('/article/') ||
               window.location.href.includes('/blog/') ||
               document.querySelector('.article-content') ||
               document.querySelector('.blog-content');
    }

    // 检查是否在题目页面
    function isProblemPage() {
        return window.location.href.includes('/problem/') ||
               document.querySelector('.problem-content') ||
               document.querySelector('.problem-info') ||
               document.querySelector('.sample-container');
    }

    // 检查是否在个人主页
    function isUserProfilePage() {
        return window.location.pathname.match(/^\/user\/\d+$/);
    }

    // 检查元素是否需要保护
    function isProtectedElement(element) {
        // 如果在题目页面,保护所有题目相关元素
        if (isProblemPage()) {
            const problemSelectors = protectedConfig.problemInfo.flat();
            for (let selector of problemSelectors) {
                if (element.matches(selector) || element.closest(selector)) {
                    return true;
                }
            }

            // 检查是否在题目内容区域内
            if (element.closest('.problem-content') ||
                element.closest('.sample-container') ||
                element.closest('.problem-sidebar')) {
                return true;
            }
        }

        // 检查用户统计数据
        const userStatsSelectors = protectedConfig.userStats.flat();
        for (let selector of userStatsSelectors) {
            if (element.matches(selector) || element.closest(selector)) {
                return true;
            }
        }

        // 检查左侧栏重要功能
        const sidebarSelectors = protectedConfig.sidebarFunctions.flat();
        for (let selector of sidebarSelectors) {
            if (element.matches(selector) || element.closest(selector)) {
                return true;
            }
        }

        // 检查文本内容是否包含重要功能
        const text = element.textContent || '';
        const protectedTexts = [
            '提交数', '通过数', '等级分', '通过率', '排名',
            '题库', '题目', '比赛', '训练',
            '专栏', '博客', '评测记录', '记录',
            '主页', '首页',
            '时间限制', '空间限制', '内存限制', '数据范围',
            '输入', '输出', '样例', '题目描述'
        ];

        for (let protectedText of protectedTexts) {
            if (text.includes(protectedText)) {
                return true;
            }
        }

        return false;
    }

    // 获取父容器
    function getParentContainer(element) {
        // 优先查找常见的父容器类型
        const parentContainers = [
            element.closest('.icon-btn'), // 图标按钮
            element.closest('.entry'), // 导航项
            element.closest('.card'), // 卡片
            element.closest('.am-panel'), // 面板
            element.closest('.panel'), // 面板
            element.closest('.l-form-layout'), // 设置布局
            element.closest('.field'), // 字段容器
            element.closest('.lg-article'), // 文章容器
            element.closest('section'), // 区域
            element.closest('li'), // 列表项
            element.closest('.nav-item'), // 导航项
            element.closest('.btn-group'), // 按钮组
            element.parentElement // 直接父元素
        ];

        for (let container of parentContainers) {
            if (container && container !== document.body) {
                // 检查父容器是否需要保护
                if (!isProtectedElement(container)) {
                    return container;
                }
            }
        }
        return element.parentElement;
    }

    // 批量删除父容器
    function removeParentContainers() {
        // 如果在编辑器或专栏页面,不执行删除
        if (isEditorPage() || isArticlePage()) {
            console.log('在编辑器或专栏页面,跳过删除');
            return;
        }

        console.log('开始批量删除父容器...');

        let removedCount = 0;

        // 遍历所有配置的选择器
        Object.values(parentContainerConfig).flat().forEach(selector => {
            try {
                document.querySelectorAll(selector).forEach(element => {
                    if (element && element.parentNode) {
                        // 检查元素是否在安全删除区域且不需要保护
                        if (isInSafeRemovalArea(element) && !isProtectedElement(element)) {
                            const parentContainer = getParentContainer(element);

                            if (parentContainer && parentContainer.parentNode && !isProtectedElement(parentContainer)) {
                                console.log('删除父容器:', selector, parentContainer);
                                parentContainer.remove();
                                removedCount++;
                            }
                        }
                    }
                });
            } catch (e) {
                // 忽略选择器错误
            }
        });

        console.log(`本次删除了 ${removedCount} 个父容器`);

        // 特殊处理:删除整个犇犇区域(包括别人的犇犇)
        removeFeedAreaCompletely();

        // 特殊处理:彻底删除讨论区域
        removeDiscussionAreaCompletely();

        // 特殊处理:删除设置项(学术模式和权限记录)
        removeSettingsItems();

        // 特殊处理:删除个人主页白色栏的关注和动态
        removeProfileTabs();

        // 特殊处理:删除左侧栏讨论链接
        removeSidebarDiscussion();

        // 特殊处理:删除讨论文本链接
        removeDiscussionTextLinks();

        // 特殊处理:删除权限变更记录
        removePermissionChangeRecord();
    }

    // 彻底删除犇犇区域(包括别人的犇犇)
    function removeFeedAreaCompletely() {
        if (window.location.pathname === '/' || window.location.pathname === '/index') {
            console.log('开始彻底删除犇犇区域...');

            // 方法1: 删除所有犇犇相关容器
            const feedContainers = [
                document.querySelector('#feed-content'),
                document.querySelector('.lg-article'),
                document.querySelector('[class*="feed"]')
            ];

            feedContainers.forEach(container => {
                if (container && isInSafeRemovalArea(container) && !isProtectedElement(container)) {
                    console.log('删除犇犇容器:', container);
                    container.remove();
                }
            });

            // 方法2: 删除所有犇犇项
            const feedItems = document.querySelectorAll('.feed-item, [class*="feed"]');
            feedItems.forEach(item => {
                if (item && isInSafeRemovalArea(item) && !isProtectedElement(item)) {
                    console.log('删除犇犇项:', item);
                    item.remove();
                }
            });

            // 方法3: 通过文本内容识别犇犇
            document.querySelectorAll('div, section, article').forEach(element => {
                if (element && element.parentNode && isInSafeRemovalArea(element) && !isProtectedElement(element)) {
                    const text = element.textContent || '';
                    if (text.includes('犇犇') || text.includes('新鲜事') ||
                        text.includes('发射') || (text.includes('@') && text.includes(':'))) {
                        console.log('通过文本识别删除犇犇:', element);
                        element.remove();
                    }
                }
            });
        }
    }

    // 彻底删除讨论区域
    function removeDiscussionAreaCompletely() {
        // 只在首页删除讨论版
        if (window.location.pathname === '/' || window.location.pathname === '/index') {
            console.log('开始彻底删除讨论区域...');

            // 方法1: 删除包含"最近讨论"标题的整个容器
            const discussionTitles = document.querySelectorAll('h2, h3, .title, .header');
            discussionTitles.forEach(title => {
                if (title && title.textContent.includes('最近讨论')) {
                    const container = title.closest('.lg-article') ||
                                    title.closest('.card') ||
                                    title.closest('.am-panel') ||
                                    title.closest('section') ||
                                    title.parentElement;
                    if (container && isInSafeRemovalArea(container) && !isProtectedElement(container)) {
                        console.log('删除讨论标题容器:', container);
                        container.remove();
                    }
                }
            });

            // 方法2: 删除所有讨论帖子
            const discussionPosts = document.querySelectorAll('.am-panel, section, .lg-index-contest');
            discussionPosts.forEach(post => {
                if (post && isInSafeRemovalArea(post) && !isProtectedElement(post)) {
                    const text = post.textContent || '';
                    const html = post.innerHTML || '';
                    // 检查是否是讨论帖子
                    if (text.includes('回复') || text.includes('讨论') ||
                        html.includes('/discuss/') || html.includes('lg-index-contest')) {
                        console.log('删除讨论帖子:', post);
                        post.remove();
                    }
                }
            });

            // 方法3: 删除整个讨论区域容器
            const discussionContainers = document.querySelectorAll('.lg-article');
            discussionContainers.forEach(container => {
                if (container && isInSafeRemovalArea(container) && !isProtectedElement(container)) {
                    const text = container.textContent || '';
                    if (text.includes('最近讨论') || text.includes('讨论区')) {
                        console.log('删除整个讨论区域:', container);
                        container.remove();
                    }
                }
            });
        }
    }

    // 删除设置项(学术模式和权限记录)
    function removeSettingsItems() {
        if (window.location.href.includes('/user/setting')) {
            // 查找所有设置项
            const settingsItems = document.querySelectorAll('.l-form-layout, .setting-item, .form-item, [class*="radio"]');

            settingsItems.forEach(item => {
                if (item && item.parentNode && isInSafeRemovalArea(item) && !isProtectedElement(item)) {
                    const text = item.textContent || '';
                    // 删除学术模式
                    if (text.includes('学术模式') || text.includes('学术模式开关')) {
                        console.log('删除学术模式设置:', item);
                        item.remove();
                    }
                }
            });
        }
    }

    // 删除权限变更记录
function removePermissionChangeRecord() {
    if (window.location.href.includes('/user/setting')) {
        console.log('开始删除权限变更记录...');

        // 方法1: 直接查找包含"权限变更记录"文本的span元素,然后删除其父容器
        const permissionSpans = document.querySelectorAll('span');
        permissionSpans.forEach(span => {
            if (span.textContent === '权限变更记录') {
                const container = span.closest('.l-form-layout');
                if (container && container.parentNode) {
                    console.log('找到权限变更记录容器,准备删除:', container);
                    container.remove();
                    console.log('权限变更记录已删除');
                }
            }
        });

        // 方法2: 如果方法1失败,使用更精确的选择器
        const permissionContainers = document.querySelectorAll('.l-form-layout');
        permissionContainers.forEach(container => {
            if (container && container.parentNode) {
                const span = container.querySelector('span');
                if (span && span.textContent === '权限变更记录') {
                    console.log('通过精确选择器找到权限变更记录容器:', container);
                    container.remove();
                    console.log('权限变更记录已删除');
                }
            }
        });

        // 方法3: 使用TreeWalker确保找到所有实例
        const walker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            null,
            false
        );

        let node;
        while (node = walker.nextNode()) {
            if (node.textContent.trim() === '权限变更记录') {
                const span = node.parentElement;
                if (span && span.tagName === 'SPAN') {
                    const container = span.closest('.l-form-layout');
                    if (container && container.parentNode) {
                        console.log('通过TreeWalker找到权限变更记录容器:', container);
                        container.remove();
                        console.log('权限变更记录已删除');
                        break; // 删除一个就足够
                    }
                }
            }
        }

        // 方法4: 强制删除 - 查找所有包含权限变更记录文本的.l-form-layout
        const allLayouts = document.querySelectorAll('.l-form-layout');
        allLayouts.forEach(layout => {
            if (layout && layout.parentNode) {
                const text = layout.textContent || '';
                if (text.includes('权限变更记录') &&
                    text.includes('时间') &&
                    text.includes('原因') &&
                    text.includes('权限变更')) {
                    console.log('通过文本特征找到权限变更记录容器:', layout);
                    layout.remove();
                    console.log('权限变更记录已删除');
                }
            }
        });
    }
}

    // 删除个人主页白色栏的关注和动态
    function removeProfileTabs() {
        if (isUserProfilePage()) {
            // 查找白色栏容器
            const profileNav = document.querySelector('.user-nav, .profile-nav, .tabs, [class*="tab"], [class*="nav"]');
            if (profileNav) {
                // 删除关注和动态标签
                const tabsToRemove = profileNav.querySelectorAll('a, .item, .tab, li');
                tabsToRemove.forEach(tab => {
                    if (tab && tab.parentNode) {
                        const text = tab.textContent || '';
                        const href = tab.getAttribute('href') || '';

                        if (text.includes('关注') || text.includes('粉丝') ||
                            text.includes('动态') || text.includes('Feed') ||
                            href.includes('following') || href.includes('follower') ||
                            href.includes('feed')) {
                            console.log('删除个人主页标签:', text, tab);
                            tab.remove();
                        }
                    }
                });
            }

            // 额外查找:直接通过文本内容删除
            document.querySelectorAll('a, .item, .tab').forEach(element => {
                if (element && element.parentNode && isInSafeRemovalArea(element) && !isProtectedElement(element)) {
                    const text = element.textContent || '';
                    if ((text.includes('关注') || text.includes('动态')) &&
                        !text.includes('提交数') && !text.includes('通过数')) {
                        console.log('删除个人主页元素:', text, element);
                        element.remove();
                    }
                }
            });
        }
    }

    // 删除左侧栏讨论链接
    function removeSidebarDiscussion() {
        // 查找左侧栏容器
        const sidebar = document.querySelector('.sidebar, .left, [class*="sidebar"], [class*="left"]');
        if (!sidebar) return;

        // 在左侧栏中删除讨论链接
        const sidebarItems = sidebar.querySelectorAll('a, .item, .nav-item, li');
        sidebarItems.forEach(item => {
            if (item && item.parentNode) {
                const text = item.textContent || '';
                const href = item.getAttribute('href') || '';

                // 删除讨论相关
                if (text.includes('讨论') || text.includes('Discuss') ||
                    href === '/discuss' || href.includes('/discuss/') ||
                    item.querySelector('svg[data-icon="comments"]')) {
                    console.log('删除左侧栏讨论:', item);
                    // 删除整个列表项,避免留下空白
                    const listItem = item.closest('li') || item;
                    listItem.remove();
                }
            }
        });
    }

    // 删除讨论文本链接
    function removeDiscussionTextLinks() {
        // 查找所有包含"讨论"文本的链接
        const discussionLinks = document.querySelectorAll('a');
        discussionLinks.forEach(link => {
            if (link && link.parentNode && isInSafeRemovalArea(link) && !isProtectedElement(link)) {
                const text = link.textContent || '';
                const href = link.getAttribute('href') || '';

                if ((text.trim() === '讨论' || href === '/discuss') &&
                    !text.includes('提交数') && !text.includes('通过数')) {
                    console.log('删除讨论文本链接:', link);
                    // 删除整个链接的父容器,避免留下空白
                    const container = link.closest('li') || link.closest('.item') || link;
                    container.remove();
                }
            }
        });
    }

    // 检查元素是否在安全删除区域
    function isInSafeRemovalArea(element) {
        // 安全删除区域:导航栏、头部、设置页面、首页特定区域、个人主页白色栏
        const safeAreas = [
            'header', '.header', 'nav', '.nav',
            '.icon-btn', '.popup-button',
            '.l-form-layout',  // 设置区域
            '#app > div > div > div',  // 首页主要内容区
            '.main-container',
            '.user-nav', '.profile-nav', '.tabs', // 个人主页导航栏
            '.sidebar', '.left' // 左侧栏
        ];

        // 检查元素是否在安全区域
        for (let area of safeAreas) {
            if (element.closest(area)) {
                return true;
            }
        }

        // 特定元素的ID检查
        const safeIds = [
            'feed-content', 'feed-submit', 'emo-lst', 'home-center-nav',
            'LRadio-1', 'LRadio-2'
        ];

        if (element.id && safeIds.includes(element.id)) {
            return true;
        }

        // 检查是否在危险区域(编辑器、文章内容、题目页面等)
        const dangerAreas = [
            '.editor-container', '.markdown-editor', '[class*="editor"]',
            '.article-content', '.blog-content', '.content-area',
            '.code-editor', '.w-md-editor',
            '.problem-content', // 题目内容
            '.sample-container', // 样例容器
            '.problem-list', // 题目列表
            '.practice-plan', // 练习计划
            '.training-container', // 训练容器
            '.problem-info', // 题目信息
            '.info-item' // 信息项
        ];

        for (let area of dangerAreas) {
            if (element.closest(area)) {
                return false;
            }
        }

        return false;
    }

    // 智能监控函数
    function startIntelligentMonitoring() {
        let removalCount = 0;
        const maxRemovals = 15;

        function smartRemove() {
            if (removalCount >= maxRemovals) {
                console.log('达到最大删除次数,停止监控');
                return;
            }

            removalCount++;
            removeParentContainers();

            // 只在非编辑器页面检查
            if (!isEditorPage() && !isArticlePage()) {
                const hasTargets =
                    document.querySelector('header a[href="/user/notification"]') ||
                    document.querySelector('header a[href="/chat"]') ||
                    document.querySelector('a[href="/discuss"]') ||
                    document.querySelector('a .text') && document.querySelector('a .text').textContent.includes('讨论') ||
                    (window.location.pathname === '/' &&
                     (document.querySelector('#feed-content') ||
                      document.querySelector('.lg-article') ||
                      document.querySelector('[class*="feed"]') ||
                      document.querySelector('h2') && document.querySelector('h2').textContent.includes('最近讨论'))) ||
                    (window.location.href.includes('/user/setting') &&
                     (document.querySelector('.l-form-layout') ||
                      document.querySelector('*').textContent.includes('权限变更记录'))) ||
                    (isUserProfilePage() &&
                     (document.querySelector('a[href*="following"]') ||
                      document.querySelector('a[href*="follower"]') ||
                      document.querySelector('a[href*="feed"]')));

                if (hasTargets) {
                    console.log('检测到残留元素,继续清理');
                    setTimeout(smartRemove, 300);
                } else {
                    console.log('所有目标元素已清理完成');
                }
            }
        }

        smartRemove();
    }

    // 初始执行
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', startIntelligentMonitoring);
    } else {
        startIntelligentMonitoring();
    }

    // 监听动态加载的内容
    const observer = new MutationObserver((mutations) => {
        if (isEditorPage() || isArticlePage()) {
            return;
        }

        let shouldRemove = false;

        mutations.forEach((mutation) => {
            if (mutation.addedNodes.length) {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === 1) {
                        // 检查是否添加了目标元素
                        if (node.querySelector && (
                            node.querySelector('a[href="/user/notification"]') ||
                            node.querySelector('a[href="/chat"]') ||
                            node.querySelector('a[href="/discuss"]') ||
                            node.querySelector('#feed-content') ||
                            node.querySelector('.popup-button') ||
                            node.querySelector('a[href*="following"]') ||
                            node.querySelector('a[href*="follower"]') ||
                            node.querySelector('a[href*="feed"]') ||
                            node.querySelector('.lg-article') ||
                            node.querySelector('h2') ||
                            node.querySelector('a .text') ||
                            node.textContent.includes('权限变更记录')
                        )) {
                            shouldRemove = true;
                        }

                        // 检查元素本身
                        if (node.href === '/user/notification' ||
                            node.href === '/chat' ||
                            node.href === '/discuss' ||
                            node.id === 'feed-content' ||
                            node.classList.contains('popup-button') ||
                            (node.href && (node.href.includes('following') ||
                                          node.href.includes('follower') ||
                                          node.href.includes('feed'))) ||
                            node.classList.contains('lg-article') ||
                            (node.tagName === 'H2' && node.textContent.includes('最近讨论')) ||
                            (node.classList.contains('text') && node.textContent.includes('讨论')) ||
                            node.textContent.includes('权限变更记录')) {
                            shouldRemove = true;
                        }
                    }
                });
            }
        });

        if (shouldRemove) {
            console.log('检测到动态添加的目标元素');
            setTimeout(removeParentContainers, 100);
        }
    });

    // 开始观察
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['href', 'id', 'class']
    });

    // 页面加载完成后的清理
    window.addEventListener('load', () => {
        setTimeout(() => {
            console.log('页面加载完成,执行最终清理');
            removeParentContainers();
        }, 2000);
    });

    // 定期维护清理(只在非编辑器页面)
    setInterval(() => {
        if (!isEditorPage() && !isArticlePage()) {
            removeParentContainers();
        }
    }, 10000);

    console.log('洛谷综合清理器保护版初始化完成');

})();
原文