Plex Random Movie Picker

Picks a random movie from the Plex library on localhost

La data de 30-10-2024. Vezi ultima versiune.

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

(function() {
    'use strict';

    // Create button for new random 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);

    async function fetchRandomMovie() {
        try {
            // Replace with your Plex token
            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
            const movies = xmlDoc.getElementsByTagName("Video");
            if (movies.length === 0) {
                alert("No movies found.");
                return;
            }

            // Pick a random movie
            const randomIndex = Math.floor(Math.random() * movies.length);
            const randomMovie = movies[randomIndex];
            const title = randomMovie.getAttribute("title");

            // Display selected movie
            alert("Selected movie: " + title);

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

})();