Google Exclusions Dynamic Clean Fixed

Blocks keywords and websites from Google search without breaking typing spaces

当前为 2025-11-22 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Exclusions Dynamic Clean Fixed
// @namespace    http://tampermonkey.net/
// @version      1.9
// @description  Blocks keywords and websites from Google search without breaking typing spaces
// @author       ScriptKing
// @match        https://www.google.com/search*
// @match        https://www.google.com/
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    // IMPORTANT
    // (1) update your website search engine to exclude the excluded websites/keywords, see an example below:
    // like this: https://www.google.com/search?q=%s+-site:website_to_exclude.com+-keyword_to_exclude
    // (2) put the exclusions also in the list below
    const exclusions = ['website_to_exclude.com', 'keyword_to_exclude'];

    function isDomain(str) {
        return str.includes('.');
    }

    function appendExclusions(query) {
        let newQuery = query;
        exclusions.forEach(ex => {
            let exStr = isDomain(ex) ? `-site:${ex}` : `-${ex}`;
            if (!newQuery.toLowerCase().includes(exStr.toLowerCase())) {
                newQuery += ` ${exStr}`;
            }
        });
        return newQuery;
    }

    function cleanQueryBox() {
        const selectors = 'input[name="q"], textarea[name="q"], input[type="search"]';
        const queryBox = document.querySelector(selectors);

        if (queryBox && queryBox.value) {
            let cleaned = queryBox.value;
            exclusions.forEach(ex => {
                const exEsc = ex.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
                const regex = isDomain(ex)
                    ? new RegExp(`\\s*-site:${exEsc}\\s*`, 'gi')
                    : new RegExp(`\\s*-${exEsc}\\s*`, 'gi');
                cleaned = cleaned.replace(regex, ' ');
            });
            cleaned = cleaned.replace(/\s+/g, ' ').trim();

            if (cleaned !== queryBox.value) {
                queryBox.value = cleaned;
                ['input', 'change'].forEach(eventType => {
                    queryBox.dispatchEvent(new Event(eventType, { bubbles: true }));
                });
            }
        }
    }

    async function setupSearchHandler() {
        const button = await new Promise(resolve => {
            const interval = setInterval(() => {
                const btn = document.querySelector('button[aria-label="Search"], input[name="btnK"], button[name="btnK"]');
                if (btn) {
                    clearInterval(interval);
                    resolve(btn);
                }
            }, 100);
        });

        const queryBox = document.querySelector('input[name="q"], textarea[name="q"]');

        button.addEventListener('click', e => {
            if (queryBox && queryBox.value.trim()) {
                e.preventDefault();
                queryBox.value = appendExclusions(queryBox.value);
                setTimeout(() => {
                    queryBox.form.submit();
                }, 0);
            }
        });
    }

    function initialize() {
        setupSearchHandler();
        window.addEventListener('load', cleanQueryBox); // clean after results load
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initialize);
    } else {
        initialize();
    }

    // Only clean after typing finishes (Enter key)
    document.addEventListener('keydown', e => {
        if (e.key === 'Enter' && e.target.matches('input[name="q"], textarea[name="q"]')) {
            const queryBox = e.target;
            if (queryBox.value.trim()) {
                queryBox.value = appendExclusions(queryBox.value);
                setTimeout(cleanQueryBox, 100); // clean after submit
            }
        }
    });

})();