baidu-push.swig 1.1 KB

123456789101112131415161718192021222324252627
  1. {%- if theme.baidu_push %}
  2. <script{{ pjax }}>
  3. (function(){
  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);})();
  26. </script>
  27. {%- endif %}