diff options
author | Sam Potts <sam@potts.es> | 2019-06-01 19:29:21 +1000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2019-06-01 19:29:21 +1000 |
commit | 5d699d5f47e5bce9e6c59ff5a1f3fab236d7e00f (patch) | |
tree | c24f5087b482a0e86aa456a4313e8ac099d5ec24 /src | |
parent | 36ad132c8278115074a93691fad97ae65eb4bd18 (diff) | |
parent | 34d79a54432804dfd0bcfb9f70155eb48e03eef9 (diff) | |
download | plyr-5d699d5f47e5bce9e6c59ff5a1f3fab236d7e00f.tar.lz plyr-5d699d5f47e5bce9e6c59ff5a1f3fab236d7e00f.tar.xz plyr-5d699d5f47e5bce9e6c59ff5a1f3fab236d7e00f.zip |
Merge branch 'develop' of github.com:sampotts/plyr into develop
Diffstat (limited to 'src')
-rw-r--r-- | src/js/plugins/youtube.js | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 73448b67..bf933646 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -47,6 +47,8 @@ function getHost(config) { return undefined; } +let onYouTubeReadyCallbacks; + const youtube = { setup() { // Add embed class for responsive @@ -56,26 +58,32 @@ const youtube = { if (is.object(window.YT) && is.function(window.YT.Player)) { youtube.ready.call(this); } else { - // Load the API - loadScript(this.config.urls.youtube.sdk).catch(error => { - this.debug.warn('YouTube API failed to load', error); - }); - // Setup callback for the API - // YouTube has it's own system of course... - window.onYouTubeReadyCallbacks = window.onYouTubeReadyCallbacks || []; + if (!onYouTubeReadyCallbacks) { + const oldYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady; + // Load the API + onYouTubeReadyCallbacks = []; + + // Set callback to process queue + window.onYouTubeIframeAPIReady = () => { + if (oldYouTubeIframeAPIReady && is.function(oldYouTubeIframeAPIReady)) { + oldYouTubeIframeAPIReady(); + } + + window.onYouTubeReadyCallbacks.forEach(callback => { + callback(); + }); + }; + + loadScript(this.config.urls.youtube.sdk).catch(error => { + this.debug.warn('YouTube API failed to load', error); + }); + } // Add to queue - window.onYouTubeReadyCallbacks.push(() => { + onYouTubeReadyCallbacks.push(() => { youtube.ready.call(this); }); - - // Set callback to process queue - window.onYouTubeIframeAPIReady = () => { - window.onYouTubeReadyCallbacks.forEach(callback => { - callback(); - }); - }; } }, @@ -108,7 +116,7 @@ const youtube = { ready() { const player = this; // Ignore already setup (race condition) - const currentId = player.media.getAttribute('id'); + const currentId = player.media && player.media.getAttribute('id'); if (!is.empty(currentId) && currentId.startsWith('youtube-')) { return; } |