-
Notifications
You must be signed in to change notification settings - Fork 160
Added Both Revoke and Read function to the Blob Extension #439
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
1b09c84
b98564f
2483fe3
fbe1f14
f27c4e6
e178b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,31 @@ | |
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "readBlob", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: "Read blob [URL] as a [TYPE]", | ||
| arguments: { | ||
| URL: { | ||
| type: Scratch.ArgumentType.STRING | ||
| }, | ||
| TYPE: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "text/plain", | ||
| menu: "types" | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "revokeBlob", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "Revoke blob with the URL of [URL]", | ||
| arguments: { | ||
| URL: { | ||
| type: Scratch.ArgumentType.STRING | ||
| } | ||
| }, | ||
| }, | ||
| ] | ||
| ,menus: { | ||
| types: { | ||
|
|
@@ -103,10 +128,43 @@ | |
| } | ||
|
|
||
| toBlob(args, util) { | ||
| const text = String(args.DATA); // ensure it's a string (why can't it be just like python or smth) | ||
| const text = String(args.DATA); | ||
| const blob = new Blob([text], { type: args.TYPE }); | ||
| return URL.createObjectURL(blob); | ||
| } | ||
| } | ||
|
|
||
| revokeBlob(args, util){ | ||
| URL.revokeObjectURL(args.URL); | ||
| } | ||
|
|
||
| readBlob(args, util) { | ||
| async function readBlobContent(url, mime) { | ||
|
|
||
| if (!url.startsWith("blob:")) return ""; // I WOULD return "Url Error", but it would make it a bit harder to track if an output has been returned or not | ||
|
|
||
| const response = await fetch(url); | ||
| const blob = await response.blob(); | ||
|
|
||
|
|
||
| if (blob.size === 0) return ""; // js for u TheShovel | ||
|
|
||
| if (mime.startsWith("text/") || mime === "application/json") { | ||
| return await blob.text(); | ||
| } | ||
|
|
||
| const buffer = await blob.arrayBuffer(); | ||
|
||
| const view = new DataView(buffer); | ||
|
|
||
| let binary = ''; // = decoder.decode(bytes); Decoder doesn't work with btoa, and replacing it reduces efficiency. | ||
|
|
||
| for (let i = 0; i < view.byteLength; i++) { | ||
| binary += String.fromCharCode(view.getUint8(i)); | ||
| } | ||
| return btoa(binary); | ||
|
||
| } | ||
|
|
||
| return readBlobContent(args.URL, args.TYPE); | ||
|
||
| } | ||
| } | ||
| Scratch.extensions.register(new faunks_Blobs()); | ||
| })(Scratch); | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could throw an error. So
throw new URIError("must be a blob url");or something similar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. I've just added it so it would do that (I hope it also acts as a return method)