Created
May 6, 2020 05:08
-
-
Save SheepTester/f8aefd01a0ed76d4311bddfdcacb863c to your computer and use it in GitHub Desktop.
Simple Scratch fetch extension
This file contains hidden or 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
| 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