YouTube RSS Feed

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

目前為 2018-04-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name        YouTube RSS Feed
// @namespace   http://greasyfork.org/users/2240-doodles
// @author      Doodles
// @version     17
// @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
// @match       *://www.youtube.com/*
// @match       *://youtube.com/*
// @grant       none
// @run-at      document-end
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// ==/UserScript==

this.$ = this.jQuery = jQuery.noConflict(true);

$(function () {
    "use strict";
    var additionalWait = setInterval(function () {
        if (additonalWaitComplete()) {
            addRssFeedSupport();
            clearInterval(additionalWait);
        }
    }, 1000);
    document.body.addEventListener("yt-navigate-finish", function (event) {
        addRssFeedSupport();
    });
});

function addRssFeedSupport() {
    if (isPlaylistPage()) {
        var channelFeedLink = getPlaylistFeed(getPlatlistId());
        addRssLink(channelFeedLink);
        addRssButtonPlaylist(channelFeedLink);
    } else if (isVideoPage()) {
        var channelFeedLink = getChannelFeed(getChannelIdFromVideoPage());
        addRssLink(channelFeedLink);
        addRssButton(channelFeedLink);
    } else if (isChannelPage()) {
        var channelFeedLink = getChannelFeed(getChannelIdFromChannelPage());
        addRssLink(channelFeedLink);
        addRssButton(channelFeedLink);
    }
}

function isPlaylistPage() {
    return document.URL.indexOf("/playlist?list=") !== -1;
}

function isVideoPage() {
    return document.URL.indexOf("/watch") !== -1 && document.URL.indexOf("v=") !== -1;
}

function isChannelPage() {
    return $("#channel-header").length > 0;
}

function additonalWaitComplete() {
    if (isPlaylistPage()) {
        if (document.URL.indexOf("disable_polymer=true") !== -1) {
            return $("#pl-header").length > 0;
        } else {
            return $("#items").length > 0;
        }
    } else if (isVideoPage()) {
        return $("#upload-info").length > 0;
    } else if (isChannelPage()) {
        return $("#channel-title-container").length > 0;
    } else {
        return true; // default to true to kill loop on unknown pages
    }
}

function getPlatlistId() {
    var playlistId = document.URL.split("list=")[1].split("&")[0];
    if (!playlistId.startsWith("PL")) {
        playlistId = "PL" + playlistId;
    }
    return playlistId;
}

function getChannelIdFromVideoPage() {
    return $("#upload-info a[href*='/channel/']:first").attr("href").split("/channel/")[1];
}

function getChannelIdFromChannelPage() {
    return $("meta[property='og:url']").attr("content").split("/channel/")[1];
}

function getChannelFeed(channelId) {
    return "http://www.youtube.com/feeds/videos.xml?channel_id=" + channelId;
}

function getPlaylistFeed(playlistId) {
    return "http://www.youtube.com/feeds/videos.xml?playlist_id=" + playlistId;
}

function addRssLink(link) {
    if ($("link[type='application/rss+xml']").length === 0) {
        $("head").append('<link rel="alternate" type="application/rss+xml" title="RSS" href="' +
                link + '" />');
    }
}

function addRssButton(link) {
    if ($("#rssSubButton").length === 0) {
        $("#subscribe-button")
                .css({
                    "display": "flex",
                    "flex-flow": "nowrap",
                    "height": "37px"
                })
                .prepend(makeRssButton(link));
    }
}

function addRssButtonPlaylist(link) {
    if ($("#rssSubButton").length === 0) {
        if (document.URL.indexOf("disable_polymer=true") !== -1) {
            $(".playlist-actions").prepend(makeRssButton(link));
        } else {
            $("#owner-container > #button")
                    .css({
                        "display": "flex",
                        "flex-flow": "nowrap",
                        "height": "37px"
                    })
                    .prepend(makeRssButton(link));
        }
    }
}

function makeRssButton(link) {
    return $("<a>RSS</a>")
            .attr("id", "rssSubButton")
            .attr("target", "_blank")
            .attr("href", link)
            .css({
                "background-color": "#fd9b12",
                "border-radius": "3px",
                "padding": "10px 16px",
                "color": "#ffffff",
                "font-size": "14px",
                "text-decoration": "none",
                "text-transform": "uppercase",
                "margin-right": "5px"
            });
}