Plex- Add IMDB Link

Add an IMDB Link to Plex

  1. // ==UserScript==
  2. // @name Plex- Add IMDB Link
  3. // @description Add an IMDB Link to Plex
  4. // @match http://virgulestar.noip.me:32400/*
  5. // @grant GM_xmlhttpRequest
  6. // @version 0.0.2.20220211
  7. // @namespace https://greasyfork.org/users/718390
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. 'use strict';
  12.  
  13. //since plex is a spa (single page app), we have to perpetually monitor the page elements that get rendered, we can't count on a traditional full browser page refresh
  14. var timerHandle = setInterval(main, 1000);
  15.  
  16. function main() {
  17.  
  18. //"metadata" string in url means we're on an actual movie page in plex, otherwise just keep looping on timer
  19. if (window.location.href.indexOf("metadata") === -1) return;
  20.  
  21. //also keep looping till we see the title element get rendered
  22. var titleElement = document.querySelector("div[data-testid='preplay-mainTitle']");
  23. if (!titleElement) return;
  24. var title = titleElement.textContent;
  25. var year = document.querySelector("div[data-testid='preplay-secondTitle']").textContent;
  26.  
  27. //now that we've found a title we can do our bizness...
  28. //but only if we haven't already =)
  29. if (document.getElementById("imdbLink")) {
  30. clearInterval(timerHandle);
  31. return;
  32. }
  33.  
  34. var imdbLinkQueryUrl = "https://v2.sg.media-imdb.com/suggestion/" + title[0].toLowerCase() + "/" + encodeURI(title + " (" + year +")") + ".json";
  35. console.log("imdb query:" + imdbLinkQueryUrl);
  36.  
  37. //stack-o showed this oddball imdb "api" we can use to look up the magic imdb "id" for a movie
  38. //https://stackoverflow.com/questions/1966503/does-imdb-provide-an-api/7744369#7744369
  39. GM_xmlhttpRequest({
  40. method: "GET",
  41. url: imdbLinkQueryUrl,
  42. onload: function (response) {
  43.  
  44. //wrapper existing imdb rating with a link to the imdb url for the movie
  45. var imdbLink = document.createElement("a");
  46. imdbLink.id = "imdbLink";
  47. var imdbRatingElement = document.querySelector("div[title^='IMDb Rating']");
  48. imdbRatingElement.parentNode.insertBefore(imdbLink, imdbRatingElement);
  49. imdbLink.appendChild(imdbRatingElement);
  50.  
  51. if (response.responseText.indexOf("Bad query") > -1) {
  52. imdbLink.appendChild("not found on imdb");
  53. }
  54. else {
  55. var getId = /\"id\":\"(.*?)\"/;
  56. var match = response.responseText.match(getId); //match[1] will contain the id for the win!
  57.  
  58. //finally we get to jam in our href to the corresponding imdb movie deets page!
  59. //nugget: cool part here is we can target the anchor id for the user reviews!! if that's not your main interest, just remove
  60. imdbLink.href = "https://www.imdb.com/title/" + match[1] + "/#titleUserReviewsTeaser";
  61. imdbLink.target = "_blank";
  62. };
  63. }
  64. });
  65. }