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.3
  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. // Create button for new random selection
  16. const button = document.createElement("button");
  17. button.innerText = "Pick Random Movie";
  18. button.style.position = "fixed";
  19. button.style.top = "20px"; // Space from the top
  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. // Replace with your Plex token
  31. const token = "Kbx3LfZzfsM9XkRzPqk9";
  32. const url = "http://localhost:32400/library/sections/1/all?X-Plex-Token=" + token;
  33.  
  34. const response = await fetch(url);
  35. const data = await response.text();
  36. const parser = new DOMParser();
  37. const xmlDoc = parser.parseFromString(data, "text/xml");
  38.  
  39. // Get all movies
  40. const movies = xmlDoc.getElementsByTagName("Video");
  41. if (movies.length === 0) {
  42. alert("No movies found.");
  43. return;
  44. }
  45.  
  46. // Pick a random movie
  47. const randomIndex = Math.floor(Math.random() * movies.length);
  48. const randomMovie = movies[randomIndex];
  49.  
  50. // Retrieve movie details
  51. const title = randomMovie.getAttribute("title");
  52. const year = randomMovie.getAttribute("year");
  53. const durationMs = randomMovie.getAttribute("duration");
  54. const duration = durationMs ? Math.round(durationMs / 60000) + " mins" : "Unknown";
  55.  
  56. // Construct IMDb search URL
  57. const imdbSearchUrl = `https://www.imdb.com/find?q=${encodeURIComponent(title + " " + year)}&s=tt`;
  58.  
  59. // Create custom dialog to display movie details
  60. const dialog = document.createElement("div");
  61. dialog.style.position = "fixed";
  62. dialog.style.top = "50%";
  63. dialog.style.left = "50%";
  64. dialog.style.transform = "translate(-50%, -50%)";
  65. dialog.style.backgroundColor = "#fff";
  66. dialog.style.border = "2px solid #ccc";
  67. dialog.style.padding = "20px";
  68. dialog.style.zIndex = 1001;
  69. dialog.style.boxShadow = "0 4px 8px rgba(0,0,0,0.2)";
  70. dialog.style.width = "300px";
  71. dialog.style.textAlign = "center";
  72.  
  73. const message = document.createElement("p");
  74. message.innerText = `Title: ${title}\nYear: ${year}\nDuration: ${duration}`;
  75. dialog.appendChild(message);
  76.  
  77. const imdbLink = document.createElement("a");
  78. imdbLink.href = imdbSearchUrl;
  79. imdbLink.innerText = "View on IMDb";
  80. imdbLink.target = "_blank"; // Open in a new tab
  81. imdbLink.style.display = "block";
  82. imdbLink.style.marginTop = "10px";
  83.  
  84. dialog.appendChild(imdbLink);
  85.  
  86. // Create a close button
  87. const closeButton = document.createElement("button");
  88. closeButton.innerText = "Close";
  89. closeButton.onclick = () => {
  90. document.body.removeChild(dialog);
  91. };
  92. closeButton.style.marginTop = "10px";
  93. dialog.appendChild(closeButton);
  94.  
  95. document.body.appendChild(dialog);
  96.  
  97. } catch (error) {
  98. console.error("Error fetching movie:", error);
  99. alert("Error fetching movie.");
  100. }
  101. }
  102.  
  103. })();