Skip to content

Commit

Permalink
fix: mode-toggle leads to Disqus loading failure (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
v4vong authored Mar 22, 2023
1 parent 0d9cec6 commit 6fec411
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions _includes/comments/disqus.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
<!--
The Disqus lazy loading.
-->
<!-- The Disqus lazy loading. -->
<div id="disqus_thread" class="pt-2 pb-2">
<p class="text-center text-muted small">
Comments powered by <a href="https://disqus.com/">Disqus</a>.
</p>
<p class="text-center text-muted small">Comments powered by <a href="https://disqus.com/">Disqus</a>.</p>
</div>

<script type="text/javascript">

var disqus_config = function () {
this.page.url = '{{ page.url | absolute_url }}';
this.page.identifier = '{{ page.url }}';
};

/* Lazy loading */
var disqus_observer = new IntersectionObserver(function (entries) {
if(entries[0].isIntersecting) {
var disqus_observer = new IntersectionObserver(
function (entries) {
if (entries[0].isIntersecting) {
(function () {
var d = document, s = d.createElement('script');
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
var d = document,
s = d.createElement('script');
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();

disqus_observer.disconnect();
}
}, { threshold: [0] });
}
},
{ threshold: [0] }
);

disqus_observer.observe(document.querySelector('#disqus_thread'));

/* Auto switch theme */
function reloadDisqus() {
if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {
if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
/* Disqus hasn't been loaded */
if (typeof DISQUS === "undefined") {
if (typeof DISQUS === 'undefined') {
return;
}

Expand All @@ -45,10 +43,7 @@
}
}

const modeToggle = document.querySelector(".mode-toggle");

if (typeof modeToggle !== "undefined") {
window.addEventListener("message", reloadDisqus);
if (document.querySelector('.mode-toggle')) {
window.addEventListener('message', reloadDisqus);
}

</script>

0 comments on commit 6fec411

Please sign in to comment.