Mobile search engineer switcher

mobile search engineer switcher, bing google baidu

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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();
})();