YouTube RSS Feed

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

当前为 2015-05-20 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          YouTube RSS Feed
// @namespace     http://greasyfork.org/users/2240-doodles
// @author        Doodles
// @version       14
// @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
// @updateVersion 14
// ==/UserScript==

// Using a work-around to use jQuery, and be compatible with Chrome
// Found here: http://stackoverflow.com/a/3550261

(function() {
	function addJQuery(callback){
		var script = document.createElement("script");
		script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js");
		script.addEventListener('load', function(){
			var script = document.createElement("script");
			script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
			document.body.appendChild(script);
		}, false);
		document.body.appendChild(script);
	}
	function RssFeedButton(){
		// YouTube page load fix, so script runs properly on every page
		jQ('a').click(function( event ) {
			event.preventDefault();
			window.location = jQ(this).attr("href");
		});
		// NOTE: this is a horrible solution, but everything else I tried didnt work 100% of the time
		// Discussion of this problem found here: http://stackoverflow.com/a/21146511
		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('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 + '" />');
			}
		}
	}
	addJQuery(RssFeedButton);
})();