跳到正文

科技·工程

洛谷灌水清理器 1.0

AutumnMoon约 29 分钟
原文

这是 2025年10月23日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  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 removalConfig = {
        // 通知铃铛
        bell: [
            'a[href="/user/notification"]',
            'svg[data-icon="bell"]',
            'path[d*="M224 0c-17.7"]',
            '.fa-bell'
        ],

        // 私信/邮件
        message: [
            'a[href="/chat"]',
            'svg[data-icon="envelope"]',
            'path[d*="M48 64C21.5"]',
            '.fa-envelope'
        ],

        // 应用按钮
        app: [
            '.popup-button',
            'svg[data-icon="angles-right"]'
        ],

        // 讨论 - 只删除顶部的讨论链接
        discuss: [
            'a[href="/discuss"]',
            'svg[data-icon="comments"]',
            'path[d*="M208 352c114.9"]'
        ],

        // 犇犇相关 - 只在首页删除
        feed: [
            '#feed-content',
            '#feed-submit',
            '#emo-lst',
            '.exlg-emo-btn',
            '.feed-selector',
            '#home-center-nav'
        ],

        // 用户信息
        user: [
            'a[href*="/following"]',
            'a[href*="/follower"]'
        ],

        // 设置相关
        settings: [
            '#LRadio-1',
            '#LRadio-2'
        ]
    };

    // 检查是否在编辑器页面
    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 safeRemoveElements() {
        // 如果在编辑器或专栏页面,不执行删除
        if (isEditorPage() || isArticlePage()) {
            console.log('在编辑器或专栏页面,跳过删除');
            return;
        }

        console.log('开始安全清理...');

        // 1. 按选择器删除,但只在特定区域操作
        Object.values(removalConfig).flat().forEach(selector => {
            try {
                document.querySelectorAll(selector).forEach(element => {
                    if (element && element.parentNode) {
                        // 检查元素是否在安全删除区域
                        const isSafeToRemove = isInSafeRemovalArea(element);
                        
                        if (isSafeToRemove) {
                            console.log('安全删除元素:', selector, element);
                            element.remove();
                        }
                    }
                });
            } catch (e) {
                // 忽略选择器错误
            }
        });

        // 2. 按文本内容删除,但更精确
        safeRemoveByTextContent();

        // 3. 精确区域删除
        removeSpecificAreas();
    }

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

        // 检查元素是否在安全区域
        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'
        ];

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

        return false;
    }

    // 安全的文本内容删除
    function safeRemoveByTextContent() {
        const textTargets = [
            { text: '有什么新鲜事告诉大家', container: '.lg-article', area: 'home' },
            { text: '发射犇犇', container: '.lg-article', area: 'home' },
            { text: '动态', container: '.entry', area: 'nav' },
            { text: '学术模式', container: '.l-form-layout', area: 'settings' },
            { text: '权限变更记录', container: '.l-form-layout', area: 'settings' },
            { text: '讨论', container: null, area: 'nav' }
        ];

        textTargets.forEach(({text, container, area}) => {
            try {
                const walker = document.createTreeWalker(
                    document.body,
                    NodeFilter.SHOW_TEXT,
                    null,
                    false
                );

                let node;
                const nodesToRemove = [];
                
                while (node = walker.nextNode()) {
                    if (node.textContent.includes(text)) {
                        const element = node.parentElement;
                        
                        // 检查是否在安全区域
                        if (isInSafeTextRemovalArea(element, area)) {
                            if (container) {
                                const containerEl = element.closest(container);
                                if (containerEl && isInSafeRemovalArea(containerEl)) {
                                    nodesToRemove.push(containerEl);
                                }
                            } else {
                                nodesToRemove.push(element);
                            }
                        }
                    }
                }

                // 删除找到的元素
                nodesToRemove.forEach(element => {
                    if (element && element.parentNode) {
                        console.log('安全文本删除:', text, element);
                        element.remove();
                    }
                });
            } catch (e) {
                console.log('文本删除错误:', e);
            }
        });
    }

    // 检查文本是否在安全删除区域
    function isInSafeTextRemovalArea(element, areaType) {
        switch(areaType) {
            case 'nav':
                return element.closest('header') || 
                       element.closest('.header') ||
                       element.closest('nav');
            case 'home':
                return window.location.pathname === '/' || 
                       window.location.pathname === '/index' ||
                       element.closest('#app > div > div > div');
            case 'settings':
                return window.location.href.includes('/user/setting') ||
                       element.closest('.l-form-layout');
            default:
                return isInSafeRemovalArea(element);
        }
    }

    // 精确区域删除
    function removeSpecificAreas() {
        // 只在首页删除犇犇区域
        if (window.location.pathname === '/' || window.location.pathname === '/index') {
            const articles = document.querySelectorAll('.lg-article');
            articles.forEach(article => {
                const text = article.textContent;
                if ((text.includes('有什么新鲜事') || text.includes('发射犇犇')) && 
                    isInSafeRemovalArea(article)) {
                    console.log('删除犇犇区域:', article);
                    article.remove();
                }
            });
        }

        // 导航栏图标 - 只在导航栏内删除
        const navItems = document.querySelectorAll('.icon-btn, [class*="btn"], [class*="button"]');
        navItems.forEach(item => {
            const isInNav = item.closest('header') || 
                           item.closest('.header') ||
                           item.closest('nav');
            
            if (isInNav && isInSafeRemovalArea(item)) {
                const html = item.innerHTML;
                if (html.includes('bell') ||
                    html.includes('envelope') ||
                    html.includes('M224 0c-17.7') ||
                    html.includes('M48 64C21.5')) {
                    console.log('删除导航项:', item);
                    item.remove();
                }
            }
        });

        // 设置区域 - 只在设置页面删除
        if (window.location.href.includes('/user/setting')) {
            const layouts = document.querySelectorAll('.l-form-layout');
            layouts.forEach(layout => {
                const text = layout.textContent;
                if ((text.includes('学术模式') || text.includes('权限变更记录')) && 
                    isInSafeRemovalArea(layout)) {
                    console.log('删除设置区域:', layout);
                    layout.remove();
                }
            });
        }

        // 动态入口
        const entries = document.querySelectorAll('.entry');
        entries.forEach(entry => {
            if (entry.textContent.includes('动态') && isInSafeRemovalArea(entry)) {
                console.log('删除动态入口:', entry);
                entry.remove();
            }
        });

        // 关注粉丝统计
        const statTexts = document.querySelectorAll('.stat-text.name');
        statTexts.forEach(stat => {
            if ((stat.textContent.includes('关注') || stat.textContent.includes('粉丝')) && 
                isInSafeRemovalArea(stat)) {
                console.log('删除统计信息:', stat);
                const field = stat.closest('.field');
                if (field && isInSafeRemovalArea(field)) {
                    field.remove();
                }
            }
        });
    }

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

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

            removalCount++;
            safeRemoveElements();

            // 只在非编辑器页面检查
            if (!isEditorPage() && !isArticlePage()) {
                const hasTargets =
                    document.querySelector('header a[href="/user/notification"]') ||
                    document.querySelector('header a[href="/chat"]') ||
                    document.querySelector('header a[href="/discuss"]') ||
                    (window.location.pathname === '/' && document.querySelector('#feed-content'));
                    
                if (hasTargets) {
                    console.log('检测到残留元素,继续清理');
                    setTimeout(smartRemove, 500);
                } else {
                    console.log('所有目标元素已清理完成');
                }
            }
        }

        smartRemove();
    }

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

    // 高级MutationObserver
    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) {
                        // 只在安全区域检查新增元素
                        const isInNav = node.closest && (
                            node.closest('header') || 
                            node.closest('.header') ||
                            node.closest('nav')
                        );
                        
                        if (isInNav && node.querySelector && (
                            node.querySelector('a[href="/user/notification"]') ||
                            node.querySelector('a[href="/chat"]') ||
                            node.querySelector('a[href="/discuss"]') ||
                            (window.location.pathname === '/' && node.querySelector('#feed-content'))
                        )) {
                            shouldRemove = true;
                        }

                        // 检查元素本身
                        if (node.href === '/user/notification' ||
                            node.href === '/chat' ||
                            node.href === '/discuss' ||
                            (window.location.pathname === '/' && node.id === 'feed-content')) {
                            shouldRemove = true;
                        }
                    }
                });
            }
        });

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

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

    // 页面加载完成后的深度清理
    window.addEventListener('load', () => {
        setTimeout(() => {
            console.log('页面加载完成,执行安全清理');
            safeRemoveElements();
        }, 3000);
    });

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

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

})();

致谢

感谢 Sweet_2013 ,她调戏了 Deepseek 17 遍完成了该插件的创作。(bushi

也感谢 Deepseek 连续十六次对着 Sweet_2013 道歉并一次次改版。