Persistent YouTube video queue

Make the YoutTube video queue persistent through sessions

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==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});
})()