YouTube RSS Feed

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

当前为 2015-04-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube RSS Feed
  3. // @namespace http://greasyfork.org/users/2240-doodles
  4. // @author Doodles
  5. // @version 13
  6. // @description Adds an RSS feed button to YouTube channels next to the subscribe button
  7. // @icon http://i.imgur.com/Ty5HNbT.png
  8. // @icon64 http://i.imgur.com/1FfVvNr.png
  9. // @include *://*youtube.*/*
  10. // @grant none
  11. // @run-at document-end
  12. // @updateVersion 13
  13. // ==/UserScript==
  14.  
  15. // Using a work-around to use jQuery, and be compatible with Chrome
  16. // Found here: http://stackoverflow.com/a/3550261
  17.  
  18. (function() {
  19. function addJQuery(callback){
  20. var script = document.createElement("script");
  21. script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js");
  22. script.addEventListener('load', function(){
  23. var script = document.createElement("script");
  24. script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
  25. document.body.appendChild(script);
  26. }, false);
  27. document.body.appendChild(script);
  28. }
  29. function RssFeedButton(){
  30. // YouTube page load fix, so script runs properly on every page
  31. jQ('a').click(function( event ) {
  32. event.preventDefault();
  33. window.location = jQ(this).attr("href");
  34. });
  35. // NOTE: this is a horrible solution, but everything else I tried didnt work 100% of the time
  36. // Discussion of this problem found here: http://stackoverflow.com/a/21146511
  37. var rssFeedNew = "feed://www.youtube.com/feeds/videos.xml?channel_id=";
  38. var rssFeed = "nope";
  39. if(document.URL.indexOf("/playlist?list=") != -1){
  40. rssFeed = "feed://gdata.youtube.com/feeds/api/playlists/" + document.URL.split("list=")[1].split("&")[0];
  41. }else if(document.getElementById('c4-primary-header-contents') != null){
  42. var metaId = jQ("meta[itemprop='channelId']");
  43. if(metaId.length != 0){
  44. rssFeed = rssFeedNew + jQ(metaId[0]).attr("content");
  45. }else{
  46. var linkRss = jQ("link[title='RSS']");
  47. if(linkRss.length != 0){
  48. rssFeed = jQ(linkRss[0]).attr("href");
  49. if(rssFeed.startsWith("https")){
  50. rssFeed = "feed" + rssFeed.substring(5);
  51. }else if(rssFeed.startsWith("http")){
  52. rssFeed = "feed" + rssFeed.substring(4);
  53. }
  54. }
  55. }
  56. }else if(document.URL.indexOf("/watch") != -1 && document.URL.indexOf("v=") != -1){
  57. var metaId = jQ("meta[itemprop='channelId']");
  58. if(metaId.length != 0){
  59. rssFeed = rssFeedNew + jQ(metaId[0]).attr("content");
  60. }
  61. }
  62. if(rssFeed != "nope"){
  63. var button = document.createElement('button');
  64. 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');
  65. button.setAttribute('onclick', "parent.location='" + rssFeed + "'");
  66. var outerSpan = document.createElement('span');
  67. outerSpan.setAttribute('class', 'yt-uix-button-content');
  68. var innerSpan = document.createElement('span');
  69. innerSpan.setAttribute('class', 'subscribe-label');
  70. innerSpan.appendChild(document.createTextNode('RSS Subscribe '));
  71. button.appendChild(outerSpan);
  72. outerSpan.appendChild(innerSpan);
  73. jQ(button).css("background", "linear-gradient(#fd9b12, #fe6702)");
  74. jQ(button).mouseover(function(){ jQ(this).css("background", "linear-gradient(#fe6702, #fd9b12)"); });
  75. jQ(button).mouseout(function(){ jQ(this).css("background", "linear-gradient(#fd9b12, #fe6702)"); });
  76. if(document.URL.indexOf("/playlist?list=") != -1){
  77. var playlistControls = jQ("div.playlist-actions")[0];
  78. if(playlistControls != null){
  79. playlistControls.appendChild(button);
  80. }
  81. }else if(document.getElementById('c4-primary-header-contents') != null){
  82. var header = document.getElementById('c4-primary-header-contents');
  83. if(header != null){
  84. var divs = header.getElementsByTagName('span');
  85. for(var i = 0; i < divs.length;i++){
  86. var cl = divs.item(i).getAttribute('class');
  87. if(cl.indexOf("channel-header-subscription-button-container") != -1){
  88. var firstButton = divs.item(i).getElementsByTagName('button')[0];
  89. divs.item(i).insertBefore(button, firstButton);
  90. var spacer = document.createTextNode(" ");
  91. divs.item(i).insertBefore(spacer, firstButton);
  92. }
  93. }
  94. }
  95. }else if(document.URL.indexOf("/watch") != -1 && document.URL.indexOf("v=") != -1){
  96. var header = document.getElementById('watch7-subscription-container');
  97. if(header != null){
  98. var properSpan = header.getElementsByTagName('span')[0];
  99. properSpan.insertBefore(document.createTextNode(" "), properSpan.firstChild);
  100. properSpan.insertBefore(button, properSpan.firstChild);
  101. }
  102. }
  103. if(jQ("link[title='RSS']").length == 0){
  104. jQ('head').append('<link rel="alternate" type="application/rss+xml" title="RSS" href="' + rssFeed + '" />');
  105. }
  106. }
  107. }
  108. addJQuery(RssFeedButton);
  109. })();