Greasy Fork is available in English.

AC-baidu: 优化百度、搜狗、谷歌搜索结果之重定向去除+去广告+favicon

1.繞過百度、搜狗搜索結果中的自己的跳轉鏈接,直接訪問原始網頁-反正都能看懂 2.去除百度的多余广告3.添加Favicon显示

Verzia zo dňa 24.06.2017. Pozri najnovšiu verziu.

// ==UserScript==
// @name AC-baidu: 优化百度、搜狗、谷歌搜索结果之重定向去除+去广告+favicon
// @icon            https://coding.net/u/zb227/p/zbImg/git/raw/master/img0/icon.jpg
// @grant		    GM_xmlhttpRequest
// @author          AC
// @create          2015-11-25
// @run-at          document-start
// @version         9.8
// @connect         *
// @include         http://www.baidu.com/*
// @include         https://www.baidu.com/*
// @include         http://www.sogou.com/*
// @include         https://www.sogou.com/*
// @include         /^https?://\w+.bing.com/.*/
// @include         /^https?\:\/\/encrypted.google.[^\/]+/
// @include         /^https?\:\/\/www.google.[^\/]+/
// @include         https://*.zhidao.baidu.com/*
// @include         https://zhidao.baidu.com/*
// @home-url        https://greasyfork.org/zh-TW/scripts/14178
// @namespace       1353464539@qq.com
// @copyright       2017, AC
// @description     1.繞過百度、搜狗搜索結果中的自己的跳轉鏈接,直接訪問原始網頁-反正都能看懂 2.去除百度的多余广告3.添加Favicon显示
// @lastmodified    2017-06-24
// @feedback-url    https://greasyfork.org/zh-TW/scripts/14178
// @note            2017.06.24-V9.8 更新了bing上的图片favicon地址;并且尽量减少了MO触发次数,避免页面卡顿;修复搜狗的重定向问题
// @note            2017.06.23-V9.7 上传错了~重新来,顺带处理了谷歌favicon问题
// @note            2017.06.23-V9.6 修复了谷歌重定向的问题~~我的锅
// @note            2017.06.11-V9.5 不再使用DOM方式来监听页面了,使用timer+MO来处理,极大减少了cpu占用时间和瞬时网速占用
// @note            2017.05.26-V9.4 恢复favicon模式,现在这个脚本基本上全了,有需要以后再说
// @note            2017.05.15-V9.3 暂时移除百度右边侧栏的广告移除模式,准备下次优化好了再继续
// @note            2017.05.12-V9.2 暂时移除谷歌的安全搜索模式,因为很复杂的原因
// @note            2017.05.12-V9.1 暂时移除Favicon的显示,因为这样就杂了,有需要的人自己去装这个脚本吧,保留百度去广告
// @note            2017.05.12-V9.0 集合了去重定向+去广告+Favicon显示
// @note            2017.05.12-V8.7 集合了去广告的脚本,以前的那个去广告的脚本就不用了
// @note            2017.05.12-V8.6 修复谷歌安全搜索的BUG V2
// @note            2017.05.12-V8.4 新增:默认屏蔽谷歌的安全搜索功能
// @note            2017.05.05-V8.3 修复include范围太小导致的百度知道的屏蔽问题
// @note            2017.05.04-V8.2 终于修复了百度知道图片替换了文字的这个大BUG; 顺便处理了superapi.zhidao.baidu.com; 新增谷歌搜索结果重定向去除
// @note            2017.05.04-V8.1 终于修复了百度知道图片替换了文字的这个大BUG,顺便处理了superapi.zhidao.baidu.com
// @note            2017.05.04-V8.0 终于修复了百度知道图片替换了文字的这个大BUG,待测试
// @note            2017.03.28-V7.6 修复在ViolentMonkey上的不支持的问题
// @note            2017.03.28-V7.5 尝试修复chrome上的问题
// @note            2017.03.21-V7.4 尝试处理Edge上不支持的问题,结果发现是Edge本身的TamperMonkey支持有问题
// @note            2017.03.19-V7.3 修复打开百度之后再次点击“百度一下”导致的无法更新重定向问题
// @note            2017.03.19-V7.2 未知原因chrome的MutationObserver无法使用了,继续回归以前的DOMNodeInserted
// @note            2017.02.17-V7.0 修复搜狗的搜索结果重定向问题+改个名字
// @note            2017.02.17-V6.9 修复搜狗的搜索结果重定向问题
// @note            2016.12.10-V6.8 ***
// @note            2016.10.27-V6.7 修复了以前的重复请求,现在的请求数应该小了很多,网络也就不卡了,感觉萌萌哒
// @note            2016.04.24-V6.6 恢复以前的版本,因为兼容性问题
// @note            2015.12.01-V5.0 加入搜狗的支持,但是支出不是很好
// @note            2015.11.25-V2.0 优化,已经是真实地址的不再尝试获取
// @note            2015.11.25-V1.0 完成去掉百度重定向的功能
// ==/UserScript==

// 初次:还是采用了setInterval来处理,感觉这样的话速度应该比Dom快,也比MO适用,因为MO需要在最后才能调用,实用性还不如timer
// 之后:还是采用MO的方式来处理
(function(){
    var fatherName = new Array(
        "c-container", //baidu1
        "rc", //google
        "b_algo", //bing1
        "b_ans", //bing2
        "vrwrap", //sogou1
        "rb"//sogou2
    );
    var Stype; // 去重定向的选择
    var Ftype; // favicon的选择
    var moMinDeltaSize=5;
    var ACMO = window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;
    var option = {'childList':true,'subtree':true};
    var observer = new ACMO(function(records){
        if(records.length > moMinDeltaSize && records.length<100){
            console.log("in"+records.length);
            ACHandle();
        }
    });
    if (location.host.indexOf("baidu") > -1) {
        Stype = "h3.t>a";
        Ftype = ".f13 .c-showurl[href],.c-container>div[class^='c-'] .c-showurl[href],.c-container>div[class^='c-'] .texttolink[href]";
        startBaidu();
        moMinDeltaSize = 1;
    } else if (location.host.indexOf("sogou") > -1) {
        Stype = "h3.pt>a, h3.vrTitle>a";
        Ftype = "cite[id*='cacheresult_info_']";
        srartOthers();
        moMinDeltaSize = 1;
    } else if (location.host.indexOf("google") > -1){
        Stype = "";
        Ftype = "._Rm";
        srartOthers();
    } else if (location.host.indexOf("bing") > -1){
        Stype = "";
        Ftype = ".b_attribution>cite";
        srartOthers();
        moMinDeltaSize = 9;
    } else {
        addStyle(".word-replace{display: none  !important;}");
        return;
    }
    function startBaidu(){
        var tt = setInterval(function(){
            if(document.querySelector("#content_left")){
                clearInterval(tt);
                /***最后必须要设置好MO继续监听页面数据--自动加载下一页的问题***/
                observer.observe(document.querySelector("#wrapper_wrapper"), option);
                // 处理当前-可以开始设置那个xxx了
                ACHandle();
            }
        }, 200);
    }
    function srartOthers(){
        observer.observe(document, option);
    }
    function ACHandle(){
        console.log("deal");
        if(Stype != null && Stype != "")
            resetURL(document.querySelectorAll(Stype)); // 百度去重定向
        addFavicon(document.querySelectorAll(Ftype)); // 添加Favicon显示
        removeOnMouseDownFunc(); // 移除onMouseDown事件,谷歌去重定向
        removeAD_baidu(); // 移除百度广告
    };
    function removeOnMouseDownFunc(){
        try{
            document.querySelectorAll(".g .rc .r a").forEach(function(one){
               one.setAttribute("onmousedown", "");
            });
        }catch(e){}
    }
    function addStyle(css) { //添加CSS的代码--copy的
        var pi = document.createProcessingInstruction(
            'xml-stylesheet',
            'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(css) + '"'
        );
        return document.insertBefore(pi, document.documentElement);
    }
    function resetURL(list){
        for(var i = 0; i < list.length; i++){
            // 此方法是异步,故在结束的时候使用i会出问题-严重!
            // 采用闭包的方法来进行数据的传递
            var curhref = list[i].href;
            if(list[i]!= null && list[i].getAttribute("ac_redirectStatus") == null){
                list[i].setAttribute("ac_redirectStatus", "0");
                if(curhref.indexOf("baidu.com") > -1 || curhref.indexOf("sogou.com") > -1){
                  (function(c_curhref){
                    setTimeout(function(){
                        GM_xmlhttpRequest({
                            url: c_curhref,
                            headers: {
                                "Accept": "text/html"
                            },
                            method: "GET",
                            onreadystatechange:function(response) {
                                if(response.status==200){
                                    DealResult(response, c_curhref);
                                }
                            }
                        });
                    },100);
                  })(curhref); //传递旧的网址过去,读作c_curhref
                }else if(curhref.indexOf("/interstitial") > -1){
                }else{
                }
            }
        }
    }
    function DealResult(response, c_curhref){ // 数据获取成功,替换页面地址为真实地址
        var resultURL = response.finalUrl;
        if(Stype.length > 10){
            //如果是搜狗的结果
            var resultResponseUrl = Reg_Get(response.responseText, "URL='([^']+)'");
            if(resultResponseUrl != null)
                resultURL = resultResponseUrl;
        }
        //console.log(resultURL);
        var indexhref = Reg_Get(c_curhref, "((?:http)[^&]+)");// 必须要提取部分数据,因为之后的莫名加了其他参数ck=0.0.0.0.....
        var ccnode = document.querySelectorAll("h3>[href*='"+indexhref+"']")[0];
        if(ccnode != null){
            ccnode.href = resultURL;
        }else{
            console.log("该链接已经被其他脚本干掉了哦"+resultURL);
        }
    }
    function Reg_Get(HTML, reg){
        var RegE = new RegExp(reg);
        return RegE.exec(HTML)[1];
    }
    function removeAD_baidu(){ // 移除百度自有广告
        if(location.host == "www.baidu.com"){
            if(document.querySelectorAll("#content_left")[0] != null){
                var fathers = document.querySelectorAll("#content_left")[0].childNodes;
                var lastId = 0;
                for(var i = 0; i < fathers.length; i++){
                    var currentNode = fathers[i];
                    if(fathers[i].tagName=="DIV" && fathers[i].getAttribute("dealAD") == null){
                        if(null == currentNode.id || "" == currentNode.id){
                            // 米有ID的貌似都是广告
                            console.log("移除 CLASS="+currentNode.className);
                            currentNode.remove();
                        } else if(currentNode.id == "clone"){
                            // ID 显示为CLONE的也是广告
                            console.log("移除 ID="+currentNode.id);
                            currentNode.remove();
                        } else if(currentNode.className.indexOf("result") != 0 && /^\d+$/.test(currentNode.id)){
                            // class不是result...的,并且id是纯粹数字的(很大)
                            console.log("移除 ID="+currentNode.id);
                            currentNode.remove();
                        } else{
                            var node = currentNode.querySelectorAll(".f13>span")[0];
                            if(node != null && node.innerHTML == "广告"){
                                console.log("移除 ID="+currentNode.id);
                                currentNode.remove();
                            }
                        }
                        currentNode.setAttribute("dealAD", 1);
                    }
                }
            }
        }
    }
    function addFavicon(citeList){
        for (var index = 0; index < citeList.length; index++) {
            var url = replaceAll(citeList[index].innerHTML);
            //console.log(index+"."+url);
            if(null == citeList[index].getAttribute("ac_faviconStatus")){
                if(url == ""){
                    console.log("无效地址:"+citeList[index].innerHTML);
                    citeList[index].setAttribute("ac_faviconStatus", "-1");
                    continue;
                }
                var curNode = citeList[index];
                var faviconUrl = url;
                for(II = 0; II <= 5; II++){
                    curNode = curNode.parentNode;
                    if(isInUrlList(curNode.className)){
                        break;
                    }
                }
                //console.log(index+"."+faviconUrl+"--"+II);
                if(II <= 5){
                    var tmpHTML = curNode.innerHTML;
                    var pos = tmpHTML.indexOf("fav-url")
                        &tmpHTML.indexOf("favurl")
                        &tmpHTML.indexOf("tit-ico")
                        &tmpHTML.indexOf("img_fav rms_img")
                        &tmpHTML.indexOf("c-tool-")
                        &tmpHTML.indexOf("span class=\"c-icon c-icon-");
                    //他自己已经做了favicon了
                    if(pos > -1){
                        console.log("已有图片:"+faviconUrl);
                        citeList[index].setAttribute("ac_faviconStatus", "-2");
                        continue;
                    }
                    //https://api.byi.pw/favicon/?url=???? 不稳定
                    //http://"+faviconUrl+"/cdn.ico?defaulticon=http://soz.im/favicon.ico 不稳定
                    //https://www.xtwind.com/api/index.php?url=???? 挂了。。。
                    //https://statics.dnspod.cn/proxy_favicon/_/favicon?domain=sina.cn
                    //如果地址不正确,那么丢弃
                    if(curNode.querySelector(".faviconT") == null){
                        var insNode = document.createElement("img");
                        curNode = curNode.children[0]; //firstChild容易遇到text对象
                        citeList[index].setAttribute("ac_faviconStatus", "1");
                        curNode.insertBefore(insNode, curNode.firstChild);
                        insNode.className = "faviconT";
                        insNode.style = "vertical-align:sub;height:16px;width:16px;margin-right:5px";
                        insNode.src = "http://127.0.0.1/baidu.ico";
                        insNode.setAttribute("faviconID", "0");
                        insNode.onerror = (function(host, insNode){
                            var anoHost;
                            try{
                                anoHost = host.replace(/[^.]+\.([^.]+)\.([^.]+)/, "$1.$2");
                            }catch(e){}
                            if(insNode.src == "http://127.0.0.1/baidu.ico"){
                                var img = new Image();
                                img.src = "https://favicon.yandex.net/favicon/"+host;
                                img.onload = function(){
                                    if(img.width < 16){
                                        insNode.src = "https://favicon.yandex.net/favicon/"+anoHost;
                                        //console.log("失败1:"+ img.src + "转到:"+ insNode.src);
                                    } else
                                        insNode.src = "https://favicon.yandex.net/favicon/"+host;
                                };
                            }else if(insNode.src == "http://code.taobao.org/svn/zb227IMG/favicon.ico") return;
                        })(faviconUrl, insNode);
                        insNode.onload = function(){
                            if(insNode.naturalWidth < 16){
                                //console.log("失败2:"+faviconUrl+"暂时无解");
                                insNode.src = "http://code.taobao.org/svn/zb227IMG/favicon.ico";
                            }
                        };
                    }
                }
            }
        }
        function replaceAll(sbefore){
            var send;
            var result = sbefore.split('-');
            if(location.hostname.indexOf("sogou") > -1 && location.href.indexOf("sogou") < 20){
                // --搜狗专用;如果第一个是中文的话,地址就是第二个
                sbefore = result[1];
            }
            send = sbefore.replace(/(\/[^/]*|\s*)/ ,"").replace(/<[^>]*>/g, "").replace(/https?:\/\//g,"").replace(/<\/?strong>/g,"").replace(/<\/?b>/g,"").replace(/<?>?/g,"").replace(/( |\/).*/g,"");
            return send;
        }
        function isInUrlList(url){
            var leng = fatherName.length;
            for(var i = 0; i < leng; i++){
                if(url.indexOf(fatherName[i]) >= 0){
                    return true;
                }
            }
            return false;
        }
    }
})();