-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
class MistFetch { | ||
constructor() { | ||
this.requests = {}; | ||
} | ||
|
||
getInfo() { | ||
return { | ||
id: 'mistfetch', | ||
name: 'Mist Fetch', | ||
blocks: [ | ||
{ | ||
opcode: 'fetchUrlWithId', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'fetch [URL] with ID [ID]', | ||
arguments: { | ||
URL: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'https://example.com' | ||
}, | ||
ID: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'request1' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'getBytesById', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'bytes downloaded for ID [ID]', | ||
arguments: { | ||
ID: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'request1' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'getResponseBodyById', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'response body for ID [ID]', | ||
arguments: { | ||
ID: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'request1' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'isRequestCompleted', | ||
blockType: Scratch.BlockType.BOOLEAN, | ||
text: 'is request [ID] completed?', | ||
arguments: { | ||
ID: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'request1' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'deleteRequestById', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'delete request with ID [ID]', | ||
arguments: { | ||
ID: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: 'request1' | ||
} | ||
} | ||
} | ||
] | ||
}; | ||
} | ||
|
||
fetchUrlWithId({ URL, ID }) { | ||
if (this.requests[ID]) { | ||
return `Request with ID ${ID} is already in progress.`; | ||
} | ||
|
||
this.requests[ID] = { totalBytes: 0, response: '', completed: false }; | ||
|
||
fetch(URL) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
return response.body.getReader(); | ||
}) | ||
.then(reader => { | ||
let done = false; | ||
const decoder = new TextDecoder(); | ||
const processStream = async () => { | ||
while (!done) { | ||
const { done: doneReading, value } = await reader.read(); | ||
if (doneReading) { | ||
done = true; | ||
this.requests[ID].completed = true; | ||
} else { | ||
this.requests[ID].totalBytes += value.length; | ||
this.requests[ID].response += decoder.decode(value, { stream: true }); | ||
} | ||
} | ||
}; | ||
return processStream(); | ||
}) | ||
.catch(error => { | ||
this.requests[ID].error = error.message; | ||
}); | ||
|
||
return `Started fetching URL with ID: ${ID}`; | ||
} | ||
|
||
getBytesById({ ID }) { | ||
if (this.requests[ID]) { | ||
if (this.requests[ID].error) { | ||
return `Error: ${this.requests[ID].error}`; | ||
} | ||
return this.requests[ID].totalBytes; | ||
} | ||
return `No request found for ID: ${ID}`; | ||
} | ||
|
||
getResponseBodyById({ ID }) { | ||
if (this.requests[ID]) { | ||
if (this.requests[ID].error) { | ||
return `Error: ${this.requests[ID].error}`; | ||
} | ||
return this.requests[ID].response; | ||
} | ||
return `No request found for ID: ${ID}`; | ||
} | ||
|
||
isRequestCompleted({ ID }) { | ||
if (this.requests[ID]) { | ||
return this.requests[ID].completed; | ||
} | ||
return `No request found for ID: ${ID}`; | ||
} | ||
|
||
deleteRequestById({ ID }) { | ||
if (this.requests[ID]) { | ||
delete this.requests[ID]; | ||
return `Request with ID ${ID} has been deleted.`; | ||
} | ||
return `No request found for ID: ${ID}`; | ||
} | ||
} | ||
|
||
// Register the extension | ||
Scratch.extensions.register(new MistFetch()); |