Skip to content

Instantly share code, notes, and snippets.

@aciccarello
Created January 5, 2021 05:42
Show Gist options
  • Save aciccarello/61e8da832fe4eae6697cd971f14fb384 to your computer and use it in GitHub Desktop.
Save aciccarello/61e8da832fe4eae6697cd971f14fb384 to your computer and use it in GitHub Desktop.
Set audio/video playback
function setPlaybackRate(rate) {
Array.from(document.querySelectorAll('video,audio')).forEach((v) => v.playbackRate = rate);
}
// Mutation observer for interactive UIs
// Select the node that will be observed for mutations
let targetNode = document.body;
// Options for the observer (which mutations to observe)
let config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
let callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for(const mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.addedNodes.length > 0) {
const nodes = Array.from(mutation.addedNodes);
console.log('New nodes', nodes.map((e) => e.tagName));
}
}
};
// Create an observer instance linked to the callback function
let observer = new MutationObserver((mutationList, observer) => mutationCallback(mutationList, observer));
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
function mutationCallback(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for(const mutation of mutationsList) {
if (['AUDIO', 'VIDEO'].includes(mutation.target.tagName)) {
console.log('Mutation', mutation, 'setting rate');
setPlaybackRate(1.25);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment