Skip to content

Commit cdcf711

Browse files
authored
Update baidupush (iissnan#1482)
1 parent f8719b8 commit cdcf711

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

layout/_third-party/baidu-push.swig

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
{%- if theme.baidu_push %}
22
<script{{ pjax }}>
33
(function(){
4-
var bp = document.createElement('script');
5-
var curProtocol = window.location.protocol.split(':')[0];
6-
bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
7-
var s = document.getElementsByTagName("script")[0];
8-
s.parentNode.insertBefore(bp, s);
9-
})();
4+
var canonicalURL, curProtocol;
5+
//Get the <link> tag
6+
var x=document.getElementsByTagName("link");
7+
//Find the last canonical URL
8+
if(x.length > 0){
9+
for (i=0;i<x.length;i++){
10+
if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
11+
canonicalURL=x[i].href;
12+
}
13+
}
14+
}
15+
//Get protocol
16+
if (!canonicalURL){
17+
curProtocol = window.location.protocol.split(':')[0];
18+
}
19+
else{
20+
curProtocol = canonicalURL.split(':')[0];
21+
}
22+
//Get current URL if the canonical URL does not exist
23+
if (!canonicalURL) canonicalURL = window.location.href;
24+
//Assign script content. Replace current URL with the canonical URL
25+
!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
1026
</script>
1127
{%- endif %}

0 commit comments

Comments
 (0)