Bookmarklet to go to YouTube channel RSS feed I've written a simple bookmarklet to go to the YouTube channel RSS feed URL from at least any video or channel page:

javascript:(function () {
    var newLocation = function () {
        var url;
        Array.prototype.slice.call(document.getElementsByTagName('link')).forEach(function (element) {
            if (element.getAttribute('type') === 'application/rss+xml') {
                console.log('Found direct feed link');
                url = element.getAttribute('href');
            }
        });
        if (!url) {
            Array.prototype.slice.call(document.getElementsByTagName('meta')).forEach(function (element) {
                if (element.getAttribute('itemprop') === 'channelId') {
                    console.log('Found channel ID');
                    url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' + element.getAttribute('content');
                }
            });
        }
        return url;
    }();
    if (newLocation === undefined) {
        console.log('Could not find a channel RSS feed from ' + location.href);
    } else {
        location.href = newLocation;
    }
})();


Some questions which come to mind:


Is this the simplest way to find an element with a specific attribute value?
Does this work with recent browsers other than Firefox?
Does it use any non-standard/deprecated features?
Are there any YouTube pages this does not work on, where it should?
Is this the simplest way to find an element with a specific attribute value?


"simple" is subjective. It could mean anything, like lesser lines, fewer keystrokes, lesser complexity. To me, what I consider simple is code that's short, concise and one that I can understand without running the code.


  Does this work with recent browsers other than Firefox?


With regards to the code, I don't see why not. However, I'm not sure how bookmarklets work with browsers these days. That part you might want to check.


  Does it use any non-standard/deprecated features?


Nothing out of the ordinary. You might want to check MDN or Kangax's compatibility table for more info about what APIs are available to which browsers.


  Are there any YouTube pages this does not work on, where it should?


Not something you'd normally ask in Code Review. That's up to you to find out.



You could substitute array.forEach with find. That way, it bails out on the first item found and not go through the entire array.

Next, you can use template literals instead of string concatenation. It's much more concise and lessens the chance that you'll miss a quote or a + somewhere.

Next, your logic has a lot of if statements. You could try to invert the logic and bail out early. You can also split your logic into functions, that way you can maintain extraction methods with clear separation. Then call them in sequence with || between. That way, the script uses the return value of first call that returns a value, or tries the next if the previous fails. Same conditional effect without the use of if and a lot of nested code.

Something along these lines:

javascript:(function(){
  const slice = Array.prototype.slice;

  function getUrlFromRss(){
    const links = slice.call(document.getElementByTagName('link'));
    const link = links.find(l =&gt; l.getAttribute('type') === 'application/rss+xml');
    return link ? link.getAttribute('href') : '';
  }

  function getUrlFromMeta(){
    const metas = slice.call(document.getElementsByTagName('meta'));
    const meta = metas.find(m =&gt; m.getAttribute('itemprop') === 'channelId');
    const id = meta ? meta.getAttribute('content') : '';
    return id ? `https://www.youtube.com/feeds/videos.xml?channel_id=${id}` : '';
  }

  const url = getUrlFromRss() || getUrlFromMeta() || '';

  if(!url) return console.log(`Could not find a channel RSS feed from ${location.href}`)

  location.href = url;
})();