Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload extension #685

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions extensions/Clay/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(function (Scratch) {
class Upload {
getInfo() {
return {
id: "uploadfile",
ClaytonTDM marked this conversation as resolved.
Show resolved Hide resolved
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 options = {
body: formData,
method: 'POST',
mode: 'cors',
};

Scratch.fetch(args.url, options)
.then(response => {
if (response.ok) {
return response.text();
} else {
throw new Error("Upload failed");
}
})
.then(text => {
resolve(text);
inputElement.remove();
})
.catch(error => {
reject(error.message);
inputElement.remove();
});
} else {
reject("No file chosen");
inputElement.remove();
}
});
});
}
}

Scratch.extensions.register(new Upload());
})(Scratch);
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 @@ -236,3 +236,8 @@ All images in this folder are licensed under the [GNU General Public License ver

## clipboard.svg
- Created by [@AdamMady](https://github.com/AdamMady/)

## 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 @@ -745,6 +745,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