YouTube RSS Feed

Adds an RSS feed button to YouTube channels next to the subscribe button

目前为 2015-06-22 提交的版本。查看 最新版本

// ==UserScript==
// @name          YouTube RSS Feed
// @namespace     http://greasyfork.org/users/2240-doodles
// @author        Doodles
// @version       15
// @description   Adds an RSS feed button to YouTube channels next to the subscribe button
// @icon          http://i.imgur.com/Ty5HNbT.png
// @icon64        http://i.imgur.com/1FfVvNr.png
// @include       *://*youtube.*/*
// @grant         none
// @run-at        document-end
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js
// @updateVersion 15
// ==/UserScript==

(function() {
	window.jQ=jQuery.noConflict(true);
	RssFeedButton();
	document.body.addEventListener("DOMSubtreeModified", SelectiveFire);
	function SelectiveFire(){
		setTimeout(function() {
			if(jQ('#youtubeRssButton').length == 0){
				RssFeedButton();
			}
		}, 2500);
	}
	function RssFeedButton(){
		var rssFeedNew = "feed://www.youtube.com/feeds/videos.xml?channel_id=";
		var rssFeed = "nope";
		if(document.URL.indexOf("/playlist?list=") != -1){
			rssFeed = "feed://gdata.youtube.com/feeds/api/playlists/" + document.URL.split("list=")[1].split("&")[0];
		}else if(document.getElementById('c4-primary-header-contents') != null){
			var metaId = jQ("meta[itemprop='channelId']");
			if(metaId.length != 0){
				rssFeed = rssFeedNew + jQ(metaId[0]).attr("content");
			}else{
				var linkRss = jQ("link[title='RSS']");
				if(linkRss.length != 0){
					rssFeed = jQ(linkRss[0]).attr("href");
					if(rssFeed.startsWith("https")){
						rssFeed = "feed" + rssFeed.substring(5);
					}else if(rssFeed.startsWith("http")){
						rssFeed = "feed" + rssFeed.substring(4);
					}
				}
			}
		}else if(document.URL.indexOf("/watch") != -1 && document.URL.indexOf("v=") != -1){
			var metaId = jQ("meta[itemprop='channelId']");
			if(metaId.length != 0){
				rssFeed = rssFeedNew + jQ(metaId[0]).attr("content");
			}
		}
		if(rssFeed != "nope"){
			var button = document.createElement('button');
			button.setAttribute('class', 'yt-uix-button yt-uix-button-size-default yt-uix-button-subscribe-branded yt-uix-button-has-icon no-icon-markup');
			button.setAttribute('id', 'youtubeRssButton');
			//button.setAttribute('class', 'yt-uix-button yt-uix-button-size-default yt-uix-button-subscribe-branded yt-uix-button-has-icon no-icon-markup yt-uix-subscription-button yt-can-buffer');
			button.setAttribute('onclick', "parent.location='" + rssFeed + "'");
			var outerSpan = document.createElement('span');
			outerSpan.setAttribute('class', 'yt-uix-button-content');
			var innerSpan = document.createElement('span');
			innerSpan.setAttribute('class', 'subscribe-label');
			innerSpan.appendChild(document.createTextNode('RSS Subscribe '));
			button.appendChild(outerSpan);
			outerSpan.appendChild(innerSpan);
			jQ(button).css("background", "linear-gradient(#fd9b12, #fe6702)");
			jQ(button).mouseover(function(){ jQ(this).css("background", "linear-gradient(#fe6702, #fd9b12)"); });
			jQ(button).mouseout(function(){ jQ(this).css("background", "linear-gradient(#fd9b12, #fe6702)"); });
			if(document.URL.indexOf("/playlist?list=") != -1){
				var playlistControls = jQ("div.playlist-actions")[0];
				if(playlistControls != null){
					playlistControls.appendChild(button);
				}
			}else if(document.getElementById('c4-primary-header-contents') != null){
				var header = document.getElementById('c4-primary-header-contents');
				if(header != null){
					var divs = header.getElementsByTagName('span');
					for(var i = 0; i < divs.length;i++){
						var cl = divs.item(i).getAttribute('class');
						if(cl.indexOf("channel-header-subscription-button-container") != -1){
							var firstButton = divs.item(i).getElementsByTagName('button')[0];
							divs.item(i).insertBefore(button, firstButton);
							var spacer = document.createTextNode(" ");
							divs.item(i).insertBefore(spacer, firstButton);
						}
					}
				}
			}else if(document.URL.indexOf("/watch") != -1 && document.URL.indexOf("v=") != -1){
				var header = document.getElementById('watch7-subscription-container');
				if(header != null){
					var properSpan = header.getElementsByTagName('span')[0];
					properSpan.insertBefore(document.createTextNode(" "), properSpan.firstChild);
					properSpan.insertBefore(button, properSpan.firstChild);
				}
			}
			if(jQ("link[title='RSS']").length == 0){
				jQ('head').append('<link rel="alternate" type="application/rss+xml" title="RSS" href="' + rssFeed + '" />');
			}
		}
	}
})();