This repository has been archived on 2026-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
polyplus/js/create/image.js

81 lines
2.5 KiB
JavaScript

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(
"form[action^=\"/create\"] button[type=\"submit\"]"
);
console.log(submitBtn)
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();
});
}
});
})();