YouTube RSS Feed

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

目前為 2015-04-24 提交的版本,檢視 最新版本

// ==UserScript==
// @name          YouTube RSS Feed
// @namespace     http://gf.qytechs.cn/users/2240-doodles
// @author        Doodles
// @version       12
// @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 12
// ==/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.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 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.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);
})();

QingJ © 2025

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