搜索引擎一键切换 soTab

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

目前為 2016-03-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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
// @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      *.google.com/*
// @grant        none
// @run-at       document_end

// @date         10/30/2015
// @modified     03/01/2016
// @version      1.1.2.4
// ==/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"],
        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.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;
    }
    //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.so.com/s?q=",
            "http://hzy.pw/allso/?so=",
            "",
            "https://www.google.com/search?q="
        ];
    } 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="
        ];
    } 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 (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.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!");

}