var video = document.getElementById('video'); video.textTracks.addEventListener('change', function () { console.log("TextTracks change event fired!"); });
// in an outer scope // textTracks is our TextTrackList object var active = getActive(); // start polling poll(); function poll() { // schedule next check in a frame requestAnimationFrame(poll); var current = getActive(); if (current !== active) { active = current; // update the active one // dispatchEvent is not supported on TextTrackList in IE... onchange({ target: textTracks }); } } function getActive() { for (var i = 0; i if (textTracks[i].mode === 'showing') { return textTracks[i]; } } }
(function() { /* Tries to implement an 'change' Event on TextTrackList Objects when not implemented */ if (window.TextTrackList && !('onchange' in window.TextTrackList.prototype)) { var textTracksLists = [], // we will store all the TextTrackLists instances polling = false; // and run only one polling loop var proto = TextTrackList.prototype, original_addEvent = proto.addEventListener, original_removeEvent = proto.removeEventListener; var Onchange= { get: getonchange, set: setonchange }; Object.defineProperty(proto, 'onchange', onchange); Object.defineProperty(proto, 'addEventListener', fnGetter(addListener)); Object.defineProperty(proto, 'removeEventListener', fnGetter(removeListener)); function fnGetter(fn) { return { get: function() { return fn; } }; } /* When we add a new EventListener, we attach a new object on our instance This object set as '._fakeevent' will hold informations about the current EventListeners the current onchange handler the parent