Skip to content

Commit 3d4317f

Browse files
committed
fix: prevent memory leak
1 parent 6eae017 commit 3d4317f

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ A combination of [landscape](https://github.com/hexojs/hexo-theme-landscape)、[
5656
- Gitalk
5757
- Giscus
5858
- Disqus
59-
- utterances
59+
- Utterances
6060

6161
### Statistics & Analytics
6262
- 📊 Article reading statistics (Valine / Waline)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
- Gitalk
5858
- Giscus
5959
- Disqus
60-
- utterances
60+
- Utterances
6161

6262
### 统计与分析
6363

_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,14 @@ giscus:
393393
# commentTheme: light # deprecated, because the theme is set by the theme itself
394394
# lang: zh-CN # deprecated, use html.lang instead
395395

396-
# https://disqus.com
396+
# https://utteranc.es
397397
utterances:
398398
enable: false
399399
repo: owner/repo # Change this to "Your GitHub Username/The Repository Name" used for storing blog comments
400400
issue_term: title
401401
theme: auto # auto means to automatically adapt to dark and light themes, you can also use specific themes like github-light, github-dark, preferred-color-scheme, etc.
402402

403+
# https://disqus.com
403404
disqus:
404405
enable: false
405406
shortname: # your disqus shortname

layout/_partial/post/comment.ejs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -183,43 +183,51 @@
183183
},
184184
<%_ } _%>
185185
<%_ 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+
}
219228
}
220-
}
221-
},
222-
<%_ } _%>
229+
},
230+
<%_ } _%>
223231
<%_ if (commentSystems.giscus) { _%>
224232
giscus: {
225233
enable: <%= commentSystems.giscus %>,
@@ -259,16 +267,24 @@
259267
giscusScript.setAttribute('crossorigin', 'anonymous');
260268
giscusScript.async = true;
261269
container.appendChild(giscusScript);
262-
document.body.addEventListener('light-theme-set', () => {
270+
const lightThemeHandler = () => {
263271
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+
}
265276
iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: 'light' } } }, 'https://giscus.app');
266-
});
267-
document.body.addEventListener('dark-theme-set', () => {
277+
};
278+
const darkThemeHandler = () => {
268279
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+
}
270284
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);
272288
}
273289
},
274290
<%_ } _%>

0 commit comments

Comments
 (0)