Persistent YouTube video queue

Make the YoutTube video queue persistent through sessions

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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