nickpapoutsis / IMDB to RARBG Torrent Search

// ==UserScript==
// @name             IMDB to RARBG Torrent Search
// @description      When on IMDB, click the small buttons to search for the movie/series on TorrentGalaxy, 1337x or RARBG Index.
// @namespace        nickpapoutsis
// @author           nickpapoutsis
// @copyright        2023, nickpapoutsis (https://openuserjs.org/users/nickpapoutsis)
// @contributionURL  https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A6EL76QADA2YY&source=url
// @license          MIT
// @match            https://www.imdb.com/*
// @version          0.7
// @grant            none
// @icon             data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALwAAABGCAMAAABBng7mAAAA3lBMVEUAAAD+/v////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////84YLv///9TdcSiteDc4/NPc8O5x+iWq9tbfMesveN6ldJohsz09vv+/v9wjc9JbsHs8Pnj6fY9ZL2wwOSAmtTL1u5Dab9hgcm/zOnX3/LG0uyPpdmIoNZ2ktHQ2u+csN5Xeca1xObl2PI1AAAAKHRSTlMA/N5PhvHq98DXx+QqHbtAM9AZrY9uFHVjRw0D2qaXswhdIlc6oH6o47OqVgAAB9tJREFUaN7tmmlz2jAQhm3A3OWGcJMAoRgI92UgmDOB//+Hysq2JEsWdZNOZzrD+6Et9lr7eLVaSValhx566KGHHnrooYf+lUrF1I9U7vl3ZsVowUurEG9GcyVn0wQ2ij4zN5g2EvViiXnWrZfneiHry1RqsVq66mvlmzkxerwaCsiUAooSSaYzT4UfrGmu8eqxrJRk1YvxU3m4YWvD7wlXMtk49vsjCyaMl5qDl1I9nwkqKqVIuoWtGMU9qrOUWDZqZ/cFbAb+F4u9Kgva8Fe8KYO94tJLKhub3beijdOqWOECbdoAdloRs8G8rAoVSCfAJHvPS5zq29pMYJV0wPcyRAzeS5FkYpW7/Wp0dEa9p1ABPyvy4i2ZuZWhaKZaT9OW9Ety9E9wedS1aX09aLIZuCec2NHk7beMjQ4QDeQ0B523tDcxWvc/tJnJVjCe7dmdrK/vPdO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A9asTb0VC1mPlmNVpsFyKBROO/2PglI0E46QZFY84WqhJP1HKjafXmuxWKxWzUf/K/CHHnrooYcekn4BP1SLjzAI1FoAAAAASUVORK5CYII=
// ==/UserScript==

(function () {
  'use strict';

  console.log('So long, and thanks for all the fish RARBG team. o7');

  function getMovieYear() {
    let scripts = document.querySelectorAll('script[type="application/ld+json"]');
    for (let script of scripts) {
      try {
        let jsonContent = JSON.parse(script.innerHTML);
        if (jsonContent && jsonContent.datePublished) {
          return jsonContent.datePublished.split('-')[0];
        }
      }
      catch (e) {
        console.error('Error parsing JSON LD script:', e);
      }
    }
    return null;
  }

  function getMovieName() {
    let scripts = document.querySelectorAll('script[type="application/ld+json"]');
    for (let script of scripts) {
      try {
        let jsonContent = JSON.parse(script.innerHTML);
        if (jsonContent && jsonContent.name) {
          return jsonContent.name;
        }
      }
      catch (e) {
        console.error('Error parsing JSON LD script:', e);
      }
    }
    return null;
  }

  function createSearchableMovieName(movieName, movieYear) {
    if (!movieName) return null;
    let name = movieName.replace(/[^\w\s]/g, '');
    if (movieYear) name += ' ' + movieYear;
    return name.replace(/\s+/g, '+');
  }

  function getMovieId() {
    let x = window.location.pathname.toLowerCase();
    let arr = x.split('/');
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].substring(0, 2) === 'tt') {
        return arr[i];
      }
    }
    return null;
  }

  function createServiceButton(serviceName, searchUrl, urlParams, identifier, container) {
    let a = document.createElement('a');
    a.href = `${searchUrl}${identifier}${urlParams}`;
    a.target = '_blank';
    a.style.background = '#ffffff';
    a.style.color = 'blue';
    a.style.display = 'block';
    a.style.margin = '6px';
    a.style.padding = '4px 6px';
    a.style.border = '1px solid deepskyblue';
    a.style.borderRadius = '4px';
    a.style.textDecoration = 'none';
    a.style.fontWeight = '500';
    a.style.fontSize = '0.75em';
    a.style.textAlign = 'center';
    a.style.transition = 'all 0.2s ease';
    a.innerText = serviceName;

    a.onmouseover = function () {
      this.style.background = 'deepskyblue';
      this.style.color = '#ffffff';
      this.style.border = '1px solid deepskyblue';
    };

    a.onmouseout = function () {
      this.style.background = '#ffffff';
      this.style.color = 'blue';
      this.style.border = '1px solid deepskyblue';
    };

    container.appendChild(a);
  }

  var movieId = getMovieId();
  var movieName = getMovieName();
  var movieYear = getMovieYear();
  var searchableMovieName = createSearchableMovieName(movieName, movieYear);

  if (movieId || searchableMovieName) {
    let services = [
      // { name: "RARBG", url: "https://rarbgprx.org/torrents.php?imdb=", urlParams: "&order=size&by=ASC", use: 'id' },
      {
        name: "TorrentGalaxy",
        url: "https://torrentgalaxy.to/torrents.php?search=",
        urlParams: "&lang=1&nox=2#results",
        use: 'id'
      },
      {
        name: "1337x",
        url: "https://1337x.to/search/",
        urlParams: "/1/",
        use: 'name'
      },
      {
        name: "RARBG Index",
        url: "https://rarbg.best/search?q=",
        urlParams: "",
        use: 'name'
      }
    ];

    let container = document.createElement('div');
    container.style.position = "fixed";
    container.style.left = "1%";
    container.style.top = "8%";
    container.style.zIndex = '9999';

    document.body.append(container);

    services.forEach(service => {
      let identifier = service.use === 'id' ? movieId : searchableMovieName;
      if (identifier) {
        createServiceButton(service.name, service.url, service.urlParams, identifier, container);
      }
    });
  }

})();