async function uploadToFileIo(file) {
const formData = new FormData();
formData.append("file", file);
const response = await fetch("https://file.io", {
method: "POST",
body: formData,
});
const result = await response.json();
return result.link;
}
if (typeof HTMLElement !== "undefined") {
class UploadURL extends HTMLElement {
constructor() {
super();
this.fileInput = document.createElement("input");
this.fileInput.type = "file";
this.appendChild(this.fileInput);
this.urlInput = document.createElement("input");
this.urlInput.type = "hidden";
this.urlInput.name = this.getAttribute("name");
this.appendChild(this.urlInput);
}
connectedCallback() {
this.fileInput.addEventListener("change", async (event) => {
if (this.fileInput.files.length > 0) {
const file = this.fileInput.files[0];
console.log("Uploading...");
const url = await uploadToFileIo(file);
console.log(url);
this.urlInput.value = url;
this.fileInput.files = null;
}
});
}
}
customElements.define("upload-url", UploadURL);
}