diff --git a/extensions/Clay/upload.js b/extensions/Clay/upload.js new file mode 100644 index 0000000000..57577fcef2 --- /dev/null +++ b/extensions/Clay/upload.js @@ -0,0 +1,69 @@ +class Upload { + getInfo() { + return { + id: "uploadfile", + name: "Upload", + blocks: [ + { + opcode: "upload", + blockType: Scratch.BlockType.REPORTER, + text: "upload file to [url]", + arguments: { + url: { + type: Scratch.ArgumentType.STRING, + defaultValue: "https://store1.gofile.io/uploadFile", + }, + } + } + ] + }; + } + + upload(args) { + return new Promise((resolve, reject) => { + const inputElement = document.createElement("input"); + inputElement.type = "file"; + inputElement.style.display = "none"; + document.body.appendChild(inputElement); + + inputElement.click(); + + inputElement.addEventListener("change", function() { + if (this.files && this.files[0]) { + const formData = new FormData(); + formData.append("file", this.files[0], this.files[0].name); + + const url = args.url; + + const xhr = new XMLHttpRequest(); + xhr.open("POST", url, true); + + xhr.onload = function() { + if (xhr.readyState === 4 && xhr.status === 200) { + resolve(xhr.responseText); + inputElement.remove(); + } else { + reject("Upload failed"); + inputElement.remove(); + // throw 'Upload failed'; + } + }; + + xhr.onerror = function() { + reject("Upload failed"); + inputElement.remove(); + // throw 'Upload failed'; + }; + + xhr.send(formData); + } else { + reject("No file chosen"); + inputElement.remove(); + // throw 'No file chosen'; + } + }); + }); + } + } + + Scratch.extensions.register(new Upload()); \ No newline at end of file diff --git a/images/Clay/upload.png b/images/Clay/upload.png new file mode 100644 index 0000000000..22beec96f3 Binary files /dev/null and b/images/Clay/upload.png differ diff --git a/images/README.md b/images/README.md index 9b629d4eb8..95305040aa 100644 --- a/images/README.md +++ b/images/README.md @@ -233,3 +233,8 @@ All images in this folder are licensed under the [GNU General Public License ver ## Lily/LooksPlus.svg - Created by [@LilyMakesThings](https://github.com/LilyMakesThings) in https://github.com/TurboWarp/extensions/pull/656 + +## Clay/upload.png + - Created by [@ClaytonTDM](https://github.com/ClaytonTDM) + - Upload icon based on https://icons8.com/icon/367/upload + - Background based on https://app.haikei.app/ \ No newline at end of file diff --git a/website/index.ejs b/website/index.ejs index 653cd4d146..12ec7f85e0 100644 --- a/website/index.ejs +++ b/website/index.ejs @@ -727,6 +727,12 @@
Helpful utilities for any fast food employee. Created by LilyMakesThings.
+ +Upload files to web servers via Multipart Form data (multipart/form-data). Created by ClaytonTDM.
+