Add IMDB Link to Plex

Add an IMDB Link to Plex

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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";
            };
        }
    });
}