Click here to Skip to main content
15,867,835 members
Articles / RSS

A GreaseMonkey Script to Generate RSS Feed Links for YouTube Channels

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 May 2018CPOL1 min read 3.9K  
RSS feed for YouTube channels

Many people use RSS feeds as the primary means to access news and information. RSS is anonymous and does not require you to log in.

Though YouTube generates RSS feeds for its channels, it does not display them (to human visitors) or advertise them (to browser software). If you prefer to browse YouTube videos via RSS, then you have to manually construct the feed URL for each channel.

I decided to automate it using this Greasemonkey script. A few seconds after a YouTube video page gets loaded, the JavaScript adds an RSS icon image next to the channel name. It links the image to the RSS feed of the YouTube channel. (The RSS icon works seamlessly as it is from Google and is used in its “News” pages.) The Greasemonkey script also adds an RSS link tag to the HTML HEAD section so that browsers can activate their RSS feed button.

JavaScript
// ==UserScript==
// @name        YouTube RSS feed generator
// @namespace   com.vsubhash.js.youtube-rss-feed-generator
// @description Adds a RSS feed button to YouTube channels
// @include     https://www.youtube.com/watch*
// @version     1
// @grant       none
// ==/UserScript==

document.addEventListener("DOMContentLoaded", startItDelayed, false);
 
function startItDelayed() {
  if (document.getElementById("YT_RSS_Feed") == null) {
    window.setTimeout(addRssButton, 5000);    
  }  
}

function addRssButton() {  
  var oDivs = document.getElementsByTagName("div");
  if ((oDivs != null) && (oDivs.length > 0)) {
    
    for (var i = 0; i < oDivs.length; i++) {
      if (oDivs[i].className == "yt-user-info") {
        //console.log("YRFG Error: Here");
        var oAnchors = oDivs[i].getElementsByTagName("a");
        if ((oAnchors != null) && (oDivs.length>1)) {
          var bFound = false;
          for (var j = 0; j < oAnchors.length; j++) {
            //console.log("YRFG Error: " + oAnchors[j].href.substring
            //(0, "https://www.youtube.com/channel/".length));
            if (oAnchors[j].href.substring(0, 
                "https://www.youtube.com/channel/".length) == 
                "https://www.youtube.com/channel/") {
              var sChannelId = oAnchors[j].href.substring
                               ("https://www.youtube.com/channel/".length);
              
              var oRssElement = document.createElement("a");
              oRssElement.id = "YT_RSS_Feed";
              oRssElement.href = 
              "https://www.youtube.com/feeds/videos.xml?channel_id=" + sChannelId;
              oRssElement.innerHTML = 
              "<img src=\"https://www.google.com/images/rss.png\" 
                style=\"margin: auto 1em; \" />";
              oAnchors[j].appendChild(oRssElement);
              
              var oLinkElement = document.createElement("link");
              oLinkElement.setAttribute("rel", "alternate");
              oLinkElement.setAttribute("title", oAnchors[j].textContent);
              oLinkElement.setAttribute("type", "application/rss+xml");
              oLinkElement.setAttribute("href", 
              "https://www.youtube.com/feeds/videos.xml?channel_id=" + sChannelId);
              document.getElementsByTagName("head")[0].appendChild(oLinkElement);
              
              bFound = true;
              break;
            }
          }
          if (bFound) { break; }
        }
      }
    }
  }
}

Image 1

A YouTube channel listing in the Bamboo RSS feed reader (a Firefox add-on or extension).

RSS provides an easier way to check Youtube channels. All that matters to YouTube is that people see their ads. RSS does not block ads and so no problem. Viewers who rely on subscriptions are likely to miss new but not so fresh videos if they don’t log in regularly. (Such videos get folded/wrapped/hidden as newer videos are published.) RSS feeds ensure that viewers are more likely to get to know about a new video’s existence.

And, to use this script, you will need the Greasemonkey add-on in the browser. It can be installed from the Firefox/Seamonkey browser add-on search page (Tools – Add-ons from the main menu).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer www.VSubhash.in
India India
V. Subhash is an invisible Indian writer, programmer and illustrator. In 2020, he wrote one of the biggest jokebooks of all time and then ended up with over two dozen mostly non-fiction books including Linux Command-Line Tips & Tricks, CommonMark Ready Reference, PC Hardware Explained, Cool Electronic Projects and How To Install Solar. His book Quick Start Guide to FFmpeg has been published by Apress/SpringerNature in 2023. He wrote, illustrated, designed and produced all of his books using only open-source software. Subhash has programmed in more than a dozen languages (as varied as assembly, Java and Javascript); published software for desktop (NetCheck), mobile (Subhash Browser & RSS Reader) and web (TweetsToRSS); and designed several websites. As of 2023, he is working on a portable Javascript-free CMS using plain-jane PHP and SQLite. Subhash also occasionally writes for Open Source For You magazine and CodeProject.com.

Comments and Discussions

 
-- There are no messages in this forum --