Skip to content

Instantly share code, notes, and snippets.

@aequabit
Created June 14, 2025 18:38
Show Gist options
  • Save aequabit/39c3150dcd31dcb6c92aff8024e8cfba to your computer and use it in GitHub Desktop.
Save aequabit/39c3150dcd31dcb6c92aff8024e8cfba to your computer and use it in GitHub Desktop.
YouTube - Player Undo Button
// ==UserScript==
// @name YouTube - Player Undo Button
// @description Adds an undo button to the player controls, which undoes the last skip action.
// @version 0.0.2
// @author aequabit
// @match https://www.youtube.com/watch*
// @run-at document-start
// @downloadURL https://gist.github.com/aequabit/39c3150dcd31dcb6c92aff8024e8cfba/raw/youtube-player-undo-button.user.js
// ==/UserScript==
// Mostly taken from https://chromewebstore.google.com/detail/youtube-undo/jdoinlndbffpmioacnkpkjdbcphpembi
(async () => {
const state = {
savedTime: 0,
skipTo: null
};
const btnUndo = document.createElement("button");
btnUndo.classList.add("ytp-button");
btnUndo.id = "ytp-button-undo";
// https://fonts.google.com/icons?selected=Material%20Symbols%20Outlined%3Aundo%3AFILL%400%3Bwght%40400%3BGRAD%400%3Bopsz%4024&icon.query=undo&icon.size=24&icon.color=%23e3e3e3
btnUndo.innerHTML = `
<svg height="100%" viewBox="0 0 36 36" width="100%">
<path d="m 13.368712,25 v -1.866666 h 6.576429 q 1.458856,0 2.535631,-0.933333 1.076774,-0.933334 1.076774,-2.333334 0,-1.4 -1.076774,-2.333333 -1.076775,-0.933333 -2.535631,-0.933333 h -5.835423 l 2.40827,2.426666 -1.296761,1.306666 L 10.589938,15.666667 15.221227,11 l 1.296761,1.306666 -2.40827,2.426667 h 5.835423 q 2.246175,0 3.855548,1.47 1.609373,1.470001 1.609373,3.663334 0,2.193334 -1.609373,3.663333 Q 22.191316,25 19.945141,25 Z" id="path1" style="stroke-width:0.0232447;fill:#ffffff;fill-opacity:1" />
</svg>
`;
btnUndo.addEventListener("click", _ => {
if (state.skipTo !== null)
document.getElementsByTagName('video')[0].currentTime = state.skipTo;
})
setInterval(() => {
if (document.querySelector(".ytp-right-controls") && !document.querySelector("#ytp-button-undo"))
document.querySelector(".ytp-right-controls").prepend(btnUndo);
if (document.getElementsByTagName('video').length > 0) {
const playTime = document.getElementsByTagName('video')[0].currentTime;
if (playTime > state.savedTime + 10 || playTime < state.savedTime - 10)
state.skipTo = state.savedTime;
state.savedTime = playTime - 1;
}
}, 1000)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment