Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
wants to merge 2 commits into from

2 participants

@ScottDowne
Owner

No description provided.

wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
((28 lines not shown))
- 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";
@jbuck Owner
jbuck added a note

Instead of having it be http: or https:, can you just force it to https: ?

@ScottDowne Owner

Why?

I think there was a time where it needed to work in file:// too...

@jbuck Owner
jbuck added a note

If you set it to https: then we don't need to worry about loading it from file:// ever.

It also means I can just close https://bugzilla.mozilla.org/show_bug.cgi?id=965045

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
((29 lines not shown))
- 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 ];
@jbuck Owner
jbuck added a note

You could probably simplify what you're doing here with document.head.appendChild( tag )

@ScottDowne Owner

The docs have it as injecting it in the start. I hope there is no reason for that, but I would rather not muck with it if it is not broken.

@ScottDowne Owner

I just don't want to anger what I do not understand :P

@jbuck Owner
jbuck added a note

There is no reason for it, but it's loooonger than just saying document.head. A nice-to-have?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
((6 lines not shown))
ytCallbacks = [];
+ function onYouTubeIframeAPIReady() {
+ if ( YT.loaded ) {
+ ytReady = true;
+ while( ytCallbacks.length ) {
+ ytCallbacks[ 0 ]();
@jbuck Owner
jbuck added a note

You could pop this off the queue with .shift(), then execute it there.

This might be slightly better, as if the callback fails to execute for some reason, it won't leave the callback in the queue. Not sure if that matters though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jbuck jbuck commented on the diff
wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
((6 lines not shown))
ytCallbacks = [];
+ function onYouTubeIframeAPIReady() {
+ var callback;
+ if ( YT.loaded ) {
+ ytReady = true;
+ while( ytCallbacks.length ) {
+ callback = ytCallbacks.shift();
+ callback();
+ }
+ } else {
+ setTimeout( onYouTubeIframeAPIReady, 1000 );
@jbuck Owner
jbuck added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 13, 2014
  1. @ScottDowne
  2. @ScottDowne

    r

    ScottDowne authored
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 27 deletions.
  1. +28 −27 wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
View
55 wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
@@ -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 );
@jbuck Owner
jbuck added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ }
+
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
Something went wrong with that request. Please try again.