Plex Random Movie Picker

Picks a random movie from the Plex library on localhost and displays additional information

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Plex Random Movie Picker
// @namespace    https://greasyfork.org/en/users/247131
// @author       ALi3naTEd0
// @version      1.5
// @license      MIT
// @description  Picks a random movie from the Plex library on localhost and displays additional information
// @match        http://localhost/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Create the main button for random movie selection
    const button = document.createElement("button");
    button.innerText = "Pick Random Movie";
    button.style.position = "fixed";
    button.style.top = "20px"; // Space from the top
    button.style.left = "50%";
    button.style.transform = "translateX(-50%)";
    button.style.padding = "10px 20px";
    button.style.fontSize = "16px";
    button.style.zIndex = 1000;
    button.onclick = fetchRandomMovie;
    document.body.appendChild(button);

    // Function to fetch a random movie from the Plex library
    async function fetchRandomMovie() {
        try {
            // Replace with your Plex token and server details
            const token = "Kbx3LfZzfsM9XkRzPqk9";
            const url = "http://localhost:32400/library/sections/1/all?X-Plex-Token=" + token;

            const response = await fetch(url);
            const data = await response.text();
            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(data, "text/xml");

            // Get all movies in the library
            const movies = xmlDoc.getElementsByTagName("Video");
            if (movies.length === 0) {
                alert("No movies found.");
                return;
            }

            // Select a random movie
            const randomIndex = Math.floor(Math.random() * movies.length);
            const randomMovie = movies[randomIndex];

            // Retrieve movie details
            const title = randomMovie.getAttribute("title");
            const year = randomMovie.getAttribute("year");
            const durationMs = randomMovie.getAttribute("duration");
            const duration = durationMs ? Math.round(durationMs / 60000) + " mins" : "Unknown";
            const movieKey = randomMovie.getAttribute("ratingKey");
            const thumbPath = randomMovie.getAttribute("thumb");

            // Construct Plex movie URL on localhost
            const plexUrl = `http://localhost:32400/web/index.html#!/server/0f12cd3b912ff67f98de19cb45b4459d8e55222a/details?key=%2Flibrary%2Fmetadata%2F${movieKey}`;

            // Construct IMDb search URL
            const imdbSearchUrl = `https://www.imdb.com/find?q=${encodeURIComponent(title + " " + year)}&s=tt`;

            // If dialog exists, remove it to refresh content
            const existingDialog = document.getElementById("movieDialog");
            if (existingDialog) existingDialog.remove();

            // Create custom dialog to display movie details
            const dialog = document.createElement("div");
            dialog.id = "movieDialog";
            dialog.style.position = "fixed";
            dialog.style.top = "50%";
            dialog.style.left = "50%";
            dialog.style.transform = "translate(-50%, -50%)";
            dialog.style.backgroundColor = "#fff";
            dialog.style.border = "2px solid #ccc";
            dialog.style.padding = "20px";
            dialog.style.zIndex = 1001;
            dialog.style.boxShadow = "0 4px 8px rgba(0,0,0,0.2)";
            dialog.style.width = "300px";
            dialog.style.textAlign = "center";

            // Add movie thumbnail if available
            if (thumbPath) {
                const thumbnail = document.createElement("img");
                thumbnail.src = `http://localhost:32400${thumbPath}?X-Plex-Token=${token}`;
                thumbnail.style.width = "100%";
                thumbnail.style.borderRadius = "5px";
                thumbnail.style.marginBottom = "10px";
                dialog.appendChild(thumbnail);
            }

            // Add movie details
            const message = document.createElement("p");
            message.innerText = `Title: ${title}\nYear: ${year}\nDuration: ${duration}`;
            dialog.appendChild(message);

            // Add link to open movie in current tab
            const goButton = document.createElement("a");
            goButton.href = plexUrl; // URL of the movie in localhost
            goButton.innerText = "Go to Movie";
            goButton.style.display = "block";
            goButton.style.marginTop = "15px";
            goButton.style.textDecoration = "none";
            goButton.style.color = "#007bff";
            goButton.onclick = () => {
                window.location.href = plexUrl; // Opens the URL in the same tab
            };
            dialog.appendChild(goButton);

            // Add IMDb link in yellow Plex color
            const imdbLink = document.createElement("a");
            imdbLink.href = imdbSearchUrl;
            imdbLink.innerText = "View on IMDb";
            imdbLink.target = "_blank"; // Open IMDb in a new tab
            imdbLink.style.display = "block";
            imdbLink.style.marginTop = "10px";
            imdbLink.style.marginBottom = "15px"; // Add space before the buttons
            imdbLink.style.textDecoration = "none";
            imdbLink.style.color = "#e5a00d"; // Yellow color for IMDb link
            dialog.appendChild(imdbLink);

            // Add 'Next' button for another random selection
            const nextButton = document.createElement("button");
            nextButton.innerText = "Next";
            nextButton.onclick = fetchRandomMovie; // Calls the function again for a new random movie
            nextButton.style.marginTop = "10px";
            nextButton.style.marginRight = "10px";
            dialog.appendChild(nextButton);

            // Add 'Close' button to close the dialog
            const closeButton = document.createElement("button");
            closeButton.innerText = "Close";
            closeButton.onclick = () => {
                document.body.removeChild(dialog);
            };
            closeButton.style.marginTop = "10px";
            closeButton.style.marginLeft = "10px";
            dialog.appendChild(closeButton);

            // Add the dialog to the document
            document.body.appendChild(dialog);

        } catch (error) {
            console.error("Error fetching movie:", error);
            alert("Error fetching movie.");
        }
    }
})();