diff --git a/_includes/comments/giscus.html b/_includes/comments/giscus.html
index 805847202..2d5f7e7bf 100644
--- a/_includes/comments/giscus.html
+++ b/_includes/comments/giscus.html
@@ -39,16 +39,25 @@
addEventListener('message', (event) => {
if (event.source === window && event.data && event.data.id === Theme.ID) {
const newTheme = themeMapper[Theme.visualState];
-
const message = {
setConfig: {
theme: newTheme
}
};
- const giscus =
- document.getElementsByClassName('giscus-frame')[0].contentWindow;
- giscus.postMessage({ giscus: message }, 'https://giscus.app');
+ const iframe = document.querySelector('.giscus-frame');
+
+ if (!iframe) {
+ return;
+ }
+
+ if (iframe.classList.contains('giscus-frame--loading')) {
+ let url = new URL(iframe.src);
+ url.searchParams.set('theme', newTheme);
+ iframe.src = url.toString();
+ }
+
+ iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
});
})();