搜索引擎一键切换 soTab

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

目前為 2016-06-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

// @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?://[a-z]+\.google\.[a-z,\.]+/.+$/

// @date         10/30/2015
// @modified     06/06/2016
// @version      1.3.1.9

// @supportURL   https://github.com/h2y/sotab
// ==/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", "xueshu.baidu.com/s"];
            break;
        case 1: //bing
            kind = [".com/search", ".com/images", ".com/knows/search", ".com/videos", "/academic/search"];
            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", "scholar.google"];
            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", ".com/xueshu"];
            break;
    }
    //0:normal  1:pic  2:zhidao  3:video  4:xueshu
    var kindID = -1;
    for (i = 0; i < kind.length; i++) {
        if (!kind[i])
            continue;
        else if (Array.isArray(kind[i])) { //数组形式
            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://h2y.github.io/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=",
            "", "",
            "https://www.google.com/search?tbm=vid&q=",
            "http://hzy.pw/dm/?s=",
            "//www.soku.com/v?keyword="
        ];
    } else if (kindID == 4) { //xueshu
        link = ["http://xueshu.baidu.com/s?wd=",
            "https://cn.bing.com/academic/search?q=",
            "", "", "",
            "https://scholar.google.com/scholar?q="
        ];
    }

    //获取搜索词(get通用)
    var key;
    if (siteID == 0)
        key = (location.search.indexOf("wd=") >= 0) ? "wd" : "word";
    /*else if (siteID == 7)
        key = "keyword";*/
    else if (siteID == 8)
        key = (location.search.indexOf("query=") >= 0) ? "query" : 'w';
    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;border-radius:40px;color:#fff;padding:15px 20px;bottom:100px;height:40px;left:-320px;width:300px;z-index:9999999;transition:all 400ms}.soTab:hover{left:5px;opacity:1;border-radius:10px;box-shadow:5px -5px 10px #777}.soTab p{margin:0}p.soTab_title{font-weight:bold;margin-bottom:3px}.soTab a{color:#0cf;margin-right:1rem}';
    dom_body.appendChild(dom);

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

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

}