mirror of
https://github.com/lbr77/blog-astro.git
synced 2026-04-09 00:19:12 +00:00
test
This commit is contained in:
@@ -2,41 +2,56 @@
|
|||||||
var TWIKOO_SRC =
|
var TWIKOO_SRC =
|
||||||
'https://cdn.jsdelivr.net/npm/twikoo@1.6.44/dist/twikoo.min.js'
|
'https://cdn.jsdelivr.net/npm/twikoo@1.6.44/dist/twikoo.min.js'
|
||||||
var TARGET_SELECTOR = '#tcomment'
|
var TARGET_SELECTOR = '#tcomment'
|
||||||
var state = { loading: false }
|
var SCRIPT_ID = 'twikoo-cdn-script'
|
||||||
|
|
||||||
function renderTwikoo() {
|
function renderTwikoo() {
|
||||||
var container = document.querySelector(TARGET_SELECTOR)
|
var container = document.querySelector(TARGET_SELECTOR)
|
||||||
if (!container || !window.twikoo) return
|
if (!container || !window.twikoo) return
|
||||||
|
|
||||||
|
container.innerHTML = ''
|
||||||
window.twikoo.init({
|
window.twikoo.init({
|
||||||
envId: 'https://twikoo.hk.nvme0n1p.dev/',
|
envId: 'https://twikoo.hk.nvme0n1p.dev/',
|
||||||
el: TARGET_SELECTOR,
|
el: TARGET_SELECTOR,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function ensureScript() {
|
||||||
if (window.twikoo) {
|
var existing = document.getElementById(SCRIPT_ID)
|
||||||
renderTwikoo()
|
|
||||||
return
|
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')
|
var script = document.createElement('script')
|
||||||
|
script.id = SCRIPT_ID
|
||||||
script.src = TWIKOO_SRC
|
script.src = TWIKOO_SRC
|
||||||
script.async = true
|
script.async = true
|
||||||
script.onload = function () {
|
script.onload = function () {
|
||||||
state.loading = false
|
script.dataset.loaded = 'true'
|
||||||
renderTwikoo()
|
renderTwikoo()
|
||||||
}
|
}
|
||||||
script.onerror = function (err) {
|
script.onerror = function (err) {
|
||||||
state.loading = false
|
script.dataset.failed = 'true'
|
||||||
console.error('Failed to load Twikoo:', err)
|
console.error('Failed to load Twikoo:', err)
|
||||||
}
|
}
|
||||||
document.head.appendChild(script)
|
document.head.appendChild(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
ensureScript()
|
||||||
|
|
||||||
|
if (window.twikoo) {
|
||||||
|
renderTwikoo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function onLoad() {
|
function onLoad() {
|
||||||
init()
|
init()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user