diff options
| author | Eltrac <hi@guhub.cn> | 2025-08-29 14:53:15 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-08-29 14:53:15 +0800 |
| commit | d172c38c9d68e219edc64c123d7bae4b2fbf85a2 (patch) | |
| tree | 8011e4683988c7b91c95780081cc009b93569a0a /layouts/_partials/scripts | |
| parent | ed18b52d7102dd09d7f217f3350483935e37df33 (diff) | |
perf: load giscus and comment count lazily
Former-commit-id: f839c9ce6277a5f0bc19e2a1e23a2c225715b1a6
Diffstat (limited to 'layouts/_partials/scripts')
| -rw-r--r-- | layouts/_partials/scripts/action-menu-js.html | 38 |
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 = { |
