From 2acf14ecf00e6603cec0ab3f63578884bec4c740 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 14 Sep 2021 20:45:17 +0800 Subject: [PATCH 1/2] Replace Disqus lazy loading plugin with native JS --- _includes/disqus.html | 36 +++++++++++++++--------- assets/js/lib/jquery.disqusloader.min.js | 8 ------ 2 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 assets/js/lib/jquery.disqusloader.min.js diff --git a/_includes/disqus.html b/_includes/disqus.html index 0f32888..e3b83a0 100644 --- a/_includes/disqus.html +++ b/_includes/disqus.html @@ -1,25 +1,33 @@ -
-

+

+

Comments powered by Disqus.

- - diff --git a/assets/js/lib/jquery.disqusloader.min.js b/assets/js/lib/jquery.disqusloader.min.js deleted file mode 100644 index b2cb0be..0000000 --- a/assets/js/lib/jquery.disqusloader.min.js +++ /dev/null @@ -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&&ra.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); \ No newline at end of file From 7ef88c8b8f7f5adf3885057ba5cf2194730cf2e8 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 14 Sep 2021 20:45:17 +0800 Subject: [PATCH 2/2] Auto switch Disqus theme --- _includes/disqus.html | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/_includes/disqus.html b/_includes/disqus.html index e3b83a0..5879ac6 100644 --- a/_includes/disqus.html +++ b/_includes/disqus.html @@ -29,5 +29,26 @@ } }, { threshold: [0] }); - disqus_observer.observe(document.querySelector("#disqus_thread")); + disqus_observer.observe(document.querySelector('#disqus_thread')); + + /* Auto switch theme */ + + function reloadDisqus() { + /* Disqus hasn't been loaded */ + if (typeof DISQUS === "undefined") { + return; + } + + if (document.readyState == 'complete') { + DISQUS.reset({ reload: true, config: disqus_config }); + } + } + + const modeToggle = document.querySelector(".mode-toggle"); + + if (modeToggle !== null) { + modeToggle.addEventListener('click', reloadDisqus); + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', reloadDisqus); + } +