Skip to content
Browse files

Update to the latest version of Froogaloop

  • Loading branch information...
1 parent cc41e49 commit bc5cdef6213867b08ad60e8d71e556a455422647 @bdougherty bdougherty committed
Showing with 45 additions and 34 deletions.
  1. +40 −29 javascript/froogaloop.js
  2. +5 −5 javascript/froogaloop.min.js
View
69 javascript/froogaloop.js
@@ -1,7 +1,7 @@
-// Init style shamelessly stolen from jQuery http://jquery.com
alert('Remove this line before using in production!');
-var Froogaloop = (function(){
+// Init style shamelessly stolen from jQuery http://jquery.com
+var Froogaloop = (function(){
// Define a local copy of Froogaloop
function Froogaloop(iframe) {
// The Froogaloop object is actually just the init constructor
@@ -10,11 +10,11 @@ var Froogaloop = (function(){
var eventCallbacks = {},
hasWindowEvent = false,
- slice = Array.prototype.slice;
+ isReady = false,
+ slice = Array.prototype.slice,
+ playerDomain = '';
Froogaloop.fn = Froogaloop.prototype = {
- playerDomain: '',
-
element: null,
init: function(iframe) {
@@ -23,6 +23,10 @@ var Froogaloop = (function(){
}
this.element = iframe;
+
+ // Register message event listeners
+ playerDomain = getDomainFromUrl(this.element.getAttribute('src'));
+
return this;
},
@@ -40,7 +44,7 @@ var Froogaloop = (function(){
var self = this,
element = self.element,
- target_id = element.id != '' ? element.id : null,
+ target_id = element.id !== '' ? element.id : null,
params = !isFunction(valueOrCallback) ? valueOrCallback : null,
callback = isFunction(valueOrCallback) ? valueOrCallback : null;
@@ -66,7 +70,7 @@ var Froogaloop = (function(){
var self = this,
element = self.element,
- target_id = element.id != '' ? element.id : null;
+ target_id = element.id !== '' ? element.id : null;
storeCallback(eventName, callback, target_id);
@@ -75,25 +79,10 @@ var Froogaloop = (function(){
if (eventName != 'ready') {
postMessage('addEventListener', eventName, element);
}
-
- // Register message event listeners
- if (hasWindowEvent) {
- return self;
+ else if (eventName == 'ready' && isReady) {
+ callback.call(null, target_id);
}
- playerDomain = getDomainFromUrl(element.getAttribute('src'));
-
- // Listens for the message event.
- // W3C
- if (window.addEventListener) {
- window.addEventListener('message', onMessageReceived, false);
- }
- // IE
- else {
- window.attachEvent('onmessage', onMessageReceived, false);
- }
-
- hasWindowEvent = true;
return self;
},
@@ -109,7 +98,7 @@ var Froogaloop = (function(){
var self = this,
element = self.element,
- target_id = element.id != '' ? element.id : null,
+ target_id = element.id !== '' ? element.id : null,
removed = removeCallback(eventName, target_id);
// The ready event is not registered
@@ -148,16 +137,28 @@ var Froogaloop = (function(){
* via window.postMessage.
*/
function onMessageReceived(event) {
+ var data, method;
+
+ try {
+ data = JSON.parse(event.data);
+ method = data.event || data.method;
+ }
+ catch(e) {
+ //fail silently... like a ninja!
+ }
+
+ if (method == 'ready' && !isReady) {
+ isReady = true;
+ }
+
// Handles messages from moogaloop only
if (event.origin != playerDomain) {
return false;
}
- var data = JSON.parse(event.data),
- value = data.value,
- method = data.event || data.method,
+ var value = data.value,
eventData = data.data,
- target_id = target_id == '' ? null : data.player_id,
+ target_id = target_id === '' ? null : data.player_id,
callback = getCallback(method, target_id),
params = [];
@@ -265,6 +266,16 @@ var Froogaloop = (function(){
// Give the init function the Froogaloop prototype for later instantiation
Froogaloop.fn.init.prototype = Froogaloop.fn;
+ // Listens for the message event.
+ // W3C
+ if (window.addEventListener) {
+ window.addEventListener('message', onMessageReceived, false);
+ }
+ // IE
+ else {
+ window.attachEvent('onmessage', onMessageReceived, false);
+ }
+
// Expose froogaloop to the global object
return (window.Froogaloop = window.$f = Froogaloop);
View
10 javascript/froogaloop.min.js
@@ -1,5 +1,5 @@
-alert('Remove this line before using in production!');
-var Froogaloop=function(){function g(a){return new g.fn.init(a)}function h(a,b,c){if(!c.contentWindow.postMessage)return!1;var f=c.getAttribute("src").split("?")[0];a=JSON.stringify({method:a,value:b});c.contentWindow.postMessage(a,f)}function i(a){if(a.origin!=playerDomain)return!1;var b=JSON.parse(a.data);a=b.value;var c=b.data,f=f==""?null:b.player_id;b=f?d[f][b.event||b.method]:d[b.event||b.method];var e=[];if(!b)return!1;a!==void 0&&e.push(a);c&&e.push(c);f&&e.push(f);return e.length>0?b.apply(null,
-e):b.call()}function j(a,b,c){c?(d[c]||(d[c]={}),d[c][a]=b):d[a]=b}var d={},k=!1;g.fn=g.prototype={playerDomain:"",element:null,init:function(a){typeof a==="string"&&(a=document.getElementById(a));this.element=a;return this},api:function(a,b){if(!this.element||!a)return!1;var c=this.element,f=c.id!=""?c.id:null,e=!b||!b.constructor||!b.call||!b.apply?b:null,d=b&&b.constructor&&b.call&&b.apply?b:null;d&&j(a,d,f);h(a,e,c);return this},addEvent:function(a,b){if(!this.element)return!1;var c=this.element;
-j(a,b,c.id!=""?c.id:null);a!="ready"&&h("addEventListener",a,c);if(k)return this;c=c.getAttribute("src").split("/");for(var d="",e=0,g=c.length;e<g;e++){if(e<3)d+=c[e];else break;e<2&&(d+="/")}playerDomain=d;window.addEventListener?window.addEventListener("message",i,!1):window.attachEvent("onmessage",i,!1);k=!0;return this},removeEvent:function(a){if(!this.element)return!1;var b=this.element,c;a:{if((c=b.id!=""?b.id:null)&&d[c]){if(!d[c][a]){c=!1;break a}d[c][a]=null}else{if(!d[a]){c=!1;break a}d[a]=
-null}c=!0}a!="ready"&&c&&h("removeEventListener",a,b)}};g.fn.init.prototype=g.fn;return window.Froogaloop=window.$f=g}();
+alert("Remove this line before using in production!");
+var Froogaloop=function(){function e(c){return new e.fn.init(c)}function g(c,b,a){if(!a.contentWindow.postMessage)return!1;var d=a.getAttribute("src").split("?")[0],c=JSON.stringify({method:c,value:b});a.contentWindow.postMessage(c,d)}function i(c){var b,a;try{b=JSON.parse(c.data),a=b.event||b.method}catch(l){}"ready"==a&&!h&&(h=!0);if(c.origin!=j)return!1;var c=b.value,e=b.data,f=""===f?null:b.player_id;b=f?d[f][a]:d[a];a=[];if(!b)return!1;void 0!==c&&a.push(c);e&&a.push(e);f&&a.push(f);return 0<
+a.length?b.apply(null,a):b.call()}function k(c,b,a){a?(d[a]||(d[a]={}),d[a][c]=b):d[c]=b}var d={},h=!1,j="";e.fn=e.prototype={element:null,init:function(c){"string"===typeof c&&(c=document.getElementById(c));this.element=c;for(var c=this.element.getAttribute("src").split("/"),b="",a=0,d=c.length;a<d;a++){if(3>a)b+=c[a];else break;2>a&&(b+="/")}j=b;return this},api:function(c,b){if(!this.element||!c)return!1;var a=this.element,d=""!==a.id?a.id:null,e=!b||!b.constructor||!b.call||!b.apply?b:null,f=
+b&&b.constructor&&b.call&&b.apply?b:null;f&&k(c,f,d);g(c,e,a);return this},addEvent:function(c,b){if(!this.element)return!1;var a=this.element,d=""!==a.id?a.id:null;k(c,b,d);"ready"!=c?g("addEventListener",c,a):"ready"==c&&h&&b.call(null,d);return this},removeEvent:function(c){if(!this.element)return!1;var b=this.element,a;a:{if((a=""!==b.id?b.id:null)&&d[a]){if(!d[a][c]){a=!1;break a}d[a][c]=null}else{if(!d[c]){a=!1;break a}d[c]=null}a=!0}"ready"!=c&&a&&g("removeEventListener",c,b)}};e.fn.init.prototype=
+e.fn;window.addEventListener?window.addEventListener("message",i,!1):window.attachEvent("onmessage",i,!1);return window.Froogaloop=window.$f=e}();

0 comments on commit bc5cdef

Please sign in to comment.
Something went wrong with that request. Please try again.