YouTube RSS Feed

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

目前為 2015-05-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();