Created
April 2, 2020 21:05
-
-
Save JoelSutherland/522509ae9e4629b7c7c1a40667c31fc0 to your computer and use it in GitHub Desktop.
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
(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>'); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a little update, rather than introducing a race condition with the
setTimeout
, you can wait for zE to be ready: