Mobile search engineer switcher

mobile search engineer switcher, bing google baidu

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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();
})();