Skip to content

Commit 894526c

Browse files
authored
switch to MutationObserver
1 parent a51dd65 commit 894526c

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed
Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
22
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
33
<script>
4-
async function loadGiscusWhenReady() {
5-
while (!document.body.classList.contains('quarto-light') && !document.body.classList.contains('quarto-dark')) {
6-
await new Promise(resolve => setTimeout(resolve, 50));
7-
}
8-
4+
function loadGiscusWhenReady() {
95
// Function to get the theme based on body class
106
const getTheme = () => {
117
const baseTheme = document.getElementById('giscus-base-theme').value;
@@ -14,25 +10,44 @@ async function loadGiscusWhenReady() {
1410
};
1511
1612
// Create the Giscus script and add it to the desired location
17-
const script = document.createElement("script");
18-
script.src = "https://giscus.app/client.js";
19-
script.async = true;
20-
script.dataset.repo = "<%- giscus.repo %>";
21-
script.dataset.repoId = "<%- giscus['repo-id'] %>";
22-
script.dataset.category = "<%- giscus.category %>";
23-
script.dataset.categoryId = "<%- giscus['category-id'] %>";
24-
script.dataset.mapping = "<%- giscus.mapping %>";
25-
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
26-
script.dataset.emitMetadata = "0";
27-
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
28-
script.dataset.theme = getTheme();
29-
script.dataset.lang = "<%- giscus.language %>";
30-
script.crossOrigin = "anonymous";
13+
const loadGiscus = () => {
14+
const script = document.createElement("script");
15+
script.src = "https://giscus.app/client.js";
16+
script.async = true;
17+
script.dataset.repo = "<%- giscus.repo %>";
18+
script.dataset.repoId = "<%- giscus['repo-id'] %>";
19+
script.dataset.category = "<%- giscus.category %>";
20+
script.dataset.categoryId = "<%- giscus['category-id'] %>";
21+
script.dataset.mapping = "<%- giscus.mapping %>";
22+
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
23+
script.dataset.emitMetadata = "0";
24+
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
25+
script.dataset.theme = getTheme();
26+
script.dataset.lang = "<%- giscus.language %>";
27+
script.crossOrigin = "anonymous";
3128
32-
// Append the script to the desired div instead of at the end of the body
33-
document.getElementById("quarto-content").appendChild(script);
34-
}
29+
// Append the script to the desired div instead of at the end of the body
30+
document.getElementById("quarto-content").appendChild(script);
31+
};
3532
36-
// Call the async function to start the loop
33+
// MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
34+
const observer = new MutationObserver((mutations) => {
35+
for (const mutation of mutations) {
36+
if (mutation.type === "attributes" && mutation.attributeName === "class") {
37+
if (document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark')) {
38+
loadGiscus();
39+
observer.disconnect(); // Stop observing once Giscus is loaded
40+
break;
41+
}
42+
}
43+
}
44+
});
45+
46+
// Start observing the body for class attribute changes
47+
observer.observe(document.body, {
48+
attributes: true,
49+
attributeFilter: ["class"],
50+
});
51+
}
3752
loadGiscusWhenReady();
3853
</script>

0 commit comments

Comments
 (0)