隐藏CSDN搜索结果

自动隐藏搜索引擎结果中的CSDN链接,并显示统计信息

目前為 2025-04-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         隐藏CSDN搜索结果
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  自动隐藏搜索引擎结果中的CSDN链接,并显示统计信息
// @author       YourName
// @match        *://www.baidu.com/s?*
// @match        *://www.google.com/search?*
// @match        *://www.bing.com/search?*
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 配置参数
    const config = {
        targetDomains: ['csdn.net'],
        styles: {
            statsBox: `
                position: fixed;
                bottom: 20px;
                right: 20px;
                background: rgba(0, 0, 0, 0.85);
                color: #fff;
                padding: 12px 16px;
                border-radius: 8px;
                z-index: 99999;
                max-height: 70vh;
                overflow: hidden;
                transition: all 0.3s ease;
                backdrop-filter: blur(5px);
                border: 1px solid rgba(255, 255, 255, 0.1);
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
                font-family: system-ui, sans-serif;
                font-size: 14px;
            `,
            hoverStyle: `
                max-height: 70vh;
                overflow-y: auto;
            `
        }
    };

    let hiddenResults = [];
    let statsBox = null;

    // 添加自定义样式
    GM_addStyle(`
        .csdn-stats-box:hover {
            ${config.styles.hoverStyle}
        }
        .csdn-stats-links {
            margin-top: 10px;
            display: none;
        }
        .csdn-stats-box:hover .csdn-stats-links {
            display: block;
        }
        .csdn-link-item {
            padding: 6px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            word-break: break-all;
        }
        .csdn-link-item:last-child {
            border-bottom: none;
        }
        .csdn-link-item a {
            color: #90caf9 !important;
            text-decoration: none;
        }
        .csdn-link-item a:hover {
            text-decoration: underline;
        }
    `);

    // 创建统计面板
    function createStatsBox() {
        statsBox = document.createElement('div');
        statsBox.className = 'csdn-stats-box';
        statsBox.style.cssText = config.styles.statsBox;
        statsBox.innerHTML = `
            <div class="csdn-stats-header">
                🚫 已隐藏 <strong class="csdn-count">0</strong> 个CSDN结果
            </div>
            <div class="csdn-stats-links"></div>
        `;
        document.body.appendChild(statsBox);
    }

    // 更新统计信息
    function updateStats() {
        statsBox.querySelector('.csdn-count').textContent = hiddenResults.length;
        const linksContainer = statsBox.querySelector('.csdn-stats-links');
        linksContainer.innerHTML = hiddenResults
            .map(link => `
                <div class="csdn-link-item">
                    <a href="${link}" target="_blank">${new URL(link).hostname}</a>
                </div>
            `).join('');
    }

    // 获取真实URL(处理百度跳转)
    function getRealUrl(linkElement) {
        // 处理百度跳转链接
        if (window.location.hostname === 'www.baidu.com') {
            const realUrl = linkElement.getAttribute('data-realurl') 
                || (linkElement.onmousedown && linkElement.onmousedown.match(/'pu':'([^']+)'/)?.[1]);
            if (realUrl) return decodeURIComponent(realUrl);
        }
        return linkElement.href;
    }

    // 判断是否需要隐藏
    function shouldHide(url) {
        return config.targetDomains.some(domain => url.includes(domain));
    }

    // 隐藏搜索结果
    function processResults() {
        const selectors = {
            'www.baidu.com': '.c-container',
            'www.google.com': '.g',
            'www.bing.com': '.b_algo'
        };

        const host = window.location.hostname;
        const selector = selectors[host] || '';
        if (!selector) return;

        document.querySelectorAll(selector).forEach(result => {
            const link = result.querySelector('a');
            if (!link) return;

            const realUrl = getRealUrl(link);
            if (shouldHide(realUrl)) {
                if (result.style.display !== 'none') {
                    result.style.display = 'none';
                    hiddenResults.push(realUrl);
                }
            }
        });

        updateStats();
    }

    // 初始化
    function init() {
        if (!statsBox) createStatsBox();
        hiddenResults = [];
        processResults();

        // 监听页面变化
        const observer = new MutationObserver(() => processResults());
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        // 监听页面滚动(适用于无限滚动页面)
        window.addEventListener('scroll', () => processResults());
    }

    // 延时初始化以避免错过初始结果
    setTimeout(init, 1000);
})();