Add IMDB Link to Plex

Add an IMDB Link to Plex

目前為 2020-12-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name  Add IMDB Link to Plex
// @description Add an IMDB Link to Plex
// @match https://app.plex.tv/desktop*
// @grant GM_xmlhttpRequest
// @version 0.0.1.20201218201500
// @namespace https://greasyfork.org/users/718390
// ==/UserScript==

'use strict';

//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
var timerHandle = setInterval(main, 1000);

function main() {

    //"metadata" string in url means we're on an actual movie page in plex, otherwise just keep looping on timer
    if (window.location.href.indexOf("metadata") === -1) return;

    //also keep looping till we see the title element get rendered
    var title = document.querySelector("div[data-qa-id='preplay-mainTitle']");
    var year = document.querySelector("div[data-qa-id='preplay-secondTitle']").textContent;
    if (!title) return;

    //now that we've found a title we can do our bizness...
    //but only if we haven't already =)
    if (document.getElementById("imdbhack")) return;

    //create new element to show imdb link (or error message)
    var imdbhack = document.createElement("a");
    imdbhack.id = "imdbhack";
    title.insertAdjacentElement('afterend', imdbhack);

    //stack-o showed this oddball imdb "api" we can use to look up the magic imdb "id" for a movie
    //https://stackoverflow.com/questions/1966503/does-imdb-provide-an-api/7744369#7744369
    GM_xmlhttpRequest({
        method: "GET",
        url: "https://sg.media-imdb.com/suggests/" + title.textContent[0].toLowerCase() + "/" + encodeURI(title.textContent + " " + year) + ".json",
        onload: function (response) {
            if (response.responseText.indexOf("Bad query") > -1) {
                imdbhack.textContent = "not found on imdb";
            }
            else {
                var getId = /\"id\":\"(.*?)\"/;
                var match = response.responseText.match(getId); //match[1] will contain the id for the win!

                //finally we get to jam in our href to the corresponding imdb movie deets page!
                //nugget: cool part here is we can target the anchor id for the user reviews!! if that's not your main interest, just remove
                imdbhack.href = "https://www.imdb.com/title/" + match[1] + "/#titleUserReviewsTeaser";
                imdbhack.textContent = "imdb link";
                imdbhack.target = "_blank";
            };
        }
    });
}