Skip to content

Instantly share code, notes, and snippets.

@Punk-UnDeaD
Created October 1, 2019 07:39
Show Gist options
  • Save Punk-UnDeaD/cde42d2f8c24790c21fdc4bed1bc0457 to your computer and use it in GitHub Desktop.
Save Punk-UnDeaD/cde42d2f8c24790c21fdc4bed1bc0457 to your computer and use it in GitHub Desktop.
((document, window) => {
window.App = window.App || {};
let tag = null;
let YT = null;
const html = document.querySelector('html');
App.YT = function (callback) {
if (!tag) {
tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
document.querySelector('head').append(tag);
window.YTConfig = {};
window.YT = {};
window.onYouTubeIframeAPIReady = () => {
YT = window.YT;
html.dispatchEvent(new CustomEvent('YouTubeIframeAPIReady'));
delete window.YT;
delete window.YTConfig;
delete window.onYouTubeIframeAPIReady;
}
}
if (YT) {
callback(YT);
}
else {
html.addEventListener('YouTubeIframeAPIReady', e => callback(YT));
}
};
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment