blob: ddfa35a02987a29adc336e333029cf14d3a53a20 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
|