搜索引擎一键切换 soTab

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

目前为 2016-02-25 提交的版本。查看 最新版本

// ==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      *.haosou.com/*
// @include      *.bing.com/*
// @include      *.zhihu.com/search?*
// @include      *.soku.com/*
// @include      *.google.com*
// @grant        none
// @run-at       document_end

// @date         10/30/2015
// @modified     02/25/2016
// @version      1.1.1.3
// ==/UserScript==


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

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

    //判断搜索引擎,将仅使用hostname适配
    var site = ["baidu", "bing", "haosou", "", "zhihu", "google", "", "soku"],
        siteName = ["百度", "必应", "好搜", "ALLSO", "知乎", "谷歌", "清澄漫语", "搜库"],
        siteID = -1;
    for (var i = 0; i < site.length; i++) {
        if (site[i] && window.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.haosou", "image.haosou", "wenda.haosou.com/search", "video.haosou"];
            break;
        case 4: //zhihu
            kind = ["", "", ".com/search"];
            break;
        case 5: //google
            kind = ["", "tbm=isch", "", "tbm=vid"];
            break;
        case 7:
            kind[3] = "soku";
            break;
    }
    //0:normal  1:pic  2:zhidao  3:video
    var kindID = -1;
    for (i = 0; i < kind.length; i++) {
        if (kind[i] && window.location.href.indexOf(kind[i]) >= 0) {
            kindID = i;
            break;
        }
    }
    //谷歌特殊处理
    if(siteID==5 && kindID==-1) {
        if(window.location.href.indexOf('q=') >= 0)
            kindID = 0;
    }
    if (kindID == -1) {
        console.log("soTab! no kind found");
        return;
    }

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

    //初始化搜索路径
    var link = []; //link[siteID]
    if (kindID == 0) { //normal
        link = ["//www.baidu.com/s?wd=",
            "//cn.bing.com/search?q=",
            "//www.haosou.com/s?q=",
            "//hzy.pw/allso/?so=",
            "",
            "https://www.google.com/#q="];
    }
    else if (kindID == 1) { //pic
        link = ["//image.baidu.com/search/index?tn=baiduimage&word=",
            "//cn.bing.com/images/search?q=",
            "//image.haosou.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="];
    }
    else if (kindID == 3) { //video
        link = ["//v.baidu.com/v?ie=utf-8&word=",
            "//video.haosou.com/v?q=",
            "//hzy.pw/dm/?s=",
            "//www.soku.com/v?keyword=",
            "",
            "https://www.google.com/search?tbm=vid&q="];
    }

    //获取搜索词(get通用)
    var key;
    if (siteID == 0) {
        if (window.location.search.indexOf("wd=") >= 0)
            key = "wd";
        else
            key = "word";
    }
    else if (siteID == 7)
        key = "keyword";
    else
        key = "q";
    var tmp = window.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.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!");

}();