Plex Random Movie Picker

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

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

  1. // ==UserScript==
  2. // @name Plex Random Movie Picker
  3. // @namespace https://greasyfork.org/en/users/247131
  4. // @author ALi3naTEd0
  5. // @version 1.4
  6. // @license MIT
  7. // @description Picks a random movie from the Plex library on localhost and displays additional information
  8. // @match http://localhost/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Botón para selección aleatoria
  16. const button = document.createElement("button");
  17. button.innerText = "Pick Random Movie";
  18. button.style.position = "fixed";
  19. button.style.top = "20px";
  20. button.style.left = "50%";
  21. button.style.transform = "translateX(-50%)";
  22. button.style.padding = "10px 20px";
  23. button.style.fontSize = "16px";
  24. button.style.zIndex = 1000;
  25. button.onclick = fetchRandomMovie;
  26. document.body.appendChild(button);
  27.  
  28. async function fetchRandomMovie() {
  29. try {
  30. const token = "Kbx3LfZzfsM9XkRzPqk9";
  31. const url = "http://localhost:32400/library/sections/1/all?X-Plex-Token=" + token;
  32.  
  33. const response = await fetch(url);
  34. const data = await response.text();
  35. const parser = new DOMParser();
  36. const xmlDoc = parser.parseFromString(data, "text/xml");
  37.  
  38. const movies = xmlDoc.getElementsByTagName("Video");
  39. if (movies.length === 0) {
  40. alert("No movies found.");
  41. return;
  42. }
  43.  
  44. // Selección aleatoria
  45. const randomIndex = Math.floor(Math.random() * movies.length);
  46. const randomMovie = movies[randomIndex];
  47.  
  48. const title = randomMovie.getAttribute("title");
  49. const year = randomMovie.getAttribute("year");
  50. const durationMs = randomMovie.getAttribute("duration");
  51. const duration = durationMs ? Math.round(durationMs / 60000) + " mins" : "Unknown";
  52. const imdbSearchUrl = `https://www.imdb.com/find?q=${encodeURIComponent(title + " " + year)}&s=tt`;
  53.  
  54. // Crear diálogo
  55. const dialog = document.createElement("div");
  56. dialog.style.position = "fixed";
  57. dialog.style.top = "50%";
  58. dialog.style.left = "50%";
  59. dialog.style.transform = "translate(-50%, -50%)";
  60. dialog.style.backgroundColor = "#fff";
  61. dialog.style.border = "2px solid #ccc";
  62. dialog.style.padding = "20px";
  63. dialog.style.zIndex = 1001;
  64. dialog.style.boxShadow = "0 4px 8px rgba(0,0,0,0.2)";
  65. dialog.style.width = "300px";
  66. dialog.style.textAlign = "center";
  67.  
  68. const message = document.createElement("p");
  69. message.innerText = `Title: ${title}\nYear: ${year}\nDuration: ${duration}`;
  70. dialog.appendChild(message);
  71.  
  72. const imdbLink = document.createElement("a");
  73. imdbLink.href = imdbSearchUrl;
  74. imdbLink.innerText = "View on IMDb";
  75. imdbLink.target = "_blank";
  76. imdbLink.style.display = "block";
  77. imdbLink.style.marginTop = "10px";
  78. dialog.appendChild(imdbLink);
  79.  
  80. // Contenedor para centrar los botones
  81. const buttonContainer = document.createElement("div");
  82. buttonContainer.style.display = "flex";
  83. buttonContainer.style.flexDirection = "column";
  84. buttonContainer.style.alignItems = "center";
  85. buttonContainer.style.marginTop = "15px";
  86.  
  87. // Botón Next
  88. const nextButton = document.createElement("button");
  89. nextButton.innerText = "Next";
  90. nextButton.onclick = async () => {
  91. dialog.remove();
  92. await fetchRandomMovie();
  93. };
  94. nextButton.style.marginBottom = "10px"; // Espacio entre botones
  95. nextButton.style.padding = "5px 15px";
  96. buttonContainer.appendChild(nextButton);
  97.  
  98. // Botón Close
  99. const closeButton = document.createElement("button");
  100. closeButton.innerText = "Close";
  101. closeButton.onclick = () => {
  102. document.body.removeChild(dialog);
  103. };
  104. closeButton.style.padding = "5px 15px";
  105. buttonContainer.appendChild(closeButton);
  106.  
  107. dialog.appendChild(buttonContainer);
  108. document.body.appendChild(dialog);
  109.  
  110. } catch (error) {
  111. console.error("Error fetching movie:", error);
  112. alert("Error fetching movie.");
  113. }
  114. }
  115.  
  116. })();