40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
export default defineNuxtPlugin(() => {
|
|
const config = useRuntimeConfig()
|
|
const baseUrl = String(config.public.chatwootBaseUrl || '').trim()
|
|
const websiteToken = String(config.public.chatwootWebsiteToken || '').trim()
|
|
|
|
if (!baseUrl || !websiteToken) {
|
|
return
|
|
}
|
|
|
|
const loadChatwoot = () => {
|
|
if (document.getElementById('chatwoot-sdk')) return
|
|
|
|
const script = document.createElement('script')
|
|
script.id = 'chatwoot-sdk'
|
|
script.src = `${baseUrl}/packs/js/sdk.js`
|
|
script.async = true
|
|
script.defer = true
|
|
script.onload = () => {
|
|
window.chatwootSDK?.run({
|
|
websiteToken,
|
|
baseUrl
|
|
})
|
|
}
|
|
|
|
if (document.body) {
|
|
document.body.appendChild(script)
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
document.body?.appendChild(script)
|
|
}, { once: true })
|
|
}
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', loadChatwoot, { once: true })
|
|
} else {
|
|
loadChatwoot()
|
|
}
|
|
})
|