AC-百度Favicon

百度Favicon、谷歌Favicon

当前为 2017-01-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name AC-百度Favicon
// @namespace BlockKafanTopicinGoogle
// @include        /^https?://www.baidu.com/.*/
// @include        /^https?://www.bing.com/.*/
// @include        /^https?://encrypted.google.[^\/]+/
// @include        /^https?://www.google.[^\/]+/
// @icon    https://coding.net/u/zb227/p/zbImg/git/raw/master/img0/icon.jpg
// @author       AC
// @version 0.4.0
// @connect https?://[\S]+
// @description 百度Favicon、谷歌Favicon
// @note 0.4.0 修复百度的各种图标丢失问题
// @grant none

// ==/UserScript==

//===================================================普通规则变量定义=======================================================

//===================================================主入口=======================================================

mo = new MutationObserver(function(allmutations) {
    blockKafanBaidu();
});
targets = document.body;
var fatherName = new Array(
    "c-container", //baidu
    "rc", //google
    "b_algo" //bing
);
mo.observe(targets, {'childList': true, 'subtree': true});

//document.addEventListener('DOMNodeInserted',blockKafanBaidu,false);
function blockKafanBaidu() {
    var citeList;
    if(location.href.indexOf(".baidu.com") > -1){
        citeList = document.querySelectorAll(".c-showurl"); 
        //之前取的是g,但这个标签在google中是最大的标签,导致谷歌页面卡住,所以先判断是baidu站点
    }else if(location.href.indexOf(".google.com") > -1){
        citeList = document.querySelectorAll("._Rm"); 
        //之前取的是g,但这个标签在google中是最大的标签,导致谷歌页面卡住,所以先判断是baidu站点
    }else if(location.href.indexOf(".bing.com") > -1){
        citeList = document.querySelectorAll(".b_attribution>cite");
    }
    deal(citeList);
}

// 传入nodelist,然后查找两个列,查看是否一致,一致则删除
function deal(citeList){
    for (var index = 0; index < citeList.length; index++) {
        var url = replaceAll(citeList[index].innerHTML);
        if(null == citeList[index].getAttribute("deal")){
            deal_fatherNode(citeList[index], getFaviconUrl(url));
        }
    }
}

function deal_fatherNode(node, faviconUrl){
    // faviconUrl = "http://"+faviconUrl+"/cdn.ico?defaulticon=http://soz.im/favicon.ico";
    var curNode = node;
    for(II = 0; II <= 5; II++){
        curNode = curNode.parentNode;
        if(isInUrlList(curNode.className)){
            break;
        }
    }
    
    if(II <= 5){
        var pos = curNode.innerHTML.indexOf("fav-url");
        //他自己已经做了favicon了
        if(pos > 0)
            return;
        //var imgHTML = "<img class=\"faviconT\" style=\"vertical-align:sub;\" src=\"http://g.soz.im/"+faviconUrl+"\" height=20 width=20>&nbsp;";
        var imgHTML = "<img class=\"faviconT\" style=\"vertical-align:sub;\" src=\"http://"+faviconUrl+"\" height=20 width=20 onerror=\"this.src='http://code.taobao.org/svn/zb227IMG/favicon.ico'\"/>&nbsp;";
        var insNode = curNode.firstChild;
        if(insNode.length > 0){ //如果是false,那么就是正常节点,否则为文字节点
            insNode = curNode.childNodes[1];
        }
        insNode.innerHTML = imgHTML + insNode.innerHTML;
        node.setAttribute("deal", "1");
    }
     
}
/*去掉网址中的<xxx>*/
function replaceAll(sbefore){
	var send;
	send = sbefore.replace(/<[^>]*>/g ,"");
	return send;
}
function getFaviconUrl(citeUrl){
    var citeUrl = citeUrl.replace(/https?:\/\//g,"");
    var citeUrl = citeUrl.replace(/( |\/).*/g,"");
    return citeUrl+"/favicon.ico";
}
function isInUrlList(url){
    var leng = fatherName.length;
    for(var i = 0; i < leng; i++){
        if(url.indexOf(fatherName[i]) > 0){
            return true;
        }
    }
    return false;
}