Replace Disqus lazy loading plugin with native JS

This commit is contained in:
Cotes Chung 2021-09-14 20:45:17 +08:00
parent 603705f20e
commit 2acf14ecf0
2 changed files with 22 additions and 22 deletions

View file

@ -1,25 +1,33 @@
<!--
The Disqus lazy loading.
Powered by: <https://github.com/osvaldasvalutis/disqusLoader.js>
-->
<div id="disqus" class="pt-2 pb-2">
<p class="text-center text-muted small pb-5">
<div id="disqus_thread" class="pt-2 pb-2">
<p class="text-center text-muted small mb-5">
Comments powered by <a href="https://disqus.com/">Disqus</a>.
</p>
</div>
<script src="{{ '/assets/js/lib/jquery.disqusloader.min.js' | relative_url }}"></script>
<script>
const options = {
scriptUrl: '//{{ site.disqus.shortname }}.disqus.com/embed.js',
disqusConfig: function() {
this.page.title = '{{ page.title }}';
this.page.url = '{{ page.url | absolute_url }}';
this.page.identifier = '{{ page.url }}';
}
<script type="text/javascript">
var disqus_config = function () {
this.page.url = '{{ page.url | absolute_url }}';
this.page.identifier = '{{ page.url }}';
};
$.disqusLoader('#disqus', options);
/* Lazy loading */
var disqus_observer = new IntersectionObserver(function (entries) {
if(entries[0].isIntersecting) {
(function () {
var d = document, s = d.createElement('script');
s.src = 'https://{{ site.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
disqus_observer.disconnect();
}
}, { threshold: [0] });
disqus_observer.observe(document.querySelector("#disqus_thread"));
</script>

View file

@ -1,8 +0,0 @@
/*!
disqusLoader.js v1.0
A JavaScript plugin for lazy-loading Disqus comments widget.
-
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/
(function(e,g,h,d){var a=e(g),k=function(o,n){var q,p;return function(){var t=this,s=arguments,r=+new Date;q&&r<q+o?(clearTimeout(p),p=setTimeout(function(){q=r,n.apply(t,s)},o)):(q=r,n.apply(t,s))}},m=false,j=false,i=false,c=false,f="unloaded",b=e(),l=function(){if(!b.length||b.data("disqusLoaderStatus")=="loaded"){return true}var n=a.scrollTop();if(b.offset().top-n>a.height()*j||n-b.offset().top-b.outerHeight()-(a.height()*j)>0){return true}e("#disqus_thread").removeAttr("id");b.attr("id","disqus_thread").data("disqusLoaderStatus","loaded");if(f=="loaded"){DISQUS.reset({reload:true,config:i})}else{g.disqus_config=i;if(f=="unloaded"){f="loading";e.ajax({url:c,async:true,cache:true,dataType:"script",success:function(){f="loaded"}})}}};a.on("scroll resize",k(m,l));e.disqusLoader=function(o,n){n=e.extend({},{laziness:1,throttle:250,scriptUrl:false,disqusConfig:false},n);j=n.laziness+1;m=n.throttle;i=n.disqusConfig;c=c===false?n.scriptUrl:c;b=(typeof o=="string"?e(o):o).eq(0);b.data("disqusLoaderStatus","unloaded");l()}})(jQuery,window,document);