Greasy Fork 支持简体中文。

CSDN滚啊

屏蔽搜索结果中出现的一切有关CSDN的选项

// ==UserScript==
// @name         CSDN滚啊
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  屏蔽搜索结果中出现的一切有关CSDN的选项
// @author       xiaoma
// @match         *://*/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

/*
MIT License

Copyright (c) 2024 xiaoma

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.
*/

(function () {
    'use strict';

    // 添加百度搜索结果的选择器
    const baiduSelectors = {
        results: '#content_left > div.result, #content_left > div.result-op',
        title: 'h3.t a, h3.c-title a',
        url: '.c-showurl'
    };

    // 屏蔽CSDN链接
    function blockCSDNLinks() {
        // 定义CSDN主域名
        const csdnDomain = 'csdn.net';

        // 查找所有链接
        const links = document.querySelectorAll('a');

        links.forEach(link => {
            // 检查链接是否包含CSDN主域名
            if (link.href.includes(csdnDomain)) {
                // 尝试找到包含链接的搜索结果项
                const resultItem = link.closest('.b_ans, .b_widgetContainer, .b_algo');
                if (resultItem) {
                    // 隐藏整个搜索结果项
                    resultItem.style.display = 'none';
                }
            }
        });

        // 新增百度搜索屏蔽逻辑
        const baiduResults = document.querySelectorAll(baiduSelectors.results);
        baiduResults.forEach(result => {
            const titleEl = result.querySelector(baiduSelectors.title);
            const urlEl = result.querySelector(baiduSelectors.url);
            if (!titleEl && !urlEl) return;
            const titleText = titleEl?.textContent?.toLowerCase() || '';
            const urlText = urlEl?.textContent?.toLowerCase() || '';
            const href = titleEl?.href?.toLowerCase() || '';
            if (titleText.includes('csdn') || urlText.includes('csdn') || href.includes('csdn.net')) {
                result.style.display = 'none';
            }
        });

        // 隐藏百度右侧栏
        const rightColumn = document.getElementById('content_right');
        if (rightColumn) {
            rightColumn.style.display = 'none';
        }
    }

    // 添加防抖函数
    function debounce(func, wait) {
        let timeout;
        return function () {
            const context = this;
            const args = arguments;
            clearTimeout(timeout);
            timeout = setTimeout(() => {
                func.apply(context, args);
            }, wait);
        };
    }

    // 修改滚动事件监听,优化触底判定
    window.addEventListener('scroll', debounce(function () {
        // 获取页面总高度
        const scrollHeight = Math.max(
            document.documentElement.scrollHeight,
            document.body.scrollHeight
        );

        // 获取当前滚动位置
        const scrollTop = window.pageYOffset ||
            document.documentElement.scrollTop ||
            document.body.scrollTop;

        // 获取视窗高度
        const clientHeight = window.innerHeight ||
            document.documentElement.clientHeight ||
            document.body.clientHeight;

        // 只在距离底部100px时触发
        if (scrollHeight - scrollTop - clientHeight < 100) {
            requestAnimationFrame(() => {
                blockCSDNLinks();
            });
        }
    }, 200));

    // 优化MutationObserver回调
    var observer = new MutationObserver(debounce(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes.length) {
                blockCSDNLinks();
            }
        });
    }, 200));

    // 配置观察器选项: 观察子节点的变化
    var config = {
        childList: true,
        subtree: true
    };

    // 配置观察器选项: 观察子节点的变化
    var targetNode = document.body;

    // 启动观察器
    observer.observe(targetNode, config);

    // 初始执行一次,以处理页面加载时已经存在的链接
    blockCSDNLinks();
})();