From b0d5956f5a0ed894984d6b1754efeba04d8bc966 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 22 Nov 2022 18:33:04 +0800 Subject: [PATCH] fix(comment): disqus doesn't follow theme mode switching --- _includes/comments/disqus.html | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/_includes/comments/disqus.html b/_includes/comments/disqus.html index 8b4e633..6174d31 100644 --- a/_includes/comments/disqus.html +++ b/_includes/comments/disqus.html @@ -15,7 +15,6 @@ }; /* Lazy loading */ - var disqus_observer = new IntersectionObserver(function (entries) { if(entries[0].isIntersecting) { (function () { @@ -32,23 +31,24 @@ disqus_observer.observe(document.querySelector('#disqus_thread')); /* Auto switch theme */ - function reloadDisqus() { - /* Disqus hasn't been loaded */ - if (typeof DISQUS === "undefined") { - return; - } + if (event.source === window && event.data && + event.data.direction === ModeToggle.ID) { + /* Disqus hasn't been loaded */ + if (typeof DISQUS === "undefined") { + return; + } - if (document.readyState == 'complete') { - DISQUS.reset({ reload: true, config: disqus_config }); + if (document.readyState == 'complete') { + DISQUS.reset({ reload: true, config: disqus_config }); + } } } const modeToggle = document.querySelector(".mode-toggle"); if (typeof modeToggle !== "undefined") { - /* modeToggle.addEventListener('click', reloadDisqus); // not pretty for 'color-scheme' */ - window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', reloadDisqus); + window.addEventListener("message", reloadDisqus); }