切换百度谷歌搜索结果

在百度中点击按钮以使用相同关键词在google搜索。在google中点击按钮以使用相同关键词在百度中搜索。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            切换百度谷歌搜索结果
// @name            BaiduGoogleResultSwitcher
// @namespace       https://github.com/NiaoBlush/BaiduGoogleSwitcher
// @version         1.0
// @description     在百度中点击按钮以使用相同关键词在google搜索。在google中点击按钮以使用相同关键词在百度中搜索。
// @author          NiaoBlush
// @license         MIT
// @grant           none
// @include         https://www.google.com/search?*
// @include         https://www.baidu.com/s?*
// ==/UserScript==

(function () {
    'use strict';

    const switcherBtnId = "baidu-google-switcher-btn-id";
    const site = getCurrentSite();

    let parent;
    let btnText;
    let logoUrl;
    if (site === "baidu") {
        parent = document.getElementsByClassName("s_tab_inner")[0];
        btnText = "Google一下";
        logoUrl = "https://www.google.com/favicon.ico";
    } else if (site === "google") {
        parent = document.getElementsByTagName("g-header-menu")[1].parentElement;
        btnText = "百度一下";
        logoUrl = "https://www.baidu.com/favicon.ico";
    }

    createButton(parent, btnText, logoUrl);

    /**
     * 创建按钮并插入
     * @param parent 父元素
     * @param btnText 按钮文本
     * @param logoUrl 图标url
     */
    function createButton(parent, btnText, logoUrl) {
        const imgSize = 16;

        let className = parent.lastElementChild.className;
        let button = document.createElement("a");
        button.className = className;
        button.id = switcherBtnId;
        button.style.width = "100px"
        button.style.cursor = "pointer";
        button.onclick = () => redirect();

        let img = document.createElement("img");
        img.src = logoUrl;
        img.style.height = `${imgSize}px`;
        img.style.width = `${imgSize}px`;

        let text = document.createElement("span");
        text.innerText = btnText;

        if (logoUrl) {
            button.appendChild(img);
        }
        button.appendChild(text);
        parent.appendChild(button);
    }

    /**
     * 获取当前网站
     * @returns {string} baidu | google
     */
    function getCurrentSite() {
        if (location.hostname.indexOf("baidu") > -1) {
            return "baidu";
        } else if (location.hostname.indexOf("google") > -1) {
            return "google";
        } else {
            return "unknown host";
        }
    }

    /**
     * 获取当前关键字
     * @returns {string} 关键字
     */
    function getKeyword() {
        const urlParams = new URLSearchParams(window.location.search);
        let keyword = "";
        if (site === "baidu") {
            keyword = urlParams.get("wd");
        } else if (site === "google") {
            keyword = urlParams.get("q");
        }
        return keyword;
    }

    /**
     * 重定向
     */
    function redirect() {
        document.getElementById(switcherBtnId).innerText = "redirecting";
        const urlParams = new URLSearchParams(window.location.search);
        let url = "";
        if (site === "google") {
            url = `https://www.baidu.com/s?wd=${getKeyword()}`;
        } else if (site === "baidu") {
            url = `https://www.google.com/search?q=${getKeyword()}`;
        }
        if (url) {
            location.href = url;
        }
    }

})();