NitroCipher

Creating extensions for Scratch 3.0


There is now official documentation regarding extensions!, but feel free to use this post to get you started


You'll want to start off by creating your extension's class, and register the extension - In my case, this would be ‘NitroBlock’

class NitroBlock { //In both instances, NitroBlock will be the name in both instances
}
Scratch.extensions.register(new NitroBlock());

Next, we will be constructing block and menu definitions - We will continue to use ‘NitroBlock’ through this tutorial
getInfo() {
    return {
        "id": "NitroBlock",
        "name": "NitroBlock",
        "blocks": [
        ],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}

We are going to take a look at how blocks are constructed

For those of you that are familiar with extensions for Scratch 2.0, we will start off with this: - If not, you can ignore this
['r', 'letters %n through %n of %s', 'substringy', '2', '5', 'hello world']
//breakdown below:
['r' = block type, 'letters %n through %n of %s' = block text, 'substringy' = block ID/opcode]

{
    "opcode": "substringy", //This will be the ID code for the block
    "blockType": "reporter", //This can either be Boolean, reporter, command, or hat
    "text": "letters [num1] through [num2] of [string]", //This is the block text, and how it will display in the Scratch interface
    "arguments": { //Arguments are the input fields in the block. In the block text, place arguments in square brackets with the corresponding ID 
        "num1": { //This is the ID for your argument
            "type": "number", //This can be either Boolean, number, or string
            "defaultValue": "2" //This is the default text that will appear in the input field, you can leave this blank if you wish
        },
        "num2": {
            "type": "number",
            "defaultValue": "5"
        },
        "string": {
            "type": "string",
            "defaultValue": "hello world"
        }
    }
},

We will put this newly constructed code into the blocks object above - My code will now look like this
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}
Scratch.extensions.register(new NitroBlock());

Next we come to the most important part, the code that actually runs the blocks! - I am keeping this short and simple for tutorial's sake.
//Make sure you name this function with with the proper ID for the block you defined above
substringy({num1, num2, string}) { //these names will match the argument names you used earlier, and will be used as the variables in your code
    //this code can be anything you want
    return string.substring(num1 - 1, num2);  //for reporters and Boolean blocks the important thing is to use 'return' to get the value back into Scratch.
}

Place this new code below your getInfo() function
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
    substringy({num1, num2, string}) {
        return string.substring(num1 - 1, num2);
    };
}

Save this code to your computer as a js file, in my case it is NitroBlock_3.js

Congratz, you now have your extension code created!!
You can use this tutorial to add it your own personal copy of scratch
Or, you can host the extension with gh-pages, and go here to test it
(provide your own url, don't use mine)

Here is the archived copy of my original post

savaka

Looks a lot easier to understand

Abe_Lincoln

savaka wrote:

Looks a lot easier to understand
Yes, but a lot longer than a simple line of JSON. It just means all extensions will have to be updated. Maybe I should build a converter that does that.

aqmoreira

Hi NitroCipher,

I'm playing with scratch 3 and found this post. Can you tell me where I can find more information to make a Command button that in the arguments values ​​come from a menu or array.

Samples:
point towards [ v]


Thanks,

Allan Moreira

NitroCipher

aqmoreira wrote:

Hi NitroCipher,

I'm playing with scratch 3 and found this post. Can you tell me where I can find more information to make a Command button that in the arguments values ​​come from a menu or array.

Samples:
point towards [ v]


Thanks,

Allan Moreira

At the moment, not much is known (publically) about the extensions. I will post here as soon as I discover more.

JGames101

Ooh! Translation support!
translation_map: {
            de: {
                'extensionName': 'Einige Blöcke',
                'myReporter': 'Buchstabe [LETTER_NUM] von [TEXT]',
                'myReporter.TEXT_default': 'Text',
                'menuA_item1': 'Artikel eins',
                // Dynamic menus can be translated too
                'menuB_example': 'Beispiel',
                // This message contains ICU placeholders (see `myReporter()` below)
                'myReporter.result': 'Buchstabe {LETTER_NUM} von {TEXT} ist {LETTER}.'
            },
            it: {
                // ...
            }
        },
This thing is interesting - it lets you filter where your block appears (stage, sprite, both), but in the example shows another extension.
                // Optional: list of target types for which this block should appear.
                // If absent, assume it applies to all builtin targets -- that is:
                // ['sprite', 'stage']
                filter: ['someBlocks.wedo2', 'sprite', 'stage']
            },

JGames101

aqmoreira wrote:

Hi NitroCipher,

I'm playing with scratch 3 and found this post. Can you tell me where I can find more information to make a Command button that in the arguments values ​​come from a menu or array.

Samples:
point towards [ v]


Thanks,

Allan Moreira
Here's the menu stuff I found:
// Optional: define extension-specific menus here.
        menus: {
            // Required: an identifier for this menu, unique within this extension.
            menuA: [
                // Static menu: list items which should appear in the menu.
                {
                    // Required: the value of the menu item when it is chosen.
                    value: 'itemId1',
                    // Optional: the human-readable label for this item.
                    // Use `value` as the text if this is absent.
                    text: 'Item One'
                },
                // The simplest form of a list item is a string which will be used as
                // both value and text.
                'itemId2'
            ],
            // Dynamic menu: a string naming a function which returns an array as above.
            // Called each time the menu is opened.
            menuB: 'getItemsForMenuB'
        },

AmazingMech2418

I actually found stuff like this on the Scratch 3.0 source code extension example. How do you add an extension to a project in Scratch 3?

NitroCipher

AmazingMech2418 wrote:

I actually found stuff like this on the Scratch 3.0 source code extension example. How do you add an extension to a project in Scratch 3?

I have yet to figure out how to do it any other way than putting the code into a fork of scratch 3.0 on github

JGames101

I am working on porting my extensions to Scratch 3 with the new format. Do you know where the actual used extension files are stored? Not the examples, but where the actual extension file is?

PullJosh

JGames101 wrote:

I am working on porting my extensions to Scratch 3 with the new format. Do you know where the actual used extension files are stored? Not the examples, but where the actual extension file is?
Not entirely sure what you're tying to ask, but this might be of interest: https://github.com/LLK/scratch-vm/tree/develop/src/extensions

JGames101-Tutorials

PullJosh wrote:

JGames101 wrote:

I am working on porting my extensions to Scratch 3 with the new format. Do you know where the actual used extension files are stored? Not the examples, but where the actual extension file is?
Not entirely sure what you're tying to ask, but this might be of interest: https://github.com/LLK/scratch-vm/tree/develop/src/extensions
That's exactly what I was looking for, thanks

NitroCipher

Thx for the sticky!

JGames101

I have managed to port one of my extension to Scratch 3 (the blocks don't completely work yet, but I'm working on that, but they do load)

I followed the instructions to download the latest version of the scratch gui to work on it. I then copied the example extension, and rewrote my notifications extension in that format. Then, I uploaded it to github. I found the file that controls the list of extensions in the extensions button, and added mine. I set the URL to my github file, and it loaded in, and as you can see, the blocks appeared.

Official instructions for developing Scratch 3 extensions will be coming sometime between now and August (Scratch 3 release), according to The FAQ.

Update: I have everything working, except asking for notification permission. For some reason, that doesn't work.
Code can be found at https://github.com/JGames101/scratch-extensions/blob/master/notifications/3.js

To test your extension, add it to the index.js file located here: https://github.com/LLK/scratch-gui/tree/develop/src/lib/libraries/extensions
You can use actual URLs.
Then, if you open the page, you should see an extra Extension under the extensions popup thing. Clicking it will add your extension to the project, and you can test it.

So… testing your extensions currently requires downloading the source for scratch 3's GUI. No parts, like the VM or Renderer, are needed to be changed to test extensions, despite where default scratch extensions are stored (I went through the process of forking the VM for nothing…)

I will be making some simple demo extensions, that you'll be able to see below, and then I'll publish a fork of Scratch-GUI with some sample extensions.

NitroCipher

JGames101 wrote:

I have managed to port one of my extension to Scratch 3 (the blocks don't completely work yet, but I'm working on that, but they do load)

I followed the instructions to download the latest version of the scratch gui to work on it. I then copied the example extension, and rewrote my notifications extension in that format. Then, I uploaded it to github. I found the file that controls the list of extensions in the extensions button, and added mine. I set the URL to my github file, and it loaded in, and as you can see, the blocks appeared.

Official instructions for developing Scratch 3 extensions will be coming sometime between now and August (Scratch 3 release), according to The FAQ.

Update: I have everything working, except asking for notification permission. For some reason, that doesn't work.
Code can be found at https://github.com/JGames101/scratch-extensions/blob/master/notifications/3.js

To test your extension, add it to the index.js file located here: https://github.com/LLK/scratch-gui/tree/develop/src/lib/libraries/extensions
You can use actual URLs.
Then, if you open the page, you should see an extra Extension under the extensions popup thing. Clicking it will add your extension to the project, and you can test it.

So… testing your extensions currently requires downloading the source for scratch 3's GUI. No parts, like the VM or Renderer, are needed to be changed to test extensions, despite where default scratch extensions are stored (I went through the process of forking the VM for nothing…)

I will be making some simple demo extensions, that you'll be able to see below, and then I'll publish a fork of Scratch-GUI with some sample extensions.

Cool, which format do you find easier to use? Personally I like the old format, mainly because I am more used to it, and I haven't played with the new one much.

JGames101

NitroCipher wrote:

JGames101 wrote:

I have managed to port one of my extension to Scratch 3 (the blocks don't completely work yet, but I'm working on that, but they do load)

I followed the instructions to download the latest version of the scratch gui to work on it. I then copied the example extension, and rewrote my notifications extension in that format. Then, I uploaded it to github. I found the file that controls the list of extensions in the extensions button, and added mine. I set the URL to my github file, and it loaded in, and as you can see, the blocks appeared.

Official instructions for developing Scratch 3 extensions will be coming sometime between now and August (Scratch 3 release), according to The FAQ.

Update: I have everything working, except asking for notification permission. For some reason, that doesn't work.
Code can be found at https://github.com/JGames101/scratch-extensions/blob/master/notifications/3.js

To test your extension, add it to the index.js file located here: https://github.com/LLK/scratch-gui/tree/develop/src/lib/libraries/extensions
You can use actual URLs.
Then, if you open the page, you should see an extra Extension under the extensions popup thing. Clicking it will add your extension to the project, and you can test it.

So… testing your extensions currently requires downloading the source for scratch 3's GUI. No parts, like the VM or Renderer, are needed to be changed to test extensions, despite where default scratch extensions are stored (I went through the process of forking the VM for nothing…)

I will be making some simple demo extensions, that you'll be able to see below, and then I'll publish a fork of Scratch-GUI with some sample extensions.

Cool, which format do you find easier to use? Personally I like the old format, mainly because I am more used to it, and I haven't played with the new one much.
I think that this one will be easier for people who have never made extensions before, but I think I personally prefer the older one. I don't mind the new one, though, and it has some nice new features.

edbotinschools

Hi.
I currently have an HTTP extension in Scratch 2. Any ideas how I can convert this to Scratch 3??
Thanks.
C

NitroCipher

edbotinschools wrote:

Hi.
I currently have an HTTP extension in Scratch 2. Any ideas how I can convert this to Scratch 3??
Thanks.
C

Hi! Can you put the extension up on github? I'd be happy to take a look at it!

(I am not entirely familiar with HTTP extensions, I mainly focus on javascript)

CoolGamesNet

NitroCipher,
I am not the originator but I just started with the Roland Go:Keys 61 extension added to Ver.1.21 firmware.

https://rolandcom.github.io/gokeys-scratch-extension/res/gokeys_block.jpg

https://www.roland.com/us/products/gokeys_go-61k/downloads/
and
https://rolandcom.github.io/gokeys-scratch-extension/

I am https://github.com/CoolGames or here as Developer at CoolGames dot net

Please help as I am using Ubuntu Studio 17.10 Linux and have the offline editor localhost working as much I can
with out Node.JS or NPM skills.
I would like to test the existing and rewritten extension and communicate with Roland for this effort.

Lee T. Davy
Retired and still working for our children's future !

discoverypark

How about if you could make your own extensions? (in scratch 3.0)