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,24 +1,21 @@
<!-- <!-- The Disqus lazy loading. -->
The Disqus lazy loading.
-->
<div id="disqus_thread" class="pt-2 pb-2"> <div id="disqus_thread" class="pt-2 pb-2">
<p class="text-center text-muted small"> <p class="text-center text-muted small">Comments powered by <a href="https://disqus.com/">Disqus</a>.</p>
Comments powered by <a href="https://disqus.com/">Disqus</a>.
</p>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var disqus_config = function () { var disqus_config = function () {
this.page.url = '{{ page.url | absolute_url }}'; this.page.url = '{{ page.url | absolute_url }}';
this.page.identifier = '{{ page.url }}'; this.page.identifier = '{{ page.url }}';
}; };
/* Lazy loading */ /* Lazy loading */
var disqus_observer = new IntersectionObserver(function (entries) { var disqus_observer = new IntersectionObserver(
function (entries) {
if (entries[0].isIntersecting) { if (entries[0].isIntersecting) {
(function () { (function () {
var d = document, s = d.createElement('script'); var d = document,
s = d.createElement('script');
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js'; s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date()); s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s); (d.head || d.body).appendChild(s);
@ -26,16 +23,17 @@
disqus_observer.disconnect(); disqus_observer.disconnect();
} }
}, { threshold: [0] }); },
{ threshold: [0] }
);
disqus_observer.observe(document.querySelector('#disqus_thread')); disqus_observer.observe(document.querySelector('#disqus_thread'));
/* Auto switch theme */ /* Auto switch theme */
function reloadDisqus() { function reloadDisqus() {
if (event.source === window && event.data && if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
event.data.direction === ModeToggle.ID) {
/* Disqus hasn't been loaded */ /* Disqus hasn't been loaded */
if (typeof DISQUS === "undefined") { if (typeof DISQUS === 'undefined') {
return; return;
} }
@ -45,10 +43,7 @@
} }
} }
const modeToggle = document.querySelector(".mode-toggle"); if (document.querySelector('.mode-toggle')) {
window.addEventListener('message', reloadDisqus);
if (typeof modeToggle !== "undefined") {
window.addEventListener("message", reloadDisqus);
} }
</script> </script>