Google Search Correction Fix

Fix Google search bar reverting to incorrect spelling after correction

当前为 2025-10-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google Search Correction Fix
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Fix Google search bar reverting to incorrect spelling after correction
// @author       Satoko
// @license MIT
// @match        https://www.google.com/search?*
// @match        https://www.google.ru/search?*
// @match        https://www.google.com.ua/search?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    console.log('Google Search Correction Fix loaded');

    function fixCorrectionLinks() {
        const correctionLinks = document.querySelectorAll('a[href*="spell=1"], a[href*="correction"], .gL9Hy, .gNG8Bc, a[jsname*="spell"]');

        correctionLinks.forEach(link => {
            if (link.hasAttribute('data-fixed')) return;

            link.setAttribute('data-fixed', 'true');

            const originalHref = link.href;

            link.addEventListener('click', function(e) {
                e.preventDefault();
                e.stopPropagation();

                console.log('Intercepted correction click, redirecting to:', originalHref);

                window.location.href = originalHref;
            });
        });
    }

    function fixDidYouMean() {
        const didYouMeanElements = document.querySelectorAll('#fprs a, .aCOpRe em, .MUxGbd, .gqFSUc');

        didYouMeanElements.forEach(element => {
            if (element.hasAttribute('data-fixed')) return;

            const link = element.closest('a') || element;
            if (link.href && link.href.includes('google.com/search')) {
                link.setAttribute('data-fixed', 'true');

                link.addEventListener('click', function(e) {
                    if (this.href && this.href !== window.location.href) {
                        e.preventDefault();
                        e.stopPropagation();
                        console.log('Intercepted "Did you mean" click');
                        window.location.href = this.href;
                    }
                });
            }
        });
    }

    function fixSearchBox() {
        const searchBox = document.querySelector('input[name="q"]');
        if (searchBox) {
            const urlParams = new URLSearchParams(window.location.search);
            const urlQuery = urlParams.get('q');

            if (urlQuery && searchBox.value !== urlQuery) {
                if (!searchBox.value.includes(urlQuery) && urlQuery.includes(searchBox.value)) {
                    const newUrl = new URL(window.location.href);
                    newUrl.searchParams.set('q', searchBox.value);
                    window.history.replaceState(null, '', newUrl.toString());
                    console.log('Fixed URL to match search box:', searchBox.value);
                }
            }
        }
    }

    function runAllFixes() {
        fixCorrectionLinks();
        fixDidYouMean();
        fixSearchBox();
    }

    runAllFixes();

    const observer = new MutationObserver(function(mutations) {
        let shouldRun = false;
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                shouldRun = true;
            }
        });
        if (shouldRun) {
            setTimeout(runAllFixes, 100);
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    setInterval(runAllFixes, 2000);

})();