Skip to content

Instantly share code, notes, and snippets.

@mpcabd
Last active December 21, 2017 14:17
Show Gist options
  • Save mpcabd/dd4be3563e62f54ad0ae72b2acd6e3b3 to your computer and use it in GitHub Desktop.
Save mpcabd/dd4be3563e62f54ad0ae72b2acd6e3b3 to your computer and use it in GitHub Desktop.
Speed up all media on page
function getAllMedia() {
var getDocuments = function () {
var f = function (document) {
var iframes = Array.from(document.getElementsByTagName('iframe'));
var result = [ document ];
iframes.forEach(function (iframe) {
var iframeDocument;
try {
iframeDocument = iframe.contentDocument;
} catch (error) {
return;
}
f(iframeDocument).forEach(function(d) { result.push(d); })
});
return result;
};
return f(window.document);
}
var media = [];
getDocuments().forEach(function (d) {
Array.from(d.getElementsByTagName('audio')).forEach(function (a) { media.push(a); });
Array.from(d.getElementsByTagName('video')).forEach(function (v) { media.push(v); });
});
return media;
}
function speedAllUp(rate) {
if (rate === undefined) {
rate = 2.25;
}
if (window._speedAllUpInterval !== undefined) {
clearInterval(window._speedAllUpInterval);
}
window._speedAllUpInterval = setInterval(function() {
getAllMedia().forEach(function (m) { if (!m.paused && m.readyState >= 2) { m.playbackRate = rate; } });
}, 100)
}
function normalPlaybackRate() {
if (window._speedAllUpInterval !== undefined) {
clearInterval(window._speedAllUpInterval);
window._speedAllUpInterval = undefined;
}
getAllMedia().forEach(function (m) { m.playbackRate = 1; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment