Skip to content

Instantly share code, notes, and snippets.

@robwalch
Last active March 29, 2021 19:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwalch/5bc57dedb23ee6eaa1f0537203b679f7 to your computer and use it in GitHub Desktop.
Save robwalch/5bc57dedb23ee6eaa1f0537203b679f7 to your computer and use it in GitHub Desktop.
Observe attribute changes, method calls and events fired on a media element
const mediaElement = document.createElement('video');
function toString(video) {
return ((video.parentNode ? ('<' + video.parentNode.nodeName + '>') : '') + '<' + video.nodeName + '>')
.toLowerCase();
}
function videoEventHandler(e) {
console.warn(toString(e.target) + ' >> "' + e.type + '"');
}
function observe(video) {
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
if (MutationObserver) {
const observer = new MutationObserver((mutations) => {
mutations.forEach(function(mutation) {
console.warn(toString(video) + ' 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(video) + '.load()', video.src);
return load.call(this);
};
video.pause = function() {
console.error(toString(video) + '.pause()');
return pause.call(this);
};
video.play = function() {
console.error(toString(video) + '.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);
});
}
observe(mediaElement);
mediaElement.load();
mediaElement.src = '';
mediaElement.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment