Youtube Subscription List Quick Playlist

Allows you to quickly create playlists from your Youtube subscription feed

目前为 2016-04-26 提交的版本。查看 最新版本

// ==UserScript==
// @name        Youtube Subscription List Quick Playlist
// @namespace   https://gf.qytechs.cn/en/users/13981-chk1
// @description Allows you to quickly create playlists from your Youtube subscription feed
// @include     https://www.youtube.com/feed/subscriptions/activity
// @version     1.0
// @grant       none
// @run-at      document-end
// ==/UserScript==

var videoIds = [];
var playlistElements;

function createPlaylistLink() {
	var playlistLinkContainer = document.createElement('div'); 
	playlistLinkContainer.style.position = "fixed";
	playlistLinkContainer.style.bottom = "10px";
	playlistLinkContainer.style.right = "10px";
	playlistLinkContainer.className = "yt-card yt-card-has-padding";
	var playlistLinkH3 = document.createElement('h3'); 
	playlistLinkH3.className = "yt-lockup-title";
	var playlistLink = document.createElement('a'); 
	playlistLink.href="https://www.youtube.com/watch_videos?video_ids=";
	playlistLink.style.display = "block";
	playlistLink.style.clear = "both";
	var playlistLinkText = document.createTextNode('Your playlist link'); 
	
	var clearLink = document.createElement('a'); 
	clearLink.style.float = "right";
	clearLink.style.fontSize = "smaller";
	clearLink.style.clear = "both";
	clearLink.style.paddingTop = "5px";
	clearLink.style.display = "block";
	clearLink.style.color = "#b00";
	clearLink.onclick = function() { clearPlaylist() };
	var clearText = document.createTextNode('Clear'); 

	clearLink.appendChild(clearText);
	playlistLink.appendChild(playlistLinkText);
	playlistLinkH3.appendChild(playlistLink);
	playlistLinkContainer.appendChild(playlistLinkH3);
	playlistLinkContainer.appendChild(clearLink);

	return {
		'container': playlistLinkContainer, 
		'link': playlistLink, 
		'text': playlistLinkText
	};
}

function updatePlaylistLink() {
	if(playlistElements.text.textContent.length > 2048){
		playlistElements.text.textContent = "Too many videos, URL too long :(";
	} else {
		playlistElements.link.href="https://www.youtube.com/watch_videos?video_ids="+videoIds.join(',');
		playlistElements.text.textContent = "Your playlist link ("+videoIds.length+" videos)";
	}
}

function clearPlaylist() {
	videoIds = [];
	var buttons = document.querySelectorAll('#browse-items-primary > .section-list .subs2playlist');
	for (var i = 0; i < buttons.length; ++i) {
		buttons[i].classList.remove("yt-uix-button-subscribed-branded");
		buttons[i].classList.add("c4-editor-plus");
	}
	updatePlaylistLink();
}

function toggleVideoId(videoId){
	var alreadyIn = videoIds.indexOf(videoId);
	if(alreadyIn === -1){
		videoIds.push(videoId);
		return true;
	} else {
		videoIds.splice(alreadyIn, 1);
		return false;
	}
}

function createPlusButton(videoId){
	var container = document.createElement('button'); 
	container.setAttribute('onclick', 'toggleVideoId(\''+videoId+'\');');
	container.className = "subs2playlist yt-uix-button yt-uix-button-size-default yt-uix-button-default yt-uix-button-has-icon no-icon-markup yt-uix-inlineedit-edit c4-editor-plus";
	container.onclick = function(){ 
		toggleVideoId(videoId);
		container.classList.toggle("yt-uix-button-subscribed-branded");
		container.classList.toggle("c4-editor-plus");
		updatePlaylistLink();
	};
	container.innerHTML = "<span class=\"yt-uix-button-content\">Add to new playlist</span>";
	return container;
}

function appendAllTheThings(node) {
	var videoId = node.getAttribute("data-context-item-id");
	var plusButtonNode = createPlusButton(videoId);
	node.appendChild(plusButtonNode);
}

var observerConfig = { 
  childList: true,
  attributes: true, 
  subtree: false,
  attributeOldValue: false
};

var listObserver = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if(mutation.type == "childList" && mutation.addedNodes.length >= 1) {
      console.log(mutation);
      for (var i = 0; i < mutation.addedNodes.length; ++i) {
      	var node = mutation.addedNodes[i];
      	if(node.nodeType === 1){
      		var videoLinkContainer = node.querySelector('.yt-lockup');
      		appendAllTheThings(videoLinkContainer);
      	}
      }
    }
  });    
});
var subListContainer = document.querySelector('#browse-items-primary > .section-list');
listObserver.observe(subListContainer, observerConfig);

function firstRun(){
	var videoLinkNodes = document.querySelectorAll('div.yt-lockup');
	for (var i = 0; i < videoLinkNodes.length; ++i) {
		var node = videoLinkNodes[i];
		appendAllTheThings(node);
	}
	playlistElements = createPlaylistLink();
	document.body.appendChild(playlistElements.container);
}

firstRun();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址