From acdf89e736cd4701f5d555a5f228659a6e2cdcdb Mon Sep 17 00:00:00 2001 From: Jon Banafato Date: Tue, 7 Jul 2020 18:51:19 -0400 Subject: [PATCH] Improve YouTube feed detection YouTube supports channels identified by both channel ID and channel name. For example, https://www.youtube.com/c/dslrvideoshooter and https://www.youtube.com/channel/UCMmA0XxraDP7ZVbv4eY3Omg are the same channel, but the former has a more human-friendly URL. This change allows the extension to detect the channel's RSS feed regardless of which URL is loaded using the canonical link provided on each channel page. This change is fully backward compatible. --- rsspreview.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/rsspreview.js b/rsspreview.js index c92e3e9..67afec5 100644 --- a/rsspreview.js +++ b/rsspreview.js @@ -379,10 +379,16 @@ } function findYouTubeFeeds() { - let match = document.URL.match(/channel\/([a-zA-Z0-9_-]+)/); - if (match) { + // YouTube's canonical channel URLs look like /channel/AlphaNumericID + // It also supports named channels of the form /c/MyChannelName + // Match on both of these to autodetect channel feeds on either URL + let idPattern = /channel\/(?[a-zA-Z0-9_-]+)/; + let namePattern = /c\/[a-zA-Z0-9_-]+/; + let urlPattern = new RegExp(`${idPattern.source}|${namePattern.source}`); + if (document.URL.match(urlPattern)) { let feeds = {}; - let channelId = match[1]; + let canonicalUrl = document.querySelector("link[rel='canonical']").href; + let channelId = canonicalUrl.match(idPattern).groups.channelId; let url = `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`; let title = document.title; feeds[url] = title;