Add IMDB Link to Plex

Add an IMDB Link to Plex

目前为 2020-12-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add IMDB Link to Plex
  3. // @description Add an IMDB Link to Plex
  4. // @match https://app.plex.tv/desktop*
  5. // @grant GM_xmlhttpRequest
  6. // @version 0.0.1.20201218201500
  7. // @namespace https://greasyfork.org/users/718390
  8. // ==/UserScript==
  9.  
  10. 'use strict';
  11.  
  12. //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
  13. var timerHandle = setInterval(main, 1000);
  14.  
  15. function main() {
  16.  
  17. //"metadata" string in url means we're on an actual movie page in plex, otherwise just keep looping on timer
  18. if (window.location.href.indexOf("metadata") === -1) return;
  19.  
  20. //also keep looping till we see the title element get rendered
  21. var title = document.querySelector("div[data-qa-id='preplay-mainTitle']");
  22. var year = document.querySelector("div[data-qa-id='preplay-secondTitle']").textContent;
  23. if (!title) return;
  24.  
  25. //now that we've found a title we can do our bizness...
  26. //but only if we haven't already =)
  27. if (document.getElementById("imdbhack")) return;
  28.  
  29. //create new element to show imdb link (or error message)
  30. var imdbhack = document.createElement("a");
  31. imdbhack.id = "imdbhack";
  32. title.insertAdjacentElement('afterend', imdbhack);
  33.  
  34. //stack-o showed this oddball imdb "api" we can use to look up the magic imdb "id" for a movie
  35. //https://stackoverflow.com/questions/1966503/does-imdb-provide-an-api/7744369#7744369
  36. GM_xmlhttpRequest({
  37. method: "GET",
  38. url: "https://sg.media-imdb.com/suggests/" + title.textContent[0].toLowerCase() + "/" + encodeURI(title.textContent + " " + year) + ".json",
  39. onload: function (response) {
  40. if (response.responseText.indexOf("Bad query") > -1) {
  41. imdbhack.textContent = "not found on imdb";
  42. }
  43. else {
  44. var getId = /\"id\":\"(.*?)\"/;
  45. var match = response.responseText.match(getId); //match[1] will contain the id for the win!
  46.  
  47. //finally we get to jam in our href to the corresponding imdb movie deets page!
  48. //nugget: cool part here is we can target the anchor id for the user reviews!! if that's not your main interest, just remove
  49. imdbhack.href = "https://www.imdb.com/title/" + match[1] + "/#titleUserReviewsTeaser";
  50. imdbhack.textContent = "imdb link";
  51. imdbhack.target = "_blank";
  52. };
  53. }
  54. });
  55. }