解决不蒜子(busuanzi)不显示数字问题记录

问题描述

在使用hexo开发个人博客时, 开启了不蒜子的统计选项, 但是在部署到Github Pages上或者本地使用serve -s 以及python -m SimpleHTTPServer的时候,经常会出现不显示数字问题
由于已经是使用最新的域名来下载的, 应该不存在busuanzi.pure.min.js 的URL不对的情况
使用最新的URL可以直接将脚本下载到本地

解决思路

将busuanzi.pure.min.js下载到本地后,显示这么一大坨

1
var bszCaller,bszTag;!function(){var c,d,e,a=!1,b=[];ready=function(c){return a||"interactive"===document.readyState||"complete"===document.readyState?c.call(document):b.push(function(){return c.call(this)}),this},d=function(){for(var a=0,c=b.length;c>a;a++)b[a].apply(document);b=[]},e=function(){a||(a=!0,d.call(window),document.removeEventListener?document.removeEventListener("DOMContentLoaded",e,!1):document.attachEvent&&(document.detachEvent("onreadystatechange",e),window==window.top&&(clearInterval(c),c=null)))},document.addEventListener?document.addEventListener("DOMContentLoaded",e,!1):document.attachEvent&&(document.attachEvent("onreadystatechange",function(){/loaded|complete/.test(document.readyState)&&e()}),window==window.top&&(c=setInterval(function(){try{a||document.documentElement.doScroll("left")}catch(b){return}e()},5)))}(),bszCaller={fetch:function(a,b){var c="BusuanziCallback_"+Math.floor(1099511627776*Math.random());window[c]=this.evalCall(b),a=a.replace("=BusuanziCallback","="+c),scriptTag=document.createElement("script"),scriptTag.type="text/javascript",scriptTag.defer=!0,scriptTag.src=a,document.getElementsByTagName("head")[0].appendChild(scriptTag)},evalCall:function(a){return function(b){ready(function(){try{a(b); scriptTag.parentElement.removeChild(scriptTag)}catch(c){bszTag.hides()}})}}},bszCaller.fetch("//busuanzi.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback",function(a){bszTag.texts(a),bszTag.shows()}),bszTag={bszs:["site_pv","page_pv","site_uv"],texts:function(a){this.bszs.map(function(b){var c=document.getElementById("busuanzi_value_"+b);c&&(c.innerHTML=a[b])})},hides:function(){this.bszs.map(function(a){var b=document.getElementById("busuanzi_container_"+a);b&&(b.style.display="none")})},shows:function(){this.bszs.map(function(a){var b=document.getElementById("busuanzi_container_"+a);b&&(b.style.display="inline")})}};

经过格式化后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var bszCaller, bszTag;
! function () {
var c, d, e, a = !1,
b = [];
ready = function (c) {
return a || "interactive" === document.readyState || "complete" === document.readyState ? c.call(document) : b.push(function () {
return c.call(this)
}), this
}, d = function () {
for (var a = 0, c = b.length; c > a; a++) b[a].apply(document);
b = []
}, e = function () {
a || (a = !0, d.call(window), document.removeEventListener ? document.removeEventListener("DOMContentLoaded", e, !1) : document.attachEvent && (document.detachEvent("onreadystatechange", e), window == window.top && (clearInterval(c), c = null)))
}, document.addEventListener ? document.addEventListener("DOMContentLoaded", e, !1) : document.attachEvent && (document.attachEvent("onreadystatechange", function () {
/loaded|complete/.test(document.readyState) && e()
}), window == window.top && (c = setInterval(function () {
try {
a || document.documentElement.doScroll("left")
} catch (b) {
return
}
e()
}, 5)))
}(), bszCaller = {
fetch: function (a, b) {
var c = "BusuanziCallback_" + Math.floor(1099511627776 * Math.random());
window[c] = this.evalCall(b), a = a.replace("=BusuanziCallback", "=" + c), scriptTag = document.createElement("script"), scriptTag.type = "text/javascript", scriptTag.defer = !0, scriptTag.src = a, document.getElementsByTagName("head")[0].appendChild(scriptTag)
},
evalCall: function (a) {
return function (b) {
ready(function () {
try {
a(b);
scriptTag.parentElement.removeChild(scriptTag)
} catch (c) {
bszTag.hides()
}
})
}
}
}, bszCaller.fetch("//busuanzi.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback", function (a) {
bszTag.texts(a), bszTag.shows()
}), bszTag = {
bszs: ["site_pv", "page_pv", "site_uv"],
texts: function (a) {
this.bszs.map(function (b) {
var c = document.getElementById("busuanzi_value_" + b);
c && (c.innerHTML = a[b])
})
},
hides: function () {
this.bszs.map(function (a) {
var b = document.getElementById("busuanzi_container_" + a);
b && (b.style.display = "none")
})
},
shows: function () {
this.bszs.map(function (a) {
var b = document.getElementById("busuanzi_container_" + a);
b && (b.style.display = "inline")
})
}
};

于是分析, 其中bszTag起到了控制显示和隐藏功能
搜索 bszTag.hides() 调用
发现了这个部分

1
2
3
4
5
6
try {
a(b);
scriptTag.parentElement.removeChild(scriptTag)
} catch (c) {
bszTag.hides()
}

bszTag.hides() 前加上console日志,

1
2
3
4
5
6
try {
a(b);
scriptTag.parentElement.removeChild(scriptTag)
} catch (c) {
console.error(c), bszTag.hides()
}

然后把js的资源URL替换到站内,再次重新打开页面时, 打印出了报错

call removeChild of null

然后在浏览器打开调试终端, 输入 scriptTag, 打印出了

1
<script type="text/javascript" defer="" src="//busuanzi.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback_95341178475"></script>

再输入 scriptTag.parentElement, 打印出了

1
null

于是确定问题出现在了

1
scriptTag.parentElement.removeChild(scriptTag)

这一行

为了不更改这一行代码的功能, 又要避免出现上面的 removeChild of null 问题

于是修改为

1
2
3
4
5
6
try {
a(b);
if(scriptTag.parentElement) scriptTag.parentElement.removeChild(scriptTag)
} catch (c) {
console.error(c), bszTag.hides()
}

最后重新build并打开本地服务器, 终于显示除了localhost这个使用最广的站点名的访问数字

然后重新压缩脚本, 扔到站内的lib/js目录, 重新build完推送至Github Pages

刷新页面, 终于一切恢复正常

感谢您对本站的支持.