Skip to content

Instantly share code, notes, and snippets.

@robwalch
Last active November 13, 2019 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwalch/42d260ae8f12d909363b6ea48933bc9b to your computer and use it in GitHub Desktop.
Save robwalch/42d260ae8f12d909363b6ea48933bc9b to your computer and use it in GitHub Desktop.
Observe attribute changes, method calls and events fired on a media element
(function (video) {
function toString() {
return ((video.parentNode ? ('<' + video.parentNode.nodeName + '>') : '') + '<' + video.nodeName + '>')
.toLowerCase();
}
function videoEventHandler(e) {
console.warn(toString() + ' >> "' + e.type + '"');
}
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
if (MutationObserver) {
const observer = new MutationObserver((mutations) => {
mutations.forEach(function(mutation) {
console.warn(toString() + ' mutation', mutation);
});
});
observer.observe(video, {
attributes: true
});
}
const load = video.load;
const pause = video.pause;
const play = video.play;
video.load = function() {
console.error(toString() + '.load()', video.src);
return load.call(this);
};
video.pause = function() {
console.error(toString() + '.pause()');
return pause.call(this);
};
video.play = function() {
console.error(toString() + '.play()');
return play.call(this);
};
[
'loadstart',
'progress',
'suspend',
'abort',
'error',
'emptied',
'stalled',
'loadedmetadata',
'loadeddata',
'canplay',
'canplaythrough',
'playing',
'waiting',
'seeking',
'seeked',
'ended',
'durationchange',
'timeupdate',
'play',
'pause',
'ratechange',
'resize',
'volumechange'
].forEach((eventName) => {
video.addEventListener(eventName, videoEventHandler);
});
}(mediaElement));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment