This commit is contained in:
2025-12-09 22:02:34 +08:00
parent 8fd47efc96
commit 9c7ae33ea5

View File

@@ -2,41 +2,56 @@
var TWIKOO_SRC =
'https://cdn.jsdelivr.net/npm/twikoo@1.6.44/dist/twikoo.min.js'
var TARGET_SELECTOR = '#tcomment'
var state = { loading: false }
var SCRIPT_ID = 'twikoo-cdn-script'
function renderTwikoo() {
var container = document.querySelector(TARGET_SELECTOR)
if (!container || !window.twikoo) return
container.innerHTML = ''
window.twikoo.init({
envId: 'https://twikoo.hk.nvme0n1p.dev/',
el: TARGET_SELECTOR,
})
}
function init() {
if (window.twikoo) {
renderTwikoo()
return
function ensureScript() {
var existing = document.getElementById(SCRIPT_ID)
if (existing) {
if (existing.dataset.failed === 'true') {
existing.remove()
} else {
if (!window.twikoo) {
existing.addEventListener('load', renderTwikoo, { once: true })
}
return
}
}
if (state.loading) return
state.loading = true
var script = document.createElement('script')
script.id = SCRIPT_ID
script.src = TWIKOO_SRC
script.async = true
script.onload = function () {
state.loading = false
script.dataset.loaded = 'true'
renderTwikoo()
}
script.onerror = function (err) {
state.loading = false
script.dataset.failed = 'true'
console.error('Failed to load Twikoo:', err)
}
document.head.appendChild(script)
}
function init() {
ensureScript()
if (window.twikoo) {
renderTwikoo()
}
}
function onLoad() {
init()
}