Skip to content

Commit c484bf8

Browse files
use mutation observer to wait for iframe.giscus-frame
because safari will run before giscus.js has created everything
1 parent 8466b62 commit c484bf8

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/resources/formats/html/giscus/giscus.ejs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@
1414
async>
1515
</script>
1616
<script type="application/javascript">
17-
window.document.addEventListener("DOMContentLoaded", function (event) {
18-
var giscusIframe = window.document.querySelector('iframe.giscus-frame');
19-
giscusIframe.addEventListener("load", function() {
20-
window.setTimeout(() => {
21-
window.toggleGiscusIfUsed(window.hasAlternateSentinel(), authorPrefersDark);
22-
}, 100);
17+
const giscusIframeObserver = new MutationObserver(function (mutations) {
18+
mutations.forEach(function (mutation) {
19+
mutation.addedNodes.forEach(function (addedNode) {
20+
if (addedNode.matches && addedNode.matches('div.giscus')) {
21+
const giscusIframe = addedNode.querySelector('iframe.giscus-frame');
22+
if(giscusIframe) {
23+
giscusIframe.addEventListener("load", function() {
24+
window.setTimeout(() => {
25+
window.toggleGiscusIfUsed(window.hasAlternateSentinel(), authorPrefersDark);
26+
}, 100);
27+
});
28+
giscusIframeObserver.disconnect();
29+
}
30+
}
31+
});
2332
});
2433
});
34+
giscusIframeObserver.observe(document.body, { childList: true, subtree: true });
2535
</script>
2636
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
2737
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">

0 commit comments

Comments
 (0)