|
183 | 183 | }, |
184 | 184 | <%_ } _%> |
185 | 185 | <%_ if (commentSystems.utterances) { _%> |
186 | | - utterances: { |
187 | | - enable: <%= commentSystems.utterances %>, |
188 | | - load: () => { |
189 | | - const container = document.querySelector('.utterances-comment'); |
190 | | - if (!container) return; |
191 | | - container.style.display = 'block'; |
192 | | -
|
193 | | - let theme = '<%= theme.utterances.theme || "github-light" %>'; |
194 | | - const parsedTheme = theme === 'auto' ? (document.documentElement.getAttribute('data-theme') === 'dark' ? 'github-dark' : 'github-light') : theme; |
195 | | -
|
196 | | - // 创建 utterances 脚本 |
197 | | - const script = document.createElement('script'); |
198 | | - script.src = 'https://utteranc.es/client.js'; |
199 | | - script.setAttribute('repo', '<%= theme.utterances.repo %>'); |
200 | | - script.setAttribute('issue-term', '<%= theme.utterances.issue_term || "pathname" %>'); |
201 | | - script.setAttribute('theme', parsedTheme); |
202 | | - script.setAttribute('crossorigin', 'anonymous'); |
203 | | - script.async = true; |
204 | | -
|
205 | | - // 清空容器(防止重复加载)并插入脚本 |
206 | | - container.innerHTML = ''; |
207 | | - container.appendChild(script); |
208 | | - if (theme === 'auto') { |
209 | | - document.body.addEventListener('light-theme-set', () => { |
210 | | - const iframe = container.querySelector('iframe.utterances-frame'); |
211 | | - if (!iframe) return; |
212 | | - iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-light' }, 'https://utteranc.es'); |
213 | | - }); |
214 | | - document.body.addEventListener('dark-theme-set', () => { |
215 | | - const iframe = container.querySelector('iframe.utterances-frame'); |
216 | | - if (!iframe) return; |
217 | | - iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-dark' }, 'https://utteranc.es'); |
218 | | - }); |
| 186 | + utterances: { |
| 187 | + enable: <%= commentSystems.utterances %>, |
| 188 | + load: () => { |
| 189 | + const container = document.querySelector('.utterances-comment'); |
| 190 | + if (!container) return; |
| 191 | + container.style.display = 'block'; |
| 192 | +
|
| 193 | + let theme = '<%= theme.utterances.theme || "github-light" %>'; |
| 194 | + const parsedTheme = theme === 'auto' ? (document.documentElement.getAttribute('data-theme') === 'dark' ? 'github-dark' : 'github-light') : theme; |
| 195 | +
|
| 196 | + // 创建 utterances 脚本 |
| 197 | + const script = document.createElement('script'); |
| 198 | + script.src = 'https://utteranc.es/client.js'; |
| 199 | + script.setAttribute('repo', '<%= theme.utterances.repo %>'); |
| 200 | + script.setAttribute('issue-term', '<%= theme.utterances.issue_term || "pathname" %>'); |
| 201 | + script.setAttribute('theme', parsedTheme); |
| 202 | + script.setAttribute('crossorigin', 'anonymous'); |
| 203 | + script.async = true; |
| 204 | +
|
| 205 | + // 清空容器(防止重复加载)并插入脚本 |
| 206 | + container.innerHTML = ''; |
| 207 | + container.appendChild(script); |
| 208 | + if (theme === 'auto') { |
| 209 | + const lightThemeHandler = () => { |
| 210 | + const iframe = container.querySelector('iframe.utterances-frame'); |
| 211 | + if (!iframe || !iframe.contentWindow) { |
| 212 | + document.body.removeEventListener('light-theme-set', lightThemeHandler); |
| 213 | + return; |
| 214 | + } |
| 215 | + iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-light' }, 'https://utteranc.es'); |
| 216 | + } |
| 217 | + const darkThemeHandler = () => { |
| 218 | + const iframe = container.querySelector('iframe.utterances-frame'); |
| 219 | + if (!iframe || !iframe.contentWindow) { |
| 220 | + document.body.removeEventListener('dark-theme-set', darkThemeHandler); |
| 221 | + return; |
| 222 | + } |
| 223 | + iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-dark' }, 'https://utteranc.es'); |
| 224 | + } |
| 225 | + document.body.addEventListener('light-theme-set', lightThemeHandler); |
| 226 | + document.body.addEventListener('dark-theme-set', darkThemeHandler); |
| 227 | + } |
219 | 228 | } |
220 | | - } |
221 | | - }, |
222 | | - <%_ } _%> |
| 229 | + }, |
| 230 | + <%_ } _%> |
223 | 231 | <%_ if (commentSystems.giscus) { _%> |
224 | 232 | giscus: { |
225 | 233 | enable: <%= commentSystems.giscus %>, |
|
259 | 267 | giscusScript.setAttribute('crossorigin', 'anonymous'); |
260 | 268 | giscusScript.async = true; |
261 | 269 | container.appendChild(giscusScript); |
262 | | - document.body.addEventListener('light-theme-set', () => { |
| 270 | + const lightThemeHandler = () => { |
263 | 271 | const iframe = document.querySelector('iframe.giscus-frame'); |
264 | | - if (!iframe) return; |
| 272 | + if (!iframe || !iframe.contentWindow) { |
| 273 | + document.body.removeEventListener('light-theme-set', lightThemeHandler); |
| 274 | + return; |
| 275 | + } |
265 | 276 | iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: 'light' } } }, 'https://giscus.app'); |
266 | | - }); |
267 | | - document.body.addEventListener('dark-theme-set', () => { |
| 277 | + }; |
| 278 | + const darkThemeHandler = () => { |
268 | 279 | const iframe = document.querySelector('iframe.giscus-frame'); |
269 | | - if (!iframe) return; |
| 280 | + if (!iframe || !iframe.contentWindow) { |
| 281 | + document.body.removeEventListener('dark-theme-set', darkThemeHandler); |
| 282 | + return; |
| 283 | + } |
270 | 284 | iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: 'dark' } } }, 'https://giscus.app'); |
271 | | - }); |
| 285 | + }; |
| 286 | + document.body.addEventListener('light-theme-set', lightThemeHandler); |
| 287 | + document.body.addEventListener('dark-theme-set', darkThemeHandler); |
272 | 288 | } |
273 | 289 | }, |
274 | 290 | <%_ } _%> |
|
0 commit comments