Skip to content
This repository has been archived by the owner on Jun 29, 2018. It is now read-only.

[#963724] Ensure other youtube scripts don't clobber our ready events. #373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,44 @@

// Setup for YouTube API
ytReady = false,
ytLoaded = false,
ytLoading = false,
ytCallbacks = [];

function onYouTubeIframeAPIReady() {
var callback;
if ( YT.loaded ) {
ytReady = true;
while( ytCallbacks.length ) {
callback = ytCallbacks.shift();
callback();
}
} else {
setTimeout( onYouTubeIframeAPIReady, 1000 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script file loads in about 68ms from the office, so I think it might make sense to lower this? maybe to 250ms?

}
}

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 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 {
script = document.createElement( "script" );
script.addEventListener( "load", onYouTubeIframeAPIReady, false);
script.src = "https://www.youtube.com/iframe_api";
document.head.appendChild( script );
}
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
Expand Down