mirror of
https://github.com/lbr77/blog-astro.git
synced 2026-04-08 16:11:56 +00:00
comment
This commit is contained in:
52
public/js/twikoo-loader.js
Normal file
52
public/js/twikoo-loader.js
Normal file
@@ -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)
|
||||
})()
|
||||
Reference in New Issue
Block a user