Skip to content

Commit

Permalink
Add upload extension
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jul 3, 2023
1 parent d374041 commit b298d90
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
69 changes: 69 additions & 0 deletions extensions/Clay/upload.js
Original file line number Diff line number Diff line change
@@ -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());
Binary file added images/Clay/upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,12 @@
<h3>McUtils</h3>
<p>Helpful utilities for any fast food employee. Created by <a href="https://scratch.mit.edu/users/LilyMakesThings/">LilyMakesThings</a>.</p>
</div>
<div class="extension">
<%- banner('Clay/upload') %>
<h3>Upload</h3>
<p>Upload files to web servers via Multipart Form data (multipart/form-data). Created by <a href="https://scratch.mit.edu/users/ClaytonTDM/">ClaytonTDM</a>.</p>
</div>
</div>
</div>
Expand Down

0 comments on commit b298d90

Please sign in to comment.