自定义百度搜索结果页

自定义百度搜索结果

目前为 2016-10-27 提交的版本。查看 最新版本

// ==UserScript==
// @name              自定义百度搜索结果页
// @name:en           custom baidu search
// @compatible        chrome 49.0.2623.75 + TamperMonkey + 脚本 测试通过
// @author            stuart
// @namespace         http://www.shizuwu.cn
// @description       自定义百度搜索结果
// @version           2.1
// @include           http://www.baidu.com/*
// @include           https://www.baidu.com/*
// @grant             none
// @run-at            document-end
// @include-jquery    true
// ==/UserScript==
(function (){
    var clearBaiduSearchAD;
    function getVar (name, url) {
        var vars = [],
            hash;
        if ("undefined" == typeof url || url == "") {
            var url = window.location.href;
        }
        var hashes = url.slice(url.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        if (typeof name != 'undefined') {
            return typeof vars[name] != 'undefined' ? vars[name] : 0;
        } else {
            return vars;
        }
    }
    function insertCss(cssText) {
        var cssNode = $("head").get(0).appendChild(document.createElement('style'));
        cssNode.type = 'text/css';
        cssNode.innerHTML = cssText;
    }
    run();
    
    var kw = getVar("wd");
    function updateCss() {
        $("#rs").css({"margin-bottom":"110px"});
        $("#foot").hide();
        $("#content_left").css({"width":"100%"});
        $("#content_left .result-op, #content_left .result").css({"padding":"10px"});
        $("#page").css({"position":"fixed","bottom":"0px","width":"100%", "background-color":"#ffffff","margin":"0px","padding":"12px 0px 12px 120px"});
    }
    
    function initComp() {
        
        $("body").append('<div class="button_wrap ose so"><a class="slidebttn" title="360搜索">SO</a></div><div class="button_wrap ose google"><a class="slidebttn" title="google搜索">G</a></div>');
        $(".so").on("click", function() {
            window.open('http://www.so.com/s?q='+kw);
        });
        $(".google").on("click", function() {
            window.open('https://www.google.com/search?hl=zh-CN&q='+kw);
        });
        insertCss('.hover{background-color: #fefdbb;box-shadow: 3px 3px 2px #e6e6e6;border-radius: 10px;padding: 10px;}.button_wrap{position:fixed;z-index:36;width:165px;height:30px;bottom:10px;right:170px;overflow:hidden; font-size:12px;}.buttons{width:30px;height:30px;border-radius:30px;background-color:#000;color:#fff;top:0px;right:0px;position:absolute;line-height:30px;text-align:left}div.buttons a{color:#fff;display:none; margin:0px 10px; text-decoration:none;}.buttons a.holder{display:inline-block;width:30px;height:30px;margin:0px;background-color:#fff;border-radius:30px;color:#000;line-height:30px;text-align:center;cursor:pointer}.slidebttn{width:28px;height:28px;background-color:#000;border-radius:28px;color:#fff;position:absolute;top:1px;right:1px;text-transform:uppercase;line-height:30px;text-align:center;cursor:pointer}.so{right:90px; z-index:34;}.so a.slidebttn{background-color:#3ba80d;}.google{right:50px; z-index:33;}.google a.slidebttn{background-color:#1285fb;}');
    
    }
    
    function run() {
        var finding = null;
        clearBaiduSearchAD = setInterval(function () {
            if ($('.google').size() == 0) {
                $("#content_right").hide();
                kw = $("#kw").val();
                updateCss();
                initComp();
                $("#content_left .result-op, #content_left .result").hover(function(e) {
                    $(this).addClass("hover");
                }, function(e) {
                    $(this).removeClass("hover");
                })
            }
            if ($('[data-tuiguang*="推广"]').closest('div').length != 0) {
                $('[data-tuiguang*="推广"]').closest('div').empty();
            }
            try {
                find();
            } catch (error) {
            }
        }, 50);

        function find() {
            if (finding === null) {
                for (var i = 0; i < $('span').length; i++) {
                    if ($('span').eq(i).text()==="广告") {
                        finding = $('span').eq(i);
                    }
                }
            }
            if (finding.parent().attr('id') != "content_left") {
                finding = finding.parent();
            }
            else {
                finding.empty();
                finding=null;
            }
        }

        setTimeout(function () {
            clearInterval(clearBaiduSearchAD);
        }, 10000);
    }
    $('form').keyup(function(event){
        clearInterval(clearBaiduSearchAD);
        run();
    });
    $('form').submit(function(){
        clearInterval(clearBaiduSearchAD);
        run();
    });
    $('#page').click(function (){
        clearInterval(clearBaiduSearchAD);
        run();
    });
})();