65.9K
CodeProject 正在变化。 阅读更多。
Home

一个为 YouTube 频道生成 RSS 订阅链接的 GreaseMonkey 脚本

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2018 年 5 月 29 日

CPOL

1分钟阅读

viewsIcon

4459

YouTube 频道的 RSS 订阅

许多人使用 RSS 订阅作为获取新闻和信息的主要方式。RSS 是匿名的,不需要您登录。

虽然 YouTube 会为它的频道生成 RSS 订阅,但它不会向(人类)访客显示它们,也不会向浏览器软件宣传它们。如果您更喜欢通过 RSS 浏览 YouTube 视频,那么您需要手动构建每个频道的订阅 URL。

我决定使用这个 Greasemonkey 脚本来自动化这个过程。在 YouTube 视频页面加载后几秒钟,JavaScript 会在频道名称旁边添加一个 RSS 图标图像。该图像链接到 YouTube 频道的 RSS 订阅。 (RSS 图标可以无缝工作,因为它来自 Google 并在其“新闻”页面中使用。) 这个 Greasemonkey 脚本还在 HTML HEAD 部分添加一个 RSS 链接标签,以便浏览器可以激活它们的 RSS 订阅按钮。

// ==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; }
        }
      }
    }
  }
}

一个 YouTube 频道在 Bamboo RSS 订阅阅读器(Firefox 插件或扩展)中的列表。

RSS 提供了一种更轻松地查看 Youtube 频道的方式。YouTube 关心的只是人们看到他们的广告。RSS 不会屏蔽广告,所以没有问题。依赖订阅的观众如果不能定期登录,可能会错过新的但不是最新的视频。(这些视频会被折叠/包裹/隐藏,因为会发布更新的视频。)RSS 订阅确保观众更有可能了解新视频的存在。

并且,要使用此脚本,您需要在浏览器中安装 Greasemonkey 插件。可以从 Firefox/Seamonkey 浏览器插件搜索页面安装(工具 – 插件 从主菜单)。

© . All rights reserved.