Plex Random Movie Picker

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

当前为 2024-10-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Plex Random Movie Picker
// @namespace    https://greasyfork.org/en/users/247131
// @author       ALi3naTEd0
// @version      1.4
// @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';

    // Botón para selección aleatoria
    const button = document.createElement("button");
    button.innerText = "Pick Random Movie";
    button.style.position = "fixed";
    button.style.top = "20px";
    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);

    async function fetchRandomMovie() {
        try {
            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");

            const movies = xmlDoc.getElementsByTagName("Video");
            if (movies.length === 0) {
                alert("No movies found.");
                return;
            }

            // Selección aleatoria
            const randomIndex = Math.floor(Math.random() * movies.length);
            const randomMovie = movies[randomIndex];

            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 imdbSearchUrl = `https://www.imdb.com/find?q=${encodeURIComponent(title + " " + year)}&s=tt`;

            // Crear diálogo
            const dialog = document.createElement("div");
            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";

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

            const imdbLink = document.createElement("a");
            imdbLink.href = imdbSearchUrl;
            imdbLink.innerText = "View on IMDb";
            imdbLink.target = "_blank";
            imdbLink.style.display = "block";
            imdbLink.style.marginTop = "10px";
            dialog.appendChild(imdbLink);

            // Contenedor para centrar los botones
            const buttonContainer = document.createElement("div");
            buttonContainer.style.display = "flex";
            buttonContainer.style.flexDirection = "column";
            buttonContainer.style.alignItems = "center";
            buttonContainer.style.marginTop = "15px";

            // Botón Next
            const nextButton = document.createElement("button");
            nextButton.innerText = "Next";
            nextButton.onclick = async () => {
                dialog.remove();
                await fetchRandomMovie();
            };
            nextButton.style.marginBottom = "10px"; // Espacio entre botones
            nextButton.style.padding = "5px 15px";
            buttonContainer.appendChild(nextButton);

            // Botón Close
            const closeButton = document.createElement("button");
            closeButton.innerText = "Close";
            closeButton.onclick = () => {
                document.body.removeChild(dialog);
            };
            closeButton.style.padding = "5px 15px";
            buttonContainer.appendChild(closeButton);

            dialog.appendChild(buttonContainer);
            document.body.appendChild(dialog);

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

})();