yoharnu / PTP Poster Replacer (TMDb)

// ==UserScript==
// @name         PTP Poster Replacer (TMDb)
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  Change movie page poasters to TMDb posters
// @author       yoharnu
// @downloadurl  https://openuserjs.org/scripts/yoharnu/PTP_Poster_Replacer_(TMDb)
// @match        http*://*.passthepopcorn.me/torrents.php*
// @icon         https://www.google.com/s2/favicons?domain=passthepopcorn.me
// @license      MIT
// @grant        GM_xmlhttpRequest
// @connect      themoviedb.org
// ==/UserScript==

const movies = document.getElementsByClassName('cover-movie-list__movie');

(function () {
  'use strict';

  if (window.location.search.includes('?id=')) {
    let imdbID = document.getElementById('imdb-title-link');
    if (imdbID) {
      imdbID = imdbID.href.split('/title/tt')[1].split('/')[0];
      getTMDBposter(imdbID, 'l', posterURL => {
        let poster = document.querySelector('img.sidebar-cover-image');
        if (poster) {
          poster.src = posterURL;
        }
      });
    }
  }
  else {
    for (let i = 0; i < movies.length; i++) {
      let rating = movies[i].querySelector('div.cover-movie-list__movie__undercover div.cover-movie-list__movie__rating-and-tags a.cover-movie-list__movie__rating');
      if (rating) {
        let imdb_id = rating.href.split('/title/tt')[1].split('/')[0];
        if (imdb_id) {
          setPosterTile(imdb_id, i);
        }
      }
    }
  }
})();

function setPosterTile(imdb_id, number) {
  getTMDBposter(imdb_id, 's', posterURL => {
    let poster = movies[number].getElementsByClassName('cover-movie-list__movie__cover-link')[0];
    poster.style.backgroundImage = "url('" + posterURL + "')";
    poster.style.backgroundRepeat = 'no-repeat';
    poster.style.backgroundSize = '176px 246px';
  });
}

// imdb_id does not include leading "tt"
// img_size options = ['s', 'm', 'l']
function getTMDBposter(imdb_id, img_size = 'm', callback) {
  let cache = sessionStorage.getItem('poster_' + img_size + '_' + imdb_id);
  if (cache) {
    if (img_size === 's') {
      cache = cache.replace('w300_and_h450', 'w150_and_h225');
      cache = cache.replace('w600_and_h900', 'w150_and_h225');
    }
    else if (img_size === 'm') {
      cache = cache.replace('w150_and_h225', 'w300_and_h450');
      cache = cache.replace('w600_and_h900', 'w300_and_h450');
    }
    else if (img_size === 'l') {
      cache = cache.replace('w150_and_h225', 'w600_and_h900');
      cache = cache.replace('w300_and_h450', 'w600_and_h900');
    }

    callback(cache);
    return;
  }

  GM_xmlhttpRequest({
    method: 'GET',
    url: 'https://www.themoviedb.org/redirect?external_source=imdb_id&external_id=tt' + imdb_id,
    onload: function (xhr) {
      if (xhr.readyState === 4 && xhr.status === 200) {
        let newDoc = document.implementation.createHTMLDocument();
        newDoc.body.innerHTML = xhr.responseText;

        let poster = newDoc.querySelector('img.poster');
        if (poster) {
          let posterURL = 'https://www.themoviedb.org' + poster.src.split('passthepopcorn.me')[1].replace('_filter(blur)', '');

          if (img_size === 's') {
            posterURL = posterURL.replace('w300_and_h450', 'w150_and_h225');
          }
          else if (img_size === 'l') {
            posterURL = posterURL.replace('w300_and_h450', 'w600_and_h900');
          }

          sessionStorage.setItem('poster_' + imdb_id, posterURL);
          callback(posterURL);
        }
      }
    }
  });
}