Greasy Fork

Google & baidu Switcher (ALL in One)

分别在百度和google的搜索结果页面增加搜索跳转按钮,使用到jquery的jquery-1.7.2.min.js,不懂跳墙使用GOOGLE的同学请自动忽略。自动判断百度和google对JQUERY的载入,并动态载入Jquery.js。

目前为 2020-02-26 提交的版本。查看 最新版本

// ==UserScript==
// @name      Google & baidu Switcher (ALL in One)
// @namespace    https://openuserjs.org/scripts/t3xtf0rm4tgmail.com/Google_baidu_Switcher_(ALL_in_One)
// @homepageURL     https://greasyfork.org/zh-CN/scripts/12909-google-baidu-switcher-all-in-one
// @feedback-url  https://greasyfork.org/zh-CN/scripts/12909-google-baidu-switcher-all-in-one/feedback
// @author      F9y4ng
// @version      1.6.2.3
// @description    分别在百度和google的搜索结果页面增加搜索跳转按钮,使用到jquery的jquery-1.7.2.min.js,不懂跳墙使用GOOGLE的同学请自动忽略。自动判断百度和google对JQUERY的载入,并动态载入Jquery.js。
// @icon      data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHpSURBVFhH7Ze7SsRAFIYXLSwUaxXEQlEUfQIrWxUsfAk7G2+VYGHnO9j7AjYqaClYWCtWFlp5QfDS6P8lORCGTHZGswnC/vDBzDlz/kkys5lsq6v/qgfx7UCsFo0Id3KDXMe1IpjsOOmlok2MXMd1IJhsP+mlok2MXCUaE1diV0wSkObEhWAiWBUm2hZnDGMRtXjghWewjoQZwrX4yNo3Yl0MCBNtYuQY8y6oyXvgGaQFQcGLOBRPWf9LbIge4RM5xjCWGmrxwIs+3qXqFXblGKE+weaaT3phYiw11CK88MSbObxaE3a3owQ8Whan4i2D9pLwCS97Kszh1ZS4EwxkPSeEqz1BvghyrvCwvYE3c5RqSFwKCh6F/QoQd06cDcljHc7YFJ+CXP5JUIsHcTzxDlK/uBcUzhDIdCaI2f7Ia1uQY4yJWmJ44RmlZ0HxYNJL9SqIcdeuiJFjjIlaYnhFK/YC7Iyo5ALaLQFr7mpHkMsvwbQgFrUEIZuQDcdFcNfA5EWbcFxEbcLGf4ahLyLu8kSw3kB7UfgU/CJq/FWMyg4j1r3dYbQl7G6jDyNT0XHMEUv7VviOY3KM+dNxjHwfJOfCDH0fJIyZFejXHyRlquWTrExsLiZr7KPUXrlFkKtFjf4x6apCtVo/mQri6IC8s1UAAAAASUVORK5CYII=
// @compatible    Chrome + Tampermonkey
// @compatible    Firefox + Tampermonkey
// @compatible    Microsoft Edge + Tampermonkey
// @compatible    Opera + Tampermonkey
// @compatible    Safari + Tampermonkey • Safari
// @include      *://encrypted.google.*/search*
// @include      *://*.google*/search*
// @include      *://*.google*/webhp*
// @include      *://www.google*/ac-notexist*
// @include      http://www.baidu.com/*
// @include      https://www.baidu.com/*
// @license      MPL-2.0
// @create          2015-10-07
// @copyright    2015-2020, F9y4ng
// @grant      none

// ==/UserScript==

if ("undefined" == typeof (jQuery)) {
    loadJs("for_google", "https://code.jquery.com/jquery-1.7.2.min.js", callbackFunction);
}
else {
    $(document).ready(function () {
        function baiduswitchgoogle() {
            if(document.querySelector("#ggyx") == null){
                $('.s_btn_wr').after('<div class="s_btn_wr bg" style="width:0px;display:inline-block;margin-left:10px"><input type="button" id="ggyx" value="Google一下" class="ggyx" ></div>');
                $('.ggyx').css({
                    'width': '100px',
                    'height': '34px',
                    'color': '#fff',
                    'letter-spacing': '1px',
                    'background': '#2866bd',
                    'border': '0',
                    'cursor': 'pointer'
                });
                $('form.fm .s_btn').css({
                    'background': '#2866bd',
                    'border-bottom': '1px solid #4879bd'
                });
                hoverhandle($('form.fm .s_btn'));
                hoverhandle($('#ggyx'));
                $('#ggyx').off("click").on({
                    click: function () {
                        var BSurl = GetUrlParam("tn");
                        switch (BSurl) {
                            case "news":
                                BSurl = "tbm=nws&q=";
                                break;
                            case "vsearch":
                                BSurl = "tbm=vid&q=";
                                break;
                            default:
                                BSurl = "q=";
                        }
                        window.open("https://www.google.com/search?newwindow=1&hl=zh-CN&source=hp&" + BSurl + encodeURIComponent($('#kw').val()));
                        return false;
                    }
                });
            }}
        //2019/12/07 F9y4ng 检测从baidu首页进入的搜索&自动提交
        if (/^http(s)?:\/\/(www\.)?baidu\.com\/$/ig.test(window.location.href)) {
            $("#kw").on("blur", function () {
                if ($('#kw').val().length > 0) {
                    setTimeout(function () {
                        if ($('#ggyx').length < 1 && !(GetUrlParam("wd").length > 0 || GetUrlParam("word").length > 0)) {
                            $('form').submit();
                        }
                    }, 600);
                }
            });
        }
        else {
            if (GetUrlParam("wd").length > 0 || window.location.href.lastIndexOf("/s?") > 0 || GetUrlParam("word").length > 0 || window.location.href.lastIndexOf("/baidu?") > 0) {
                baiduswitchgoogle();
            }
        }
    });
}

function callbackFunction() {
    $(document).ready(function () {
        var browser = {
            versions: function() {
                var u = navigator.userAgent;
                return {
                    Firefox: u.indexOf('Firefox') > -1,
                    Edge: u.indexOf('Edge') > -1,
                    Chrome: u.indexOf('Chrome') > -1 && u.indexOf('Edg') == -1,
                    Microsoft_Edge: u.indexOf('Edg') > -1
                };
            }()
        }
        function googleswitchbaidu() {
            if(document.querySelector("#sfdiv_bd") == null){
                $('button[type="submit"]').closest('div').first().after('<div id="sfdiv_bd" style="position:relative;float:right;height:0px"><button id="bdyx" class="lsbb kpbb" style="cursor:pointer;display:flex;border-radius:8px;border:1px solid #dfe1e5;box-shadow: none;border-radius: 24px;" type="button"><span class="sbico" id="bdyxss" style="color:#fff;"><svg style="fill: #fff;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="18px" height="18px" viewBox="0 0 64 64" xml:space="preserve"><path d="M62.243,53.758L44.658,36.173C46.768,32.602,48,28.449,48,24C48,10.745,37.255,0,24,0S0,10.745,0,24s10.745,24,24,24 c4.449,0,8.602-1.232,12.173-3.342l17.584,17.584C54.843,63.329,56.343,64,58,64c3.314,0,6-2.686,6-6 C64,56.343,63.328,54.843,62.243,53.758z M24,42c-9.941,0-18-8.059-18-18c0-9.941,8.059-18,18-18c9.941,0,18,8.059,18,18 C42,33.941,33.941,42,24,42z"/></svg></span><span id="bdyxwz" style="font-size: 16px; margin-left:26px">百度一下</span></button></div>');
                $('#sfdiv_bd').css({
                    'top': '-46px',
                    'right': '-130px'
                });
                $('#bdyx').css({
                    'width': '120px',
                    'height': '46px'
                });
                $('#sfdiv_bd').off("click").on({
                    click: function () {
                        var kw = $('input[name="q"]').val();
                        //获取属性标签容错
                        if ("undefined" == typeof (kw)) {
                            kw = GetUrlParam("q");
                        }
                        var GSurl = GetUrlParam("tbm");
                        switch (GSurl) {
                            case "isch":
                                GSurl = "https://image.baidu.com/search/index?tn=baiduimage&ie=utf-8&word=";
                                break;
                            case "vid":
                                GSurl = "https://www.baidu.com/sf/vsearch?pd=video&tn=vsearch&ie=utf-8&wd=";
                                break;
                            case "nws":
                                GSurl = "https://www.baidu.com/s?tn=news&rtt=4&bsst=1&cl=2&wd=";
                                break;
                            case "shop":
                                GSurl = "https://b2b.baidu.com/s?fr=wwwt&q=";
                                break;
                            default:
                                GSurl = "https://www.baidu.com/s?ie=utf-8&rqlang=cn&wd=";
                        }
                        window.open(GSurl + encodeURIComponent(kw));
                        return false;
                    }
                });
            }}
        if (window.location.hash.lastIndexOf("q=") > 0 || window.location.search.lastIndexOf("q=") > 0) {
            googleswitchbaidu();
        }
        //2019/05/21 F9y4ng 修正GOOGLE首页自动提交搜索优先级
        if (/^http(s)?:\/\/(www\.)?google\.\w+(\.\w+)?\/$/ig.test(window.location.href) || GetUrlParam("q") === null || GetUrlParam("q") === "") {
            var gfm = $('input[name="q"]');
            if ("undefined" == typeof (gfm)) {
                gfm = $("input[role='combobox']");
            }
            gfm.off('click').on({
                blur: function () {
                    if (gfm.val().length > 0) {
                        setTimeout(function () {
                            $("form").submit();
                        }, 200);
                    }
                }
            });
        }
        if (GetUrlParam("tbm") !== "" && GetUrlParam("tbm") !== null) {
            $('#bdyxwz').css('margin-left', '2px');
        }
        if (GetUrlParam("tbm") == "fin") {
            $('#bdyxwz').css('margin', '-2px 0 0 24px');
        }
        if (browser.versions.Firefox || browser.versions.Edge) {
            $('#bdyxwz').css('margin-top', '7px');
            $('#bdyxss').css('margin', '12px 2px 0 4px');
        }
        else {
            $('#bdyxwz').css('margin-top', '0px');
            $('#bdyxss').css('margin', '4px 2px 0 4px');
        }
        var elm = $('#bdyx');
        var startPos = $(elm).offset().top;
        $.event.add(window, "scroll", function () {
            var p = $(window).scrollTop();
            $(elm).css('height', ((p) > startPos - 0) ? '37px' : '46px');
            $(elm).css('margin-top', ((p) > startPos - 0) ? '11px' : '1px');
            if (browser.versions.Firefox || browser.versions.Edge) {
                $('#bdyxwz').css('margin-top', ((p) > startPos) ? '3px' : '7px');
                $('#bdyxss').css('margin', ((p) > startPos) ? '8px 2px 0 4px' : '13px 2px 0 4px');
            }
            if (GetUrlParam("tbm") !== "" && GetUrlParam("tbm") !== null) {
                $('#bdyx').css('margin-top', ((p) > startPos) ? '3px' : '1px');
            }
        });
    });
}

function loadJs(sid, jsurl, callback) {
    var nodeHead = document.getElementsByTagName('head')[0];
    var nodeScript = null;
    if (document.getElementById(sid) === null) {
        nodeScript = document.createElement('script');
        nodeScript.setAttribute('type', 'text/javascript');
        nodeScript.setAttribute('src', jsurl);
        nodeScript.setAttribute('id', sid);
        if (callback !== null) {
            nodeScript.onload = nodeScript.onreadystatechange = function () {
                if (nodeScript.ready) {
                    return false;
                }
                if (!nodeScript.readyState || nodeScript.readyState == "loaded" || nodeScript.readyState == 'complete') {
                    nodeScript.ready = true;
                    callback();
                }
            };
        }
        nodeHead.appendChild(nodeScript);
    }
    else {
        if (callback !== null) {
            callback();
        }
    }
}

function hoverhandle(e) {
    e.hover(function () {
        e.css({
            'background': '#4f7fbf',
            'border-bottom': '1px solid #2866bd'
        })
    }, function () {
        e.css({
            'background': '#2866bd',
            'border-bottom': '1px solid #4879bd'
        })
    });
}

function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;
        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");
            if (arr !== null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
}