summaryrefslogtreecommitdiff
path: root/layouts/_partials/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'layouts/_partials/scripts')
-rw-r--r--layouts/_partials/scripts/action-menu-js.html38
1 files changed, 22 insertions, 16 deletions
diff --git a/layouts/_partials/scripts/action-menu-js.html b/layouts/_partials/scripts/action-menu-js.html
index 66673b71..69f5189c 100644
--- a/layouts/_partials/scripts/action-menu-js.html
+++ b/layouts/_partials/scripts/action-menu-js.html
@@ -31,25 +31,31 @@ function initializeComments() {
// 评论数量功能初始化
function initializeCommentCount() {
const commentCountEl = document.getElementById('comment-count');
- const giscusIframe = document.querySelector('iframe.giscus-frame');
- if (!commentCountEl || !giscusIframe) return;
-
- commentCountEl.textContent = '0';
-
- giscusIframe.addEventListener('load', () => {
- window.addEventListener('message', function(event) {
- if (event.origin !== 'https://giscus.app') return;
-
- const giscusData = event.data;
- if (giscusData.giscus?.discussion) {
- const discussion = giscusData.giscus.discussion;
- const totalComments = (discussion.totalCommentCount || 0) + (discussion.totalReplyCount || 0);
- commentCountEl.textContent = totalComments.toString();
- }
- });
+ if (!commentCountEl) return;
+
+ const observer = new MutationObserver(() => {
+ const giscusIframe = document.querySelector('iframe.giscus-frame');
+ if (giscusIframe) {
+ giscusIframe.addEventListener('load', () => {
+ window.addEventListener('message', function(event) {
+ if (event.origin !== 'https://giscus.app') return;
+ const giscusData = event.data;
+ if (giscusData.giscus?.discussion) {
+ const discussion = giscusData.giscus.discussion;
+ const totalComments = (discussion.totalCommentCount || 0) + (discussion.totalReplyCount || 0);
+ commentCountEl.textContent = totalComments.toString();
+ }
+ });
+ });
+ observer.disconnect();
+ }
});
+
+ observer.observe(document.body, { childList: true, subtree: true });
}
+document.addEventListener('DOMContentLoaded', initializeCommentCount);
+
// 点赞功能初始化
function initializeLikes(pageId) {
const elements = {