Skip to content

Instantly share code, notes, and snippets.

@SheepTester
Created May 6, 2020 05:08
Show Gist options
  • Select an option

  • Save SheepTester/f8aefd01a0ed76d4311bddfdcacb863c to your computer and use it in GitHub Desktop.

Select an option

Save SheepTester/f8aefd01a0ed76d4311bddfdcacb863c to your computer and use it in GitHub Desktop.
Simple Scratch fetch extension
class Fetch {
constructor () {}
getInfo () {
return {
id: 'fetch',
name: 'Fetch',
blocks: [
{
opcode: 'get',
blockType: Scratch.BlockType.REPORTER,
text: 'GET from [URL]',
arguments: {
URL: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'https://httpbin.org/get'
}
}
},
{
opcode: 'post',
blockType: Scratch.BlockType.REPORTER,
text: 'POST [BODY] to [URL]',
arguments: {
URL: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'https://httpbin.org/post'
},
BODY: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello'
}
}
}
]
}
}
get ({ URL }) {
return fetch(URL, { method: 'GET' })
.then(res => res.text())
.catch(err => '')
}
post ({ URL, BODY }) {
return fetch(URL, { method: 'POST', body: BODY })
.then(res => res.text())
.catch(err => '')
}
}
Scratch.extensions.register(new Fetch())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment