搜索引擎一键切换 soTab

在常用的搜索引擎页面中添加互相切换的按钮,包括图片、视频、知道搜索。

目前为 2016-05-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         搜索引擎一键切换 soTab
// @namespace    http://hzy.pw
// @description  在常用的搜索引擎页面中添加互相切换的按钮,包括图片、视频、知道搜索。
// @author       Moshel
// @homepageURL  http://hzy.pw/p/1849
// @license      Apache License 2.0
// @supportURL   http://weibo.com/moshel
// @icon         http://q.qlogo.cn/qqapp/100229475/F1260A6CECA521F6BE517A08C4294D8A/100

// @include      *.baidu.com/*
// @exclude      *.baidu.com/link?*
// @include      *.so.com/*
// @include      *.bing.com/*
// @include      *.zhihu.com/search?*
// @include      *.soku.com/*
// @include      *.sogou.com/*
// @include      /^https?://(encrypted|www)\.google\.[a-z,\.]+/.+$/
// @grant        none
// @run-at       document_end

// @date         10/30/2015
// @modified     05/26/2016
// @version      1.2.0.6
// ==/UserScript==


var href0 = "";
! function ajax_fixer() {
    var href = location.href;
    if (href0 != href) {
        var oldDOM = document.getElementById('soTab');
        if (oldDOM) {
            oldDOM.parentNode.removeChild(oldDOM);
        }
        soTab_init();
        href0 = href;
    }
    setTimeout(ajax_fixer, 2222);
}();


function soTab_init() {
    //console.log("soTab开始加载");

    if (top != window) {
        console.log("soTab! not top window");
        return;
    }

    //判断搜索引擎,将仅使用hostname适配
    var site = ["baidu", "bing", "so.com", "", "zhihu", "google", "", "soku", "sogou"],
        siteName = ["百度", "必应", "好搜", "ALLSO", "知乎", "谷歌", "清澄漫语", "搜库", "搜狗"],
        siteID = -1;
    for (var i = 0; i < site.length; i++) {
        if (site[i] && location.hostname.indexOf(site[i]) >= 0) {
            siteID = i;
            break;
        }
    }
    if (siteID == -1) {
        console.log("soTab can't match site.");
        return;
    }

    //判断搜索类型,使用href适配
    var kind = [];
    switch (siteID) {
        case 0:
            kind = ["www.baidu", "image.baidu", "zhidao.baidu.com/search", "v.baidu"];
            break;
        case 1: //bing
            kind = [".com/search", ".com/images", ".com/knows/search", ".com/videos"];
            break;
        case 2:
            kind = ["www.so", "image.so", "wenda.so.com/search", "video.so"];
            break;
        case 4: //zhihu
            kind = ["", "", ".com/search"];
            break;
        case 5: //google
            kind = ["", "tbm=isch", "", "tbm=vid"];
            break;
        case 7:
            kind[3] = "soku";
            break;
        case 8: //sogou
            kind = [[
                "/web?", "weixin.sogou", "english.sogou"
            ], "pic.sogou", [
                "interation=196636", "mingyi.sogou", "wenwen.sogou", ".com/zhihu"
            ], "v.sogou"];
            break;
    }
    //0:normal  1:pic  2:zhidao  3:video
    var kindID = -1;
    for (i = 0; i < kind.length; i++) {
        if (!kind[i])
            continue;
        else if (typeof(kind[i])==='object') { //数组形式
            for (var j=0; j<kind[i].length; j++ )
                if(location.href.indexOf(kind[i][j]) >= 0) {
                    kindID = i;
                    break;
                }
            if(kindID!=-1)
                break;
        }
        else if (location.href.indexOf(kind[i]) >= 0) {
            kindID = i;
            break;
        }
    }
    //谷歌特殊处理
    if (siteID == 5 && kindID == -1) {
        if (location.href.indexOf('q=') >= 0)
            kindID = 0;
    }
    if (kindID == -1) {
        console.log("soTab! no kind found");
        return;
    }

    //console.log("soTab loaded: " + siteID + "." + kindID);

    //初始化搜索路径
    //"百度", "必应", "好搜", "ALLSO", "知乎", "谷歌", "清澄漫语", "搜库", "搜狗"
    var link = []; //link[siteID]
    if (kindID == 0) { //normal
        link = ["//www.baidu.com/s?wd=",
            "//cn.bing.com/search?q=",
            "//www.so.com/s?q=",
            "http://hzy.pw/allso/?so=",
            "",
            "https://www.google.com/search?q=",
            "", "",
            "https://www.sogou.com/web?query="
        ];
    } else if (kindID == 1) { //pic
        link = ["//image.baidu.com/search/index?tn=baiduimage&word=",
            "//cn.bing.com/images/search?q=",
            "//image.so.com/i?q=",
            "", "",
            "https://www.google.com/search?tbm=isch&q="
        ];
    } else if (kindID == 2) { //zhidao
        link = ["//zhidao.baidu.com/search?word=",
            "//cn.bing.com/knows/search?q=",
            "", "",
            "//www.zhihu.com/search?q=",
            "", "", "",
            "http://www.sogou.com/sogou?interation=196636&query="
        ];
    } else if (kindID == 3) { //video
        link = ["//v.baidu.com/v?ie=utf-8&word=",
            "//video.so.com/v?q=",
            "http://hzy.pw/dm/?s=",
            "//www.soku.com/v?keyword=",
            "",
            "https://www.google.com/search?tbm=vid&q="
        ];
    }

    //获取搜索词(get通用)
    var key;
    if (siteID == 0) {
        if (location.search.indexOf("wd=") >= 0)
            key = "wd";
        else
            key = "word";
    } else if (siteID == 7)
        key = "keyword";
    else if (siteID == 8)
        key = (location.search.indexOf("w=") >= 0) ? "w" : 'query';
    else
        key = "q";
    var tmp = location.href.split(key + "=", 2);
    if (tmp.length <= 1) {
        console.log("soTab! no keyword found");
        return;
    }
    var tmp2 = tmp[1];
    tmp = tmp2.split("&", 2);
    key = tmp[0];

    //console.log(key);

    //加载css
    var dom = document.createElement('style'),
        dom_body = document.getElementsByTagName("body")[0];
    dom.innerHTML = ".soTab{position:fixed;background-color:#000;opacity:.3;color:#fff;padding:10px;bottom:30px;height:40px;left:-290px;width:300px;z-index:9999999;transition:all 400ms}.soTab:hover{left:0;opacity:1}.soTab_title{font-weight:bold;margin:0 0 3px}.soTab a{color:#00c4ff}";
    dom_body.appendChild(dom);

    //生成切换框
    dom = document.createElement('div');
    dom.id = "soTab";
    dom.innerHTML = "<p class='soTab_title'>soTab 一键切换引擎:</p>";
    for (i = 0; i < link.length; i++) {
        if (i != siteID && link[i]) {
            dom.innerHTML += "<a href='" + link[i] + key + "' target='_blank'>" + siteName[i] + "</a>、";
        }
    }
    dom.innerHTML = dom.innerHTML.substring(0, dom.innerHTML.length - 1);
    dom.className = "soTab soTab_" + siteID + "_" + kindID;
    dom_body.appendChild(dom);

    //console.log("soTab all run!");

}