From 2bf18d2ce258a7e84536ac0f88bb32517279b22d Mon Sep 17 00:00:00 2001 From: scottdowne Date: Mon, 27 Jan 2014 17:32:23 -0500 Subject: [PATCH 1/2] [#963724] Implement our own YouTube ready event by checking and waiting for YT.loaded. --- .../popcorn.HTMLYouTubeVideoElement.js | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js b/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js index 5e3fc8133..153881c87 100644 --- a/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js +++ b/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js @@ -12,43 +12,48 @@ // Setup for YouTube API ytReady = false, - ytLoaded = false, + ytLoading = false, ytCallbacks = []; + function onYouTubeIframeAPIReady() { + if ( YT.loaded ) { + ytReady = true; + while( ytCallbacks.length ) { + ytCallbacks[ 0 ](); + ytCallbacks.shift(); + } + } else { + setTimeout( onYouTubeIframeAPIReady, 1000 ); + } + } + function isYouTubeReady() { - // If the YouTube iframe API isn't injected, to it now. - if( !ytLoaded ) { - var tag = document.createElement( "script" ); - var protocol = window.location.protocol === "file:" ? "http:" : ""; - - tag.src = protocol + "//www.youtube.com/iframe_api"; - var firstScriptTag = document.getElementsByTagName( "script" )[ 0 ]; - firstScriptTag.parentNode.insertBefore( tag, firstScriptTag ); - ytLoaded = true; + var tag, + protocol, + firstScriptTag; + // If we area already waiting, do nothing. + if( !ytLoading ) { + // If script is already there, check if it is loaded. + if ( window.YT ) { + onYouTubeIframeAPIReady(); + } else { + tag = document.createElement( "script" ); + protocol = window.location.protocol === "file:" ? "http:" : ""; + // Wait for the script to be loaded, then check if it's ready. + tag.addEventListener( "load", onYouTubeIframeAPIReady, false); + tag.src = protocol + "//www.youtube.com/iframe_api"; + firstScriptTag = document.getElementsByTagName( "script" )[ 0 ]; + firstScriptTag.parentNode.insertBefore( tag, firstScriptTag ); + } + ytLoading = true; } return ytReady; } function addYouTubeCallback( callback ) { - ytCallbacks.unshift( callback ); + ytCallbacks.push( callback ); } - // An existing YouTube references can break us. - // Remove it and use the one we can trust. - if ( window.YT ) { - window.quarantineYT = window.YT; - window.YT = null; - } - - window.onYouTubeIframeAPIReady = function() { - ytReady = true; - var i = ytCallbacks.length; - while( i-- ) { - ytCallbacks[ i ](); - delete ytCallbacks[ i ]; - } - }; - function HTMLYouTubeVideoElement( id ) { // YouTube iframe API requires postMessage From 838fc161b615cc71d3c2ed46c7641c5075667786 Mon Sep 17 00:00:00 2001 From: scottdowne Date: Thu, 13 Feb 2014 16:13:32 -0500 Subject: [PATCH 2/2] r --- .../popcorn.HTMLYouTubeVideoElement.js | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js b/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js index 153881c87..f0e090197 100644 --- a/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js +++ b/wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js @@ -16,11 +16,12 @@ ytCallbacks = []; function onYouTubeIframeAPIReady() { + var callback; if ( YT.loaded ) { ytReady = true; while( ytCallbacks.length ) { - ytCallbacks[ 0 ](); - ytCallbacks.shift(); + callback = ytCallbacks.shift(); + callback(); } } else { setTimeout( onYouTubeIframeAPIReady, 1000 ); @@ -28,22 +29,17 @@ } function isYouTubeReady() { - var tag, - protocol, - firstScriptTag; + var script; // If we area already waiting, do nothing. if( !ytLoading ) { // If script is already there, check if it is loaded. if ( window.YT ) { onYouTubeIframeAPIReady(); } else { - tag = document.createElement( "script" ); - protocol = window.location.protocol === "file:" ? "http:" : ""; - // Wait for the script to be loaded, then check if it's ready. - tag.addEventListener( "load", onYouTubeIframeAPIReady, false); - tag.src = protocol + "//www.youtube.com/iframe_api"; - firstScriptTag = document.getElementsByTagName( "script" )[ 0 ]; - firstScriptTag.parentNode.insertBefore( tag, firstScriptTag ); + script = document.createElement( "script" ); + script.addEventListener( "load", onYouTubeIframeAPIReady, false); + script.src = "https://www.youtube.com/iframe_api"; + document.head.appendChild( script ); } ytLoading = true; }