diff options
| author | Eltrac <hi@guhub.cn> | 2025-08-29 15:30:07 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-08-29 15:30:07 +0800 |
| commit | 78fdf540f89e39cbea5d3b22989e746846c6def1 (patch) | |
| tree | 4cd2f4d24e3c7c2434d14f3655312a1ce954753f /layouts/_partials/scripts | |
| parent | 7dd810848d6c7f9885ae8968cd4673a35e062dda (diff) | |
refactor: feature initialization and comment count logic
Former-commit-id: b14e68c36fb6c9bc44390c42ee4617cedb65bbd7
Diffstat (limited to 'layouts/_partials/scripts')
| -rw-r--r-- | layouts/_partials/scripts/action-menu-js.html | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/layouts/_partials/scripts/action-menu-js.html b/layouts/_partials/scripts/action-menu-js.html index a0d9391d..3a7611c3 100644 --- a/layouts/_partials/scripts/action-menu-js.html +++ b/layouts/_partials/scripts/action-menu-js.html @@ -9,15 +9,6 @@ * ================= */ -function initializeFeatures() { - const pageId = "{{ $pageId }}"; - if (!pageId) return; - - initializeComments(); - initializeLikes(pageId); - initializeCommentCount(); -} - /** * ================= * Toggle Comment Section @@ -62,28 +53,29 @@ function initializeCommentCount() { if (!commentCountEl) return; const observer = new MutationObserver(() => { + // check if giscus is loaded 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(); - } - }); + if (!giscusIframe) return; + + giscusIframe.addEventListener('load', () => { + // receive message from giscus iframe + 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); + // set comment count + commentCountEl.textContent = totalComments.toString(); + } }); - observer.disconnect(); - } - }); + }); + observer.disconnect(); + }); observer.observe(document.body, { childList: true, subtree: true }); } -document.addEventListener('DOMContentLoaded', initializeCommentCount); - /** * ================= * Initialize like functionality @@ -168,11 +160,14 @@ function initializeLikes(pageId) { * ================= */ -document.addEventListener('DOMContentLoaded', () => { - if ('requestIdleCallback' in window) { - requestIdleCallback(initializeFeatures); - } else { - setTimeout(initializeFeatures, 300); - } -}); +function initializeFeatures() { + const pageId = "{{ $pageId }}"; + if (!pageId) return; + + initializeComments(); + initializeLikes(pageId); + initializeCommentCount(); +} + +document.addEventListener('DOMContentLoaded', initializeFeatures); </script> |
