屏蔽CSDN

自动排除浏览器中搜索到的所有来自CSDN的结果,并显示隐藏数量和链接

当前为 2025-01-14 提交的版本,查看 最新版本

// ==UserScript==
// @name 屏蔽CSDN
// @namespace http://tampermonkey.net/
// @license MIT
// @version 0.3
// @description 自动排除浏览器中搜索到的所有来自CSDN的结果,并显示隐藏数量和链接
// @author GNEH
// @match *://*.google.com/*
// @grant none
// ==/UserScript==

(function() {
    'use strict';

    // 初始化隐藏结果计数和链接数组
    let hiddenCount = 0;
    const hiddenLinks = [];
    const maxDisplayedLinks = 5; // 最多显示5个链接

    // 创建一个显示计数和链接的元素
    const countElement = document.createElement('div');
    countElement.style.position = 'fixed';
    countElement.style.bottom = '20px';
    countElement.style.right = '20px';
    countElement.style.backgroundColor = '#f0f0f0';
    countElement.style.padding = '10px';
    countElement.style.zIndex = 9999;
    // 添加圆角和大小限制
    countElement.style.borderRadius = '5px';
    countElement.style.maxWidth = '250px';
    //countElement.style.maxHeight = '100px';
    document.body.appendChild(countElement);
    document.body.appendChild(countElement);

    // 隐藏CSDN搜索结果
    function hideCsdnSearchResults() {
        const searchResults = document.querySelectorAll(".g"); // 根据搜索引擎调整选择器

        searchResults.forEach(result => {
            const link = result.querySelector("a");

            if (link && link.href.indexOf("csdn.net") !== -1)
            {
                result.style.display = "none";
                hiddenCount++;

                // 存储链接和序号
                if (hiddenLinks.length < maxDisplayedLinks)
                {
                    hiddenLinks.push({
                        index: hiddenLinks.length + 1,
                        link: `<a href="${link.href}" target="_blank">${link.textContent}</a>`
                    });
                }
            }
        });

        // 更新显示内容
        let html = `已隐藏 CSDN 结果:${hiddenCount} 条<br>`;
        hiddenLinks.forEach(item => {
            html += `${item.index}. ${item.link}<br>`;
        });
        countElement.innerHTML = html;
    }

    // 页面加载完成后立即执行
    hideCsdnSearchResults();
})();