Persistent YouTube video queue

Make the YoutTube video queue persistent through sessions

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Persistent YouTube video queue
// @version      0.3
// @namespace    Violentmonkey Scripts
// @description  Make the YoutTube video queue persistent through sessions
// @author       CarlosMarques
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==


(function(){
	let ytqItems = Object.keys(localStorage).filter(e => e.startsWith("yt-queue"));
	if(ytqItems.length && !Object.keys(window.sessionStorage).some(e => e.startsWith("yt-queue"))){
		for(let yqsi of ytqItems){
			window.sessionStorage.setItem(yqsi, localStorage.getItem(yqsi));
		}
	}

	let mObserver = new MutationObserver(function(mutations){
		let ytqItems = Object.keys(window.sessionStorage).filter(e => e.startsWith("yt-queue"));
		if(!ytqItems.length){
			Object.keys(localStorage).forEach(e => e.startsWith("yt-queue") ? localStorage.removeItem(e) : null);
		}
		else{
			ytqItems.forEach(e => localStorage.setItem(e, window.sessionStorage.getItem(e)));
		}
	})
	mObserver.observe(document.body, {subtree: true, childList: true, characterData: true});
})()