搜索引擎官网净化器

自动屏蔽虚假搜索结果,智能保留官方网站

目前为 2025-03-31 提交的版本。查看 最新版本

// ==UserScript==
// @name         搜索引擎官网净化器
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  自动屏蔽虚假搜索结果,智能保留官方网站
// @author       little fool
// @license      MIT
// @match        *://www.baidu.com/*
// @match        *://www.google.com/*
// @match        *://www.bing.com/*
// @grant        GM_addStyle
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.registerMenuCommand
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // 增强样式(保持不变)
    GM_addStyle(`...`); // 保持原有样式不变

    // 更新后的智能配置
    const SearchEngineConfig = {
        'www.baidu.com': {
            resultContainer: '#content_left',
            resultSelector: '.c-container.new-pmd',
            officialMarkers: [
                '.ic-certification', // 更新后的官方标识选择器
                { type: 'text', pattern: /官网$/ },
                { type: 'attr', selector: '[src*="official_icon"]' } // 百度新图标属性
            ],
            // 其他配置保持不变...
        },
        'www.google.com': {
            resultContainer: '#search',
            resultSelector: '.g:not(.g-blk)', // 排除广告结果
            officialMarkers: [
                '.VjFXd', // 更新后的官方标识选择器
                { type: 'text', pattern: / - Official Site$/i },
                { type: 'text', pattern: /官网/i },
                { type: 'attr', selector: '[aria-label="Verified website"]' }
            ],
            // 其他配置保持不变...
        },
        // 必应配置保持不变...
    };

    class EnhancedSearchOptimizer {
        constructor() {
            this.currentEngine = location.hostname;
            this.config = this.getUpdatedConfig(); // 使用动态配置获取
            if (!this.config) return;

            this.enabled = true;
            this.init();
        }

        getUpdatedConfig() {
            // 动态检测搜索引擎更新
            const baiduNewSelectors = {
                resultSelector: '.result-op, .c-container',
                officialMarkers: [
                    '.official-icon-outer',
                    { type: 'text', pattern: /官方认证/ }
                ]
            };

            const googleNewSelectors = {
                resultSelector: '.MjjYud:not(.ULSxyf)',
                officialMarkers: [
                    '.iDjcJe[aria-label="Verified"]',
                    { type: 'text', pattern: /Official Website/i }
                ]
            };

            return {
                'www.baidu.com': { ...SearchEngineConfig['www.baidu.com'], ...baiduNewSelectors },
                'www.google.com': { ...SearchEngineConfig['www.google.com'], ...googleNewSelectors },
                'www.bing.com': SearchEngineConfig['www.bing.com']
            }[this.currentEngine];
        }

        // 修复方法名拼写错误
        highlightOfficial(element) {
            if (element.querySelector('.official-site-mark')) return;
            const titleElement = element.querySelector('h3, h2, a h3'); // 扩展标题选择范围
            if (titleElement) {
                const mark = document.createElement('span');
                mark.className = 'official-site-mark';
                mark.textContent = this.getOfficialText();
                titleElement.appendChild(mark);
            }
        }

        getOfficialText() {
            return {
                'www.baidu.com': '认证官网',
                'www.google.com': 'Verified',
                'www.bing.com': 'Official'
            }[this.currentEngine];
        }

        // 增强的DOM检测逻辑
        setupDOMObserver() {
            const observerOptions = {
                childList: true,
                subtree: true,
                attributes: false,
                characterData: false
            };

            const checkAndObserve = () => {
                const container = document.querySelector(this.config.resultContainer);
                if (container) {
                    this.observer = new MutationObserver(() => this.processExistingResults());
                    this.observer.observe(container, observerOptions);
                    this.processExistingResults();
                } else {
                    setTimeout(checkAndObserve, 500);
                }
            };

            checkAndObserve();
        }

        // 改进的结果处理逻辑
        processExistingResults() {
            if (!this.enabled) return;
            $(this.config.resultSelector)
                .not('[data-processed]')
                .each((i, el) => {
                    this.analyzeResult(el);
                    el.dataset.processed = 'true';
                });
        }

        // 其他方法保持不变...
    }

    // 初始化优化器(增加容错机制)
    setTimeout(() => {
        try {
            new EnhancedSearchOptimizer();
        } catch (error) {
            console.error('Search Optimizer Error:', error);
        }
    }, 1000); // 延迟1秒确保DOM加载

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址