SB100 / IMDb Torrent Links

// ==UserScript==
// @namespace    https://openuserjs.org/users/SB100
// @name         IMDb Torrent Links
// @description  Show links to torrent sites on IMDb film pages
// @updateURL    https://openuserjs.org/meta/SB100/IMDb_Torrent_Links.meta.js
// @version      1.0.1
// @author       SB100
// @copyright    2021, SB100 (https://openuserjs.org/users/SB100)
// @license      MIT
// @include      https://www.imdb.com/title/tt*
// ==/UserScript==

// ==OpenUserJS==
// @author SB100
// ==/OpenUserJS==

/* jshint esversion: 6 */

/**
 * =============================
 * ADVANCED OPTIONS
 * =============================
 */

const CONFIG = [{
    name: 'PTP',
    icon: 'https://passthepopcorn.me/favicon.ico',
    basePath: 'https://passthepopcorn.me/torrents.php?searchstr=',
  },
  {
    name: 'BTN',
    icon: 'https://broadcasthe.net/favicon.ico',
    basePath: 'https://broadcasthe.net/torrents.php?searchstr=',
  },
  {
    name: 'HDB',
    icon: 'https://hdbits.org/pic/favicon/favicon.ico',
    basePath: 'https://hdbits.org/browse.php?search=',
  },
  {
    name: 'BHD',
    icon: 'https://beyond-hd.me/favicon.ico',
    basePath: 'https://beyond-hd.me/torrents/all?search=&categories%5B%5D=1&imdb=',
  },
  {
    name: 'TL',
    icon: 'https://www.torrentleech.org/favicon.ico',
    basePath: 'https://www.torrentleech.org/torrents/browse/index/imdbID/',
  },
  {
    name: 'YouTube',
    icon: 'https://www.youtube.com/s/desktop/b4335f76/img/favicon_32.png',
    basePath: 'https://www.youtube.com/results?search_query=',
    extra: {
      useMovieNameInstead: true,
      append: ' trailer'
    }
  }
]

/**
 * =============================
 * END ADVANCED OPTIONS
 * DO NOT MODIFY BELOW THIS LINE
 * =============================
 */

/**
 * Find the IMDb ID from the URL
 */
function getTTId() {
  const idMatch = window.location.href.match(/tt[\d]+/);
  return idMatch && idMatch[0];
}

/**
 * Find the movie name from the header
 */
function getMovieName() {
  const name = document.querySelector('[data-testid="hero-title-block__title"]');
  if (!name) {
    return '';
  }

  return name.innerText;
}

/**
 * Build a list of icons to be placed into the navigation bar
 */
function buildIcons(imdbId, movieName) {
  const fragment = document.createDocumentFragment();

  CONFIG.forEach(c => {
    const img = document.createElement('img');
    img.src = c.icon;
    img.title = c.name;
    img.classList.add('t-link__img');

    const searchString = `${c.extra && c.extra.useMovieNameInstead ? movieName : imdbId}${c.extra && c.extra.append ? c.extra.append : ''}`;
    const a = document.createElement('a');
    a.href = `${c.basePath}${encodeURIComponent(searchString)}`
    a.target = '_blank';
    a.rel = 'noopener noreferrer';
    a.classList.add('t-link');

    a.appendChild(img);
    fragment.appendChild(a);
  });

  return fragment;
}

/**
 * Add the built list of icons to the navigation bar
 */
function addIconsToSite(icons) {
  const nav = document.querySelector('[data-testid="hero-subnav-bar-topic-links"]');
  if (!nav) {
    return;
  }

  const li = document.createElement('li');
  li.classList.add('ipc-inline-list__item');
  li.appendChild(icons);

  nav.appendChild(li);
}

/**
 * Create a style tag to add into the document head, and add some styles to it
 */
function createCss() {
  const css = `.t-link {
    padding: 0 4px;
    position: relative;
    top: 2px;
}

.t-link__img {
    max-width: 16px;
    max-height: 16px;
}`;

  const style = document.createElement('style');
  style.type = 'text/css';
  style.appendChild(document.createTextNode(css));

  document.head.appendChild(style);
}

// run the script
(function () {
  'use strict';

  createCss();
  const imdbId = getTTId();
  const movieName = getMovieName();
  const icons = buildIcons(imdbId, movieName);
  addIconsToSite(icons);
})();