diff options
| author | Alejandro Soto <alejandro@34project.org> | 2025-09-13 14:55:15 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2025-09-13 14:55:15 -0600 |
| commit | 87f85704af1fa866be83077b2b351c1fdad7c3ce (patch) | |
| tree | b49c8d49ef717e539bff6578992e692eea55d380 /themes/tabi-lean/static/js/utterances.js | |
| parent | 1a4c3216f027d6a6f36104547377b7b21faa5015 (diff) | |
| parent | a2ea06d513a5802964f8f0ef5795cec7e548ed7b (diff) | |
Merge commit 'a2ea06d513a5802964f8f0ef5795cec7e548ed7b' as 'themes/tabi-lean'
Diffstat (limited to 'themes/tabi-lean/static/js/utterances.js')
| -rw-r--r-- | themes/tabi-lean/static/js/utterances.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/themes/tabi-lean/static/js/utterances.js b/themes/tabi-lean/static/js/utterances.js new file mode 100644 index 0000000..ddfa35a --- /dev/null +++ b/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(); |
