Skip to content

Instantly share code, notes, and snippets.

@JoelSutherland
Created April 2, 2020 21:05
Show Gist options
  • Save JoelSutherland/522509ae9e4629b7c7c1a40667c31fc0 to your computer and use it in GitHub Desktop.
Save JoelSutherland/522509ae9e4629b7c7c1a40667c31fc0 to your computer and use it in GitHub Desktop.
(function () {
function loadZendeskChat(callback) {
var zdscript = document.createElement('script');
zdscript.setAttribute('id','ze-snippet');
zdscript.src = 'https://static.zdassets.com/ekr/snippet.js?key=XXX-XXX-XXX-XXX';
(document.getElementsByTagName('body')[0]).appendChild(zdscript);
window.zdonload = setInterval(function(){
if(typeof zE !== "undefined" && typeof zE.activate !== "undefined") {
clearInterval(window.zdonload);
callback();
}
},50,null)
};
window.loadAndOpenZendeskChat = function() {
var button = document.getElementById('zdbutton');
localStorage.setItem('ff_zd_hasOpened',true);
button.innerHTML = 'Loading...';
loadZendeskChat(function(){
window.setTimeout(function(){
zE.activate();
button.parentNode.removeChild(button);
},1000);
});
}
if(localStorage.getItem('ff_zd_hasOpened')) {
loadZendeskChat(function(){});
} else {
document.getElementsByTagName('body')[0].insertAdjacentHTML('beforeend','<button id="zdbutton" aria-label="Launch Help Chat Window" onClick="window.loadAndOpenZendeskChat();">Help</button>');
}
}());
@JJCLane
Copy link

JJCLane commented Jan 29, 2021

Just a little update, rather than introducing a race condition with the setTimeout, you can wait for zE to be ready:

zE(() => {
    zE.activate();
    button.parentNode.removeChild(button);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment