Skip to content

Instantly share code, notes, and snippets.

@SoyOscarRH
Last active October 9, 2020 18:57
Show Gist options
  • Save SoyOscarRH/b3d5d4864b2fb1db2f3652f532f51b0a to your computer and use it in GitHub Desktop.
Save SoyOscarRH/b3d5d4864b2fb1db2f3652f532f51b0a to your computer and use it in GitHub Desktop.
JavaScript code to change the speed videos on the internet using "[" and "]".
const getAllVideos = () => Array.from(document.getElementsByTagName("video"));
const changeSpeedAllVideos = ({ to }) => {
to = Math.min(Math.max(to, 0.5), 6);
getAllVideos().forEach((video) => (video.playbackRate = to));
console.log("new playback speed is " + to.toFixed(2));
};
const changeSpeedSlightly = ({ key }) => {
const videos = getAllVideos();
const currentSpeed = videos[0].playbackRate;
if (key === "[") changeSpeedAllVideos({ to: currentSpeed - 0.15 });
if (key === "]") changeSpeedAllVideos({ to: currentSpeed + 0.15 });
};
getAllVideos().forEach((video) => (video.playbackRate = 1));
document.body.addEventListener("keypress", changeSpeedSlightly);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment