diff --git a/public/js/twikoo-loader.js b/public/js/twikoo-loader.js new file mode 100644 index 0000000..a2a2815 --- /dev/null +++ b/public/js/twikoo-loader.js @@ -0,0 +1,52 @@ +(function () { + var TWIKOO_SRC = + 'https://cdn.jsdelivr.net/npm/twikoo@1.6.44/dist/twikoo.min.js' + var TARGET_SELECTOR = '#tcomment' + var state = { loading: false, loaded: false } + + function init() { + var container = document.querySelector(TARGET_SELECTOR) + if (!container) return + + function runInit() { + if (state.loaded) return + state.loaded = true + if (window.twikoo) { + window.twikoo.init({ + envId: 'https://twikoo.hk.nvme0n1p.dev/', + el: TARGET_SELECTOR, + }) + } + } + + if (window.twikoo) { + runInit() + return + } + + if (state.loading) return + state.loading = true + + var script = document.createElement('script') + script.src = TWIKOO_SRC + script.async = true + script.onload = runInit + script.onerror = function (err) { + console.error('Failed to load Twikoo:', err) + } + document.head.appendChild(script) + } + + function onLoad() { + init() + } + + if (document.readyState === 'complete') { + onLoad() + } else { + window.addEventListener('load', onLoad, { once: true }) + } + + document.addEventListener('astro:page-load', onLoad) + document.addEventListener('astro:after-swap', onLoad) +})() diff --git a/src/components/Comment.astro b/src/components/Comment.astro index ab367bd..6ba3d19 100644 --- a/src/components/Comment.astro +++ b/src/components/Comment.astro @@ -1,52 +1,6 @@ + +

comment

+

留言 / 评论

- + diff --git a/src/pages/about.astro b/src/pages/about.astro index 5e1d452..af7e5fa 100644 --- a/src/pages/about.astro +++ b/src/pages/about.astro @@ -155,8 +155,6 @@ import Comment from '@/components/Comment.astro'
-

comment

-

留言 / 评论