summaryrefslogtreecommitdiff
path: root/sysret.org/themes/tabi-lean/static/js/utterances.js
diff options
context:
space:
mode:
authorAlejandro Soto <alejandro@34project.org>2025-09-13 15:01:06 -0600
committerAlejandro Soto <alejandro@34project.org>2025-09-13 15:01:06 -0600
commitf4fcda54638685899c730b3fa90a87d80d6dbef5 (patch)
tree0737e627cce304c3a9c4e757bc5f6571a7456091 /sysret.org/themes/tabi-lean/static/js/utterances.js
parentd8b9cf1f61cc07d625f1c37ccc28adfd58918416 (diff)
parent2c13119932765c6d788f08fb53abc244407c0d80 (diff)
Merge commit '6a7d3111b31e73fc66af5360149d41f690fbcaa4'
Diffstat (limited to 'sysret.org/themes/tabi-lean/static/js/utterances.js')
-rw-r--r--sysret.org/themes/tabi-lean/static/js/utterances.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/sysret.org/themes/tabi-lean/static/js/utterances.js b/sysret.org/themes/tabi-lean/static/js/utterances.js
new file mode 100644
index 0000000..ddfa35a
--- /dev/null
+++ b/sysret.org/themes/tabi-lean/static/js/utterances.js
@@ -0,0 +1,64 @@
+function setUtterancesTheme(newTheme) {
+ // Get the frame with class "utterances-frame".
+ const frame = document.querySelector('.utterances-frame');
+
+ if (frame) {
+ // If the iframe exists, send a message to set the theme.
+ frame.contentWindow.postMessage(
+ { type: 'set-theme', theme: newTheme },
+ 'https://utteranc.es'
+ );
+ }
+}
+
+function initUtterances() {
+ // Get the comments div.
+ const commentsDiv = document.querySelector('.comments');
+
+ // Check if the comments div exists.
+ if (commentsDiv) {
+ // Get all necessary attributes for initializing Utterances.
+ const repo = commentsDiv.getAttribute('data-repo');
+ const issueTerm = commentsDiv.getAttribute('data-issue-term');
+ const label = commentsDiv.getAttribute('data-label');
+ const lightTheme = commentsDiv.getAttribute('data-light-theme');
+ const darkTheme = commentsDiv.getAttribute('data-dark-theme');
+ const lazyLoading = commentsDiv.getAttribute('data-lazy-loading');
+
+ // Create a new script element.
+ const script = document.createElement('script');
+ script.src = 'https://utteranc.es/client.js';
+ script.async = true;
+ script.setAttribute('repo', repo);
+ script.setAttribute('issue-term', issueTerm);
+ script.setAttribute('label', label);
+
+ // Set the initial theme.
+ const currentTheme =
+ document.documentElement.getAttribute('data-theme') || 'light';
+ const selectedTheme = currentTheme === 'dark' ? darkTheme : lightTheme;
+ script.setAttribute('theme', selectedTheme);
+
+ script.setAttribute('crossorigin', 'anonymous');
+
+ // Enable lazy loading if specified.
+ if (lazyLoading === 'true') {
+ script.setAttribute('data-loading', 'lazy');
+ }
+
+ // Append the script to the comments div.
+ commentsDiv.appendChild(script);
+
+ // Listen for themeChanged event to update the theme.
+ window.addEventListener('themeChanged', (event) => {
+ // Determine the new theme based on the event detail.
+ const selectedTheme =
+ event.detail.theme === 'dark' ? darkTheme : lightTheme;
+ // Set the new theme.
+ setUtterancesTheme(selectedTheme);
+ });
+ }
+}
+
+// Initialize Utterances.
+initUtterances();