fix: mode-toggle leads to Disqus loading failure (#945)

This commit is contained in:
Song-Zi Vong 2023-03-22 22:22:07 +08:00 committed by GitHub
parent 0d9cec6abc
commit 6fec411c18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
}
@ -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>