YouTube Playlist Close

Allow quick closing of playlists

当前为 2016-12-09 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Playlist Close
// @version      1.0
// @description  Allow quick closing of playlists
// @author       AjaxGb
// @match        http*://www.youtube.com/*
// @run-at       document-start
// @resource     button https://raw.githubusercontent.com/AjaxGb/YouTube-Playlist-Close/master/closeMedium.png
// @grant        GM_getResourceURL
// @noframes
// @namespace https://greasyfork.org/users/85711
// ==/UserScript==

(function(){
	'use strict';

	function getQueryArgs(query){
		query = query || window.location.search.substring(1);
		if(!query) return {};
		return query.split('&').reduce(function(prev, curr){
			const p = curr.split('=');
			prev[decodeURIComponent(p[0])] = p[1] ? decodeURIComponent(p[1]) : p[1];
			return prev;
		}, {});
	}

	function setQueryArgs(query){
		if(!query) return;
		let search = '';
		for(let prop in query){
			if(query[prop] === undefined){
				search += '&'+encodeURIComponent(prop);
			}else{
				search += '&'+encodeURIComponent(prop)+'='+encodeURIComponent(query[prop]);
			}
		}
		window.location.search = '?' + search.substr(1);
	}

	const b = document.createElement('button'), s = b.style;
	s.width  = '26px';
	s.height = '28px';
	s.right = s.top = '0';
	s.background = 'url("' + GM_getResourceURL('button') + '") center';
	s.position = 'absolute';
	s.cursor = 'pointer';
	s.opacity = 0.5;
	b.classList.add('yt-uix-tooltip');
	b.title = 'Close playlist';

	b.onmouseenter = function(){
		s.opacity = 0.6;
	};
	b.onmouseleave = function(){
		s.opacity = 0.5;
	};
	b.onclick = function(){
		const q = getQueryArgs();
		q.time_continue = document.getElementById('movie_player').getCurrentTime()|0;
		delete q.list;
		delete q.index;
		setQueryArgs(q);
	};

	function addButton(p){
		p.style.position = 'relative';
		p.appendChild(b);
	}

	const observer = new MutationObserver(function(mrs){
		if(document.contains(b)) return;
		for(let i = mrs.length - 1; i >= 0; --i){
			const t = mrs[i].target;
			if(t){
				if(t.classList && t.classList.contains('playlist-info')){
					addButton(t);
				}else if (t.getElementsByClassName){
					const p = t.getElementsByClassName('playlist-info')[0];
					if(p) addButton(p);
				}
			}
		}
	});
	observer.observe(document.documentElement, {
		childList: true,
		subtree: true
	});
})();