移动端搜索引擎切换

移动端搜索引擎切换,必应、谷歌、百度

当前为 2023-10-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name              Mobile search engineer switcher
// @name:zh-CN        移动端搜索引擎切换
// @namespace         http://tampermonkey.net/
// @version           2023.10.28
// @description       mobile search engineer switcher, bing google baidu
// @description:zh-CN 移动端搜索引擎切换,必应、谷歌、百度
// @author            Andy Yuen

// @include           https://*.bing.*/search*
// @include           https://*.google.*/search*
// @include           https://*.baidu.*/*wd*
// @include           https://*.baidu.*/*word*
// @icon              https://www.google.com/s2/favicons?sz=64&domain=bing.com
// @license           MIT
// @grant             none
// ==/UserScript==

(function () {
    'use strict';

    console.log('mobile search engineer switcher')

    if (!/Mobi|Android|iPhone/i.test(navigator.userAgent)) return;

    // Disable the scroll to top functionality
    if (location.host.includes('bing.com')) {
        window.addEventListener('focus', function () {
            window.scrollTo = function (x, y) {
                if (y !== 0) {
                    window.scrollTo.originalFunc(x, y);
                }
            };
            window.scrollTo.originalFunc = window.scrollTo;
        });
    }

    // search engineer
    let selector, mapCallback;
    if (location.host.includes('bing.com')) {
        selector = '[role="navigation"] ul';
        mapCallback = ([name, nameCn, url]) => {
            let element = document.createElement('li');
            element.className = 'injection-mses';
            element.innerHTML = `<a>${nameCn}</a>`;
            element.onclick = () => {
                location.href = 'https://' + url + new URLSearchParams(location.search).get('q');
            };
            return element;
        };
    } else if (location.host.includes('google.com')) {
        selector = '#hdtb-msb';
        mapCallback = ([name, nameCn, url]) => {
            let element = document.createElement('li');
            element.className = 'injection-mses hdtb-mitem';
            element.innerHTML = `<a>${nameCn}</a>`;
            element.onclick = () => {
                location.href = 'https://' + url + new URLSearchParams(location.search).get('q');
            };
            return element;
        };
    } else if (location.host.includes('baidu.com')) {
        selector = '.se-tab-lists';
        mapCallback = ([name, nameCn, url]) => {
            let element = document.createElement('a');
            element.className = 'injection-mses se-tabitem';
            element.innerHTML = `<span>${nameCn}</span>`;
            element.onclick = () => {
                const params = new URLSearchParams(location.search);
                location.href = 'https://' + url + (params.get('wd') || params.get('word'));
            };
            return element;
        };
    }

    function inject() {
        if (document.querySelector(selector) && !document.querySelector('.injection-mses')) {
            document.querySelector(selector).prepend(...[
                ['google', '谷歌', 'google.com/search?q='],
                ['bing', '必应', 'bing.com/search?q='],
                ['baidu', '百度', 'baidu.com/s?wd=']
            ].filter(([name]) => !location.host.includes(name)).map(mapCallback));
        } else {
            setTimeout(inject, 10);
        }
    }

    inject();
})();