Last active
November 18, 2015 21:39
-
-
Save robwalch/2e4aee26040eb9ec6081 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script extends jwplayer api instances with a `_history` | |
property which is a hash of event types including "all". | |
After including this on the page, to check if a "play" event fired, | |
call `jwplayer()._history.play`. Initially this returns `undefined`. | |
Once the event is fired an array is populated with an object for each | |
event fired. | |
The `jwplayer()._history.all` array will always be present and contains | |
all events fired by the player since this script attached it's "all" listener. | |
Added `_getEvents(type)` and `_getLastEvent(type)`. | |
`_getEvents` always returns an Array so you can check length. | |
`_getLastEvent` always returns an Object so you can check event properties. | |
This script will add player on embed and jQuery ready. You can require it | |
or run the global `jwplayerEventHistory()` right after any player setup | |
to add history to it. | |
*/ | |
(function() { | |
// this event callback adds history to jwplayer() instances | |
var storeEventHistory = function(type, event) { | |
event.__time = Date.now(); | |
this._history[type] = this._history[type] || []; | |
this._history[type].push(event); | |
this._history.all.push(event); | |
}; | |
// this registers the above callback to all current jwplayer() instances | |
var jwplayerEventHistory = function(jwplayer) { | |
var players = []; | |
jwplayer = jwplayer || window.jwplayer; | |
if (!jwplayer) { | |
return players; | |
} | |
for (var i=0; i<100; i++) { | |
var jwInstance = jwplayer(i); | |
if (typeof jwInstance.on !== 'function') { | |
return players; | |
} | |
jwInstance._history = jwInstance._history || { | |
all: [] | |
}; | |
jwInstance._getEvents = function(type) { | |
return this._history[type] || []; | |
}; | |
jwInstance._getLastEvent = function(type) { | |
var history = this._getEvents(type); | |
if (history.length) { | |
return history[history.length-1]; | |
} | |
return {}; | |
}; | |
jwInstance.off('all'); //, storeEventHistory); // off('all', fn) does not work | |
jwInstance.on('all', storeEventHistory); | |
players.push(jwInstance); | |
} | |
return players; | |
}; | |
// run above function to add players on page ready (jQuery), or require (AMD/CommonJS) | |
if (typeof $ === 'function') { | |
$(jwplayerEventHistory); | |
} | |
if (typeof define === 'function' && define.amd) { | |
define(['jwplayerEventHistory'], jwplayerEventHistory); | |
} else if (typeof module === 'object' && module.exports) { | |
module.exports = jwplayerEventHistory; | |
} | |
window.jwplayerEventHistory = jwplayerEventHistory; | |
return jwplayerEventHistory(window.jwplayer); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment