From a426fa87521aa03c8854b408a5bf31253b2027e5 Mon Sep 17 00:00:00 2001 From: Mars Niermann Date: Wed, 8 Mar 2023 00:37:33 +0100 Subject: [PATCH] Add refresh button --- jellyfin-quick-delete.user.js | 117 ++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 33 deletions(-) diff --git a/jellyfin-quick-delete.user.js b/jellyfin-quick-delete.user.js index 2c1d409..48248f1 100644 --- a/jellyfin-quick-delete.user.js +++ b/jellyfin-quick-delete.user.js @@ -11,46 +11,97 @@ (function () { "use strict"; - var username = "mars"; - var password = "absw3712mcS"; - var apiKey = "38346c399d57454da3bdbf47ba716765"; - var userid = "e55a6ca076a14cb5a6ca9cfaa75498c1"; - function insert() { - const baseURL = window.origin; - fetch(`${baseURL}/Users/${userid}/Authenticate/?pw=${password}`, { - method: "POST", - headers: { - "X-Emby-Authorization": `MediaBrowser Client="QuickDelete", Device="Chrome", DeviceId="test", Version="10.8.9", Token="${apiKey}"`, - }, - }) - .then((response) => response.json()) - .then((data) => (apiKey = data.AccessToken)); + // Define Constants + const username = "mars"; + const password = "absw3712mcS"; + const apiKey = "38346c399d57454da3bdbf47ba716765"; + const userid = "e55a6ca076a14cb5a6ca9cfaa75498c1"; + const baseURL = window.origin; + + // Authorize user + fetch(`${baseURL}/Users/${userid}/Authenticate/?pw=${password}`, { + method: "POST", + headers: { + "X-Emby-Authorization": `MediaBrowser Client="QuickDelete", Device="Browser", DeviceId="", Version="1.0.0", Token="${apiKey}"`, + }, + }) + .then((response) => response.json()) + .then((data) => (apiKey = data.AccessToken)); // Store AccessToken + + // Helper function to create delete button + function delButton() { + let el = document.createElement("button"); + el.className = "listItemButton paper-icon-button-light emby-button"; + let el2 = document.createElement("span"); + + // Set icon and color + el2.className = "material-icons delete"; + el2.style.color = "#fb4934"; + el.appendChild(el2); + + // Bind delete function + el.onclick = function () { + let url = `${window.origin}/Items/${this.parentElement.dataset.id}`; + console.log(`Deleting ${url}`); + + // Send deletion request + fetch(url, { + method: "DELETE", + headers: { + "X-Emby-Authorization": `MediaBrowser Client="QuickDelete", Device="Chrome", DeviceId="test", Version="10.8.9", Token="${apiKey}"`, + }, + }); + + // Remove parent to provide feedback and prevent double deletion + this.parentElement.remove(); + }; + //Return complete element + return el2; + } + + // Button to refresh the metadata of an item + function refreshButton() { + let el = document.createElement("button"); + el.className = "listItemButton paper-icon-button-light emby-button"; + let el2 = document.createElement("span"); + + // Set icon and color + el2.className = "material-icons refresh"; + el2.style.color = "#83a598"; + el.appendChild(el2); + + el.onclick = function () { + let url = `${window.origin}/Items/${this.parentElement.dataset.id}/Refresh?Recursive=true&ImageRefreshMode=FullRefresh&MetadataRefreshMode=FullRefresh&ReplaceAllImages=true&ReplaceAllMetadata=true`; + console.log(`Deleting ${url}`); + + // Send deletion request + fetch(url, { + method: "POST", + headers: { + "X-Emby-Authorization": `MediaBrowser Client="QuickDelete", Device="Chrome", DeviceId="test", Version="10.8.9", Token="${apiKey}"`, + }, + }); + }; + return el2; + } + + // Insert buttons + function insert() { const collection = document.getElementsByClassName("listItem"); + for (let i = 0; i < collection.length; i++) { let curr = collection[i]; - let el = document.createElement("button"); - el.className = "listItemButton paper-icon-button-light emby-button"; - let el2 = document.createElement("span"); - el2.className = "material-icons delete"; - el2.style.color = "red"; - el.appendChild(el2); - el.onclick = function () { - let url = "https://media.m3.fyi/Items/" + this.parentElement.dataset.id; - console.log(`Deleting ${url}`); - fetch(url, { - method: "DELETE", - headers: { - "X-Emby-Authorization": `MediaBrowser Client="QuickDelete", Device="Chrome", DeviceId="test", Version="10.8.9", Token="${apiKey}"`, - }, - }); - this.parentElement.remove(); - }; - curr.appendChild(el); + + // Append buttons to list + curr.appendChild(refreshButton()); + curr.appendChild(delButton()); } } + + // Setup key handler function onKeydown(evt) { - // Use https://keycode.info/ to get keys + // Alt + Q if (evt.altKey && evt.keyCode == 81) { insert(); }