Skip to content

Commit

Permalink
Allow fetching covers in edit page
Browse files Browse the repository at this point in the history
  • Loading branch information
Morea committed Oct 31, 2023
1 parent b9c6911 commit f4c6b77
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions PTP - Add IMDB Cover/PTP - Add IMDB Cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// @match https://passthepopcorn.me/upload.php?*
// @match https://passthepopcorn.me/upload.php
// @match https://passthepopcorn.me/requests.php*
// @match https://passthepopcorn.me/torrents.php*action=editgroup*
// @icon https://www.google.com/s2/favicons?domain=passthepopcorn.me
// @grant GM_xmlhttpRequest
// @grant GM.getValue
Expand All @@ -15,6 +16,9 @@
await (async function () {
"use strict";

const isEditPage = () => {
return location.toString().includes("action=editgroup");
};
const addListener = () => {
if (window.location.href.includes("requests.php")) {
document.querySelectorAll("#request_form > div").forEach((d) => {
Expand Down Expand Up @@ -294,10 +298,23 @@ await (async function () {
};

let coverInput = document.getElementById("image");
document.querySelector("#autofill").addEventListener("click", async () => {
let triggerButton = document.querySelector("#autofill");
if (isEditPage()) {
coverInput = document.getElementsByName("image")[0];
triggerButton = document.createElement("button");
triggerButton.textContent = "Fetch";
coverInput.parentElement.appendChild(triggerButton);
}
triggerButton.addEventListener("click", async (event) => {
try {
let apiToken = await getPtpImgApiKey();
let imdbId = document.querySelector("#imdb").value;
let imdbId;
if (isEditPage()) {
event.preventDefault();
imdbId = document.getElementsByName("newimdb")[0].value;
} else {
imdbId = document.querySelector("#imdb").value;
}
if (imdbId.startsWith("http"))
imdbId = "tt" + imdbId.split("/tt")[1].split("/")[0];
let url = await fetchCover(imdbId);
Expand All @@ -308,7 +325,7 @@ await (async function () {
coverInput.value = await uploadToPtpimg(url, apiToken);
coverInput.dispatchEvent(new Event("change"));
} catch (err) {
console.trace(err)
console.trace(err);
}
});
coverInput.addEventListener("change", async (event) => {
Expand Down

0 comments on commit f4c6b77

Please sign in to comment.