alike03 / IMDB Youtube Links

// ==UserScript==
// @name        IMDB Youtube Links
// @namespace   IMDB_youtube
// @include     https://*.imdb.com/title/*
// @version     7.2
// @copyright   2024, alike03 (https://openuserjs.org/users/alike03)
// @updateURL   https://openuserjs.org/meta/alike03/IMDB_Youtube_Links.meta.js
// @downloadURL https://openuserjs.org/install/alike03/IMDB_Youtube_Links.user.js
// @license MIT
// ==/UserScript==

/* jshint esversion: 6 */

const path = window.location.pathname;

if (path.split('/')[1] === 'title') {
  window.addEventListener('load', function() {

    const channels = [
      { name: 'RedLetterMedia', url: 'https://www.youtube.com/@RedLetterMedia/search?query=__TITLE__' },
      { name: 'Jeremy Jahns', url: 'https://www.youtube.com/@JeremyJahns/search?query=__TITLE__' },
      { name: 'Pitch Meeting', url: 'https://www.youtube.com/@PitchMeetings/search?query=__TITLE__' },
      { name: 'New Rockstars', url: 'https://www.youtube.com/@NewRockstars/search?query=__TITLE__' },
      { name: 'HISHE', url: 'https://www.youtube.com/@HowItShouldHaveEnded/search?query=__TITLE__' },
      { name: 'Screen Junkies', url: 'https://www.youtube.com/@screenjunkies/search?query=__TITLE__' },
      { name: 'CinemaSins', url: 'https://www.youtube.com/@CinemaSins/search?query=__TITLE__' },
      { name: 'CinemaWins', url: 'https://www.youtube.com/@CinemaWins/search?query=__TITLE__' },
      { name: 'Holden Hardman', url: 'https://www.youtube.com/@HoldenHardman/search?query=__TITLE__' },
      { name: 'Natalie Gold', url: 'https://www.youtube.com/@NatalieGoldReacts/search?query=__TITLE__' }
    ];

    const targets = document.querySelectorAll('ul.ipc-metadata-list [data-testid="title-pc-principal-credit"]');
    const parents = () => {
      let list = [];
      targets.forEach(target => {
        const parent = target.parentElement;
        if (list.indexOf(parent) === -1) {
          list.push(parent);
        }
      });
      return list;
    };

    const title = document.querySelector('h1').textContent;

    let container = document.createElement('li');
    container.setAttribute('class', 'ipc-metadata-list__item');

    let span = document.createElement('span');
    span.setAttribute('class', 'ipc-metadata-list-item__label');
    span.textContent = 'Youtube';
    container.appendChild(span);

    let div = document.createElement('div');
    div.setAttribute('class', 'ipc-metadata-list-item__content-container');
    div.setAttribute('style', 'width: calc(100% - 75px); padding-top: .75rem');
    container.appendChild(div);

    let ul = document.createElement('ul');
    ul.setAttribute('class', 'ipc-inline-list--show-dividers alikeYoutube');
    div.appendChild(ul);

    let li = document.createElement('li');
    li.setAttribute('class', 'ipc-inline-list__item');

    let all = createButton('ALL', '#', li);
    ul.appendChild(all);

    channels.forEach(channel => {
      const name = channel.name;
      const url = channel.url.replace('__TITLE__', title);
      let listItem = createButton(name, url, li);
      ul.appendChild(listItem);
    });

    all.addEventListener('click', function() {
      // open all links in new tabs
      let links = document.querySelectorAll('.alikeYoutube a');
      links.forEach(link => {
        window.open(link.href, '_blank');
      });
    });

    parents().forEach(parent => {
      parent.appendChild(container.cloneNode(true));
    });

  }, false);
}

function createButton(name, url, li) {
  let link = document.createElement('a');
  link.setAttribute('target', '_blank');
  link.setAttribute('class', 'ipc-metadata-list-item__list-content-item--link');
  link.setAttribute('role', 'button');

  link.setAttribute('href', url);
  link.textContent = name;

  let listItem = li.cloneNode(true);
  listItem.appendChild(link);
  return listItem;
}