From f100caba81d0f2ecb9305988993072ccb1cdc233 Mon Sep 17 00:00:00 2001 From: Alexander Farkas Date: Fri, 24 May 2019 16:53:58 +0200 Subject: [PATCH 1/2] fix youtube embed + handle early destroy --- src/js/plugins/youtube.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 7abc05fe..0b812505 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(() => { youtube.ready.call(this); }); - - // Set callback to process queue - window.onYouTubeIframeAPIReady = () => { - window.onYouTubeReadyCallbacks.forEach(callback => { - callback(); - }); - }; } }, @@ -109,7 +117,7 @@ const youtube = { 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; } From 97a6e72e104b6a47abefaa7d6216f0a6c45f0ce9 Mon Sep 17 00:00:00 2001 From: Alexander Farkas Date: Fri, 24 May 2019 16:55:45 +0200 Subject: [PATCH 2/2] fix youtube embed + handle early destroy --- src/js/plugins/youtube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 0b812505..d82dab22 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -81,7 +81,7 @@ const youtube = { } // Add to queue - window.onYouTubeReadyCallbacks.push(() => { + onYouTubeReadyCallbacks.push(() => { youtube.ready.call(this); }); }