百度重定向Google搜索

当输入以"``"或"··"开头的查询时,自动重定向到Google搜索

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         百度重定向Google搜索
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  当输入以"``"或"··"开头的查询时,自动重定向到Google搜索
// @author       wze
// @match        *://www.baidu.com/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const prefixes = ['``', '··']; 

    function redirectIfNeeded(query) {
        if (!query) return false;
        const trimmedQuery = query.trim();
        for (const prefix of prefixes) {
            if (trimmedQuery.startsWith(prefix)) {
                const googleQuery = trimmedQuery.substring(prefix.length).trim();
                window.location.replace(`https://www.google.com/search?q=${encodeURIComponent(googleQuery)}`);
                return true;
            }
        }
        return false;
    }

    function checkUrlParams() {
        const params = new URLSearchParams(window.location.search);
        const query = params.get('wd') || params.get('word');
        return redirectIfNeeded(query);
    }

    if (checkUrlParams()) {
        return;
    }

    function getSearchInput() {
        return document.getElementById('kw') ||
               document.querySelector('input[name="wd"]') ||
               document.querySelector('input[type="search"]');
    }

    function bindSearchEvent(searchInput) {
        if (searchInput && !searchInput._redirectBound) {
            searchInput._redirectBound = true;
            const form = searchInput.form || document.querySelector('form[action="/s"]');
            if (form) {
                form.addEventListener('submit', function(e) {
                    const query = searchInput.value;
                    if (redirectIfNeeded(query)) {
                        e.preventDefault();
                    }
                });
            }
        }
    }

    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                const searchInput = getSearchInput();
                if (searchInput) {
                    bindSearchEvent(searchInput);
                }
            }
        });
    });

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

    const initialSearchInput = getSearchInput();
    if (initialSearchInput) {
        bindSearchEvent(initialSearchInput);
    }
})();