Skip to content

Commit

Permalink
feat: support old YouTube layout
Browse files Browse the repository at this point in the history
  • Loading branch information
teddy-gustiaux committed Nov 10, 2018
1 parent 367a7ae commit ab42c24
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/content-script.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ab42c24

Please sign in to comment.