Last active
February 6, 2023 00:59
-
-
Save mortenoc/acc2c498488d077caca8230172757cdc to your computer and use it in GitHub Desktop.
Zendesk Vue3 port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createApp } from "vue" | |
import Zendesk from "./Zendesk.js"; | |
const app = createApp(MyApp); | |
app.use(Zendesk, { | |
key: "{key}", | |
disabled: false, | |
hideOnLoad: false, | |
settings: { | |
webWidget: { | |
color: { | |
theme: "#f0f", | |
launcherText: '#fff' | |
}, | |
position: { | |
horizontal: 'left', | |
vertical: 'bottom' | |
} | |
} | |
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import emitter from 'tiny-emitter/instance' | |
export default { | |
install: (app, options = {}) => { | |
if (!options.disabled && (!options.key || options.key.length === 0)) { | |
console.warn("Please enter a Zendesk Web Widget Key"); | |
} | |
const disabledLogger = function(method, ...args) { | |
console.log("Zendesk is disabled, you called:", { method, args }); | |
}; | |
if (options.disabled) { | |
window.zE = disabledLogger; | |
} | |
window.zESettings = options.settings; | |
const root = {}; | |
root.$on = (...args) => emitter.on(...args); | |
root.$once = (...args) => emitter.once(...args); | |
root.$off = (...args) => emitter.off(...args); | |
root.$emit = (...args) => emitter.emit(...args); | |
let isLoaded = false; | |
root.isLoaded = () => isLoaded; | |
root.load = zendeskKey => { | |
if (isLoaded) { | |
return; | |
} | |
const script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.async = true; | |
script.id = "ze-snippet"; | |
const actualZendeskKey = zendeskKey || options.key; | |
script.src = | |
"https://static.zdassets.com/ekr/snippet.js?key=" + actualZendeskKey; | |
if (options.nonce) { | |
script.setAttribute('nonce', options.nonce) | |
} | |
delete window.zE; | |
const first = document.getElementsByTagName("script")[0]; | |
first.parentNode.insertBefore(script, first); | |
script.onload = event => { | |
isLoaded = true; | |
if (options.hideOnLoad) { | |
root.hide(); | |
} | |
root.$emit("loaded", event); | |
window.zE("webWidget:on", "open", () => { | |
root.$emit("open"); | |
}); | |
window.zE("webWidget:on", "close", () => { | |
root.$emit("close"); | |
}); | |
}; | |
}; | |
if (!options.disabled) { | |
root.load(options.key); | |
} | |
root.hide = () => window.zE("webWidget", "hide"); | |
root.show = () => window.zE("webWidget", "show"); | |
root.logout = () => window.zE("webWidget", "logout"); | |
root.identify = user => window.zE("webWidget", "identify", user); | |
root.prefill = user => window.zE("webWidget", "prefill", user); | |
root.setLocale = locale => window.zE("webWidget", "setLocale", locale); | |
root.updateSettings = settings => window.zE("webWidget", "updateSettings", settings); | |
root.clear = () => window.zE("webWidget", "clear"); | |
root.updatePath = options => window.zE("updatePath", "clear", options); | |
root.toggle = () => window.zE("webWidget", "toggle"); | |
root.reset = () => window.zE("webWidget", "reset"); | |
root.close = () => window.zE("webWidget", "close"); | |
root.open = () => window.zE("webWidget", "open"); | |
Object.defineProperty(root, "zE", { | |
get: function get() { | |
return window.zE; | |
} | |
}); | |
app.config.globalProperties.$zendesk = root; | |
} | |
}; |
Hi @agracia-foticos ,
I tried to implement your fixes : https://stackblitz.com/edit/nuxt-starter-t3wwc5?file=lib%2Fzendesk.js
noticed that I had the same issue
My script src is https://static.zdassets.com/ekr/snippet.js?key=xxxxx
Buttt when i see the result from network tab, the javascript is loaded actually
Yet no widget being shown too.Did you change the config setting
hideOnLoad: true,
I will edit the original post to false instead.
Hello,
Yes i set to hideonLoad = false
The problem was on the script.src and the script.id
I intend to use the https://static.zdassets.com/ekr/snippet.js
and this is the way i solved it :
script.id = 'ze-snippet'
script.src = https://static.zdassets.com/ekr/snippet.js?key='XXXXXXXXXXXXXX'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you change the config setting
hideOnLoad: true,
I will edit the original post to false instead.