From 4bc5cf22603880d3f434abbb11760e1d1b717fe8 Mon Sep 17 00:00:00 2001 From: GoldenretriverYT <49924528+GoldenretriverYT@users.noreply.github.com> Date: Sun, 9 Jun 2024 01:53:55 +0200 Subject: [PATCH] Add multiple decals upload and reload "hotkey" --- js/background.js | 11 +++++-- js/create/image.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ js/sitewide.js | 15 ++++++++- manifest.json | 5 +++ resources/utils.js | 14 +++++++- settings.html | 11 +++++++ 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 js/create/image.js diff --git a/js/background.js b/js/background.js index a8a4af4..3a2ede9 100755 --- a/js/background.js +++ b/js/background.js @@ -77,7 +77,8 @@ const DefaultSettings = { Enabled: false, Banners: true, Rectangles: true - } + }, + UploadMultipleDecals: true } chrome.runtime.onInstalled.addListener(() => { @@ -295,4 +296,10 @@ function MergeObjects(obj1, obj2) { } return mergedObj; -} \ No newline at end of file +} + +chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { + if (request.action === 'reload') { + chrome.runtime.reload(); + } +}); \ No newline at end of file diff --git a/js/create/image.js b/js/create/image.js new file mode 100644 index 0000000..899b7e2 --- /dev/null +++ b/js/create/image.js @@ -0,0 +1,80 @@ +console.log("PolyPlus: Running image.js"); + +(async () => { + var Settings = []; + chrome.storage.sync.get(["PolyPlus_Settings"], async function (result) { + let Utilities = (await import(chrome.runtime.getURL("resources/utils.js"))) + .default; + Settings = result.PolyPlus_Settings || { + UploadMultipleDecals: true, + }; + + if (Settings.UploadMultipleDecals === true) { + UploadMultipleDecals(); + } else { + console.log("PolyPlus: UploadMultipleDecals is disabled"); + } + + async function UploadMultipleDecals() { + const fileInput = document.querySelector("#file"); + fileInput.setAttribute("multiple", "true"); + + const submitBtn = document.querySelector( + "#main-content > div:nth-child(4) > div.container.p-0.p-lg-5 > div.row.mx-auto > div.col-lg-10 > div.card.mb-2 > div > form > div.form-group.mb-0 > button" + ); + submitBtn.addEventListener("click", async function (ev) { + ev.preventDefault(); + submitBtn.disabled = true; + submitBtn.textContent = + "Uploading... (0/" + fileInput.files.length + ")"; + const files = fileInput.files; + + // we cant submit multiple files to the API, so we create a request for each file + let i = 0; + + const uploadFile = async (file, index) => { + const formData = new FormData(); + formData.append("file", file); + + try { + const response = await Utilities.RatelimitRepeatingFetch( + "https://polytoria.com/create/upload-decal", + { + method: "POST", + body: formData, + } + ); + + console.log(response.status); + if (response.ok) { + i++; + submitBtn.textContent = + "Uploading... (" + i + "/" + fileInput.files.length + ")"; + } else { + throw new Error(response.status); + } + } catch (err) { + console.error(err); + submitBtn.textContent = + "File " + index + " failed to upload: " + err; + submitBtn.disabled = false; + throw err; // re-throw the error to stop further processing + } + }; + + const uploadFiles = async () => { + try { + await Promise.all(Array.from(files).map(uploadFile)); + submitBtn.textContent = "Upload"; + submitBtn.disabled = false; + window.location.reload(); + } catch (err) { + console.error("Some files failed to upload:", err); + } + }; + + uploadFiles(); + }); + } + }); +})(); diff --git a/js/sitewide.js b/js/sitewide.js index bd46c30..3b34069 100755 --- a/js/sitewide.js +++ b/js/sitewide.js @@ -1,6 +1,5 @@ var Settings; let Theme = ``; - (async () => { let Utilities = await import(chrome.runtime.getURL('resources/utils.js')); Utilities = Utilities.default; @@ -113,6 +112,20 @@ let Theme = ``; } }); + + const combination = "reload"; + let currentCombination = ""; + document.addEventListener("keypress", function(e) { + currentCombination += e.key; + if (currentCombination === combination && document.activeElement.tagName !== "INPUT") { + console.log("Reloading Poly+..."); + chrome.runtime.sendMessage({ action: "reload" }); + window.location.reload(); + } else if (!combination.startsWith(currentCombination)) { + currentCombination = ""; + } + }); + if (Settings.HideUserAds && Settings.HideUserAds.Enabled === true) { if (Settings.HideUserAds.Banners && Settings.HideUserAds.Banners === true) { Theme += ` diff --git a/manifest.json b/manifest.json index 3d9b622..7fd6b0d 100755 --- a/manifest.json +++ b/manifest.json @@ -37,6 +37,11 @@ "js": ["/js/places/place-edit.js"] }, + { + "matches": ["https://polytoria.com/create/image", "https://polytoria.com/create/image/"], + "js": ["/js/create/image.js"] + }, + { "matches": ["https://polytoria.com/forum/post/**"], "js": ["/js/forum/forum-view.js"] diff --git a/resources/utils.js b/resources/utils.js index 061c6c4..40baa56 100644 --- a/resources/utils.js +++ b/resources/utils.js @@ -97,7 +97,8 @@ export default { Enabled: false, Banners: true, Rectangles: true - } + }, + UploadMultipleDecals: true, }, Limits: { PinnedGames: 10, @@ -258,5 +259,16 @@ export default { } return mergedObj; + }, + RatelimitRepeatingFetch: async function (...args) { + const req = await fetch(...args); + + if (req.status === 429) { + const retryAfter = req.headers.get('Retry-After') || 1; // Retry after 1 second if no header is present, else use the header value + await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000)); + return RatelimitRepeatingFetch(...args); + } + + return req; } }; diff --git a/settings.html b/settings.html index cedda11..846d1b3 100755 --- a/settings.html +++ b/settings.html @@ -365,6 +365,17 @@ * You can only remove up to friends at once.

+

+   + + Upload multiple decals + + +
+ + Lets you quickly upload multiple decals at once! + +