From ab42c24d0efc967585e102471b99cb6ac1ddb19e Mon Sep 17 00:00:00 2001 From: Teddy Gustiaux Date: Sat, 10 Nov 2018 17:31:57 -0500 Subject: [PATCH] feat: support old YouTube layout --- src/content-script.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/content-script.js b/src/content-script.js index 9a37e51..7da899e 100644 --- a/src/content-script.js +++ b/src/content-script.js @@ -1,15 +1,35 @@ +/** + * List of selectors to retrieve the channel homepage address from the DOM. + * Supports both new and old YouTube layouts. + * @type {Array} + */ +const CHANNEL_URL_SELECTORS = [ + 'yt-formatted-string#owner-name :first-child', + '.yt-user-info :first-child', +]; + +/** + * Check if the provided input is nil (`null` or `undefined`) + * @param {*} input The input to test + * @returns {boolean} `true` if the input is nil, `false` otherwise + */ +function isNil(input) { + return input === null || input === undefined; +} + /** * Retrieve the YouTube channel URL from the DOM. * @returns {(string|null)} The URL of the channel or `null` if not found */ function findChannelAddress() { - const ELEMENT = 'yt-formatted-string'; - const ID = 'owner-name'; - const container = window.document.querySelector(`${ELEMENT}#${ID}`); - if (container !== undefined && container.firstChild.href !== undefined) { - return container.firstChild.href; + let channelAddress = null; + // eslint-disable-next-line + for (let urlSelector of CHANNEL_URL_SELECTORS) { + if (!isNil(channelAddress)) break; + const container = window.document.querySelector(urlSelector); + if (!isNil(container)) channelAddress = container.href; } - return null; + return channelAddress; } // Notify the extension by returning the found URL