Skip to content

Instantly share code, notes, and snippets.

@mucaho
Created December 7, 2021 06:35
Show Gist options
  • Save mucaho/9806379dd582ef6d031286eb8440b1f4 to your computer and use it in GitHub Desktop.
Save mucaho/9806379dd582ef6d031286eb8440b1f4 to your computer and use it in GitHub Desktop.
YouTube restore video time - ViolentMonkey user script
// ==UserScript==
// @name YouTube restore video time
// @namespace Violentmonkey Scripts
// @match https://*.youtube.com/*
// @grant none
// @version 1.0
// @author mucaho
// @description -
// ==/UserScript==
var savedVideoTimePrefix = "SAVED_VIDEO_TIME_";
var player = document.getElementById("movie_player");
if (player) {
player.addEventListener("onStateChange", onPlayerStateChange);
function onPlayerStateChange(playerState) {
var videoId, videoTime;
if (playerState !== 1 /* YT.PlayerState.PLAYING */) return;
if (player.getCurrentTime() > 1) return;
if (!(videoId = player.getVideoData().video_id)) return;
if (!(videoTime = +window.localStorage.getItem(savedVideoTimePrefix+videoId))) return;
player.seekTo(videoTime, true);
}
window.addEventListener("beforeunload", function(evt) {
var videoId, videoTime;
if (!(videoId = player.getVideoData().video_id)) return;
if (!(videoTime = player.getCurrentTime())) return;
window.localStorage.setItem(savedVideoTimePrefix+videoId, ""+videoTime);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment