How Can We Help?

Extension Event Handler (Common Code Configuration)

extension.onLoad

Function: Triggered when an extension is loaded

Note: This function can be triggered multiple times during the loading and uploading of an extension. When an extension is loaded only once, this function may also be triggered multiple times if the extension supports multiple sprites, triggered once for each sprite.

Example:

1    async onLoad(app, target)
2    {
3            // app.log prints information in the web ide debugging window, same function as console.log,
4            // target.id is the unique identifier of an extension
5            app.log(‘I am loaded, get ID of ‘+ target.id);
6    }

extension.onUnload

Function: Triggered when an extension is unloaded

Example:

1    onUnload(app, target)
2    {
3             app.log(“I am unloading, bye, yours “+ target.id);
4    }

extension.onStopAll

Function: Triggered when you click the red button in mBlock or use the stop all block

Example:

1     onStopAll(app, device) {
2             app.log(“stop running”);
3     }

extension.onConnect

Function: Triggered when a device is connected

Note: The communication of a device must be performed after this function responds. You can determine whether a device is connected by using the device.isConnected function.

Example:

1    onConnect(app, device) {
2             app.log(“${device.id} is connected, you can send data to ${device.id}”);
3             device.writeText(“a text message”);
4    }

extension.onDisconnect

Function: Triggered when an extension is disconnected

Note: When this function is triggered, communication with the device fails. You can determine whether an extension is connected by using the device.isConnected function.

Example:

1    onDisconnect (app, device) {
2            app.log(“${device.id} is not connected, you can not communicate to device any more”);
3    }

extension.beforeChangeUploadMode

Function: Triggered when the upload mode is requested by sending data to a device to switch to the upload mode
Note: This function must be used in combination with

afterChangeUploadMode. The

afterChangeUploadMode function is triggered only when true is returned.

Example:

1    async beforeChangeUploadMode(app, device) {
2            // Asynchronous waiting, refer to the syntax of javascript es6
3            // Use a protocol of the F3 & F4 protocol group
4            await device.asyncWriteProtocol(‘f3f4’, [0x0d, 0x00, 0x03]);
5            // Assume that the operation succeeds
6            return true;
7    }

extension.afterChangeUploadMode

Function: Triggered when the device enters the upload mode

Note: This function must be used in combination with beforeChangeUploadMode. It is triggered only when beforeChangeUploadMode returns true.

Example:

1    afterChangeUploadMode(app, device) {
2          app.log(“${device.id} switch to upload mode successfully”);
3    }

extension.beforeChangeDebugMode

Function: Triggered when the debugging (live) mode is requested by sending data to a device to switch to the debugging (live) mode

Note: This function must be used in combination with afterChangeDebugMode. The afterChangeDebugMode function is triggered only when true is returned.

Example:

1    async beforeChangeDebugMode(app, device) {
2             // Asynchronous waiting, refer to the syntax of javascript es6
3             // Use the mode switching protocol of the F3 & F4 protocol group
4             await device.asyncWriteProtocol(‘f3f4’, [0x0d, 0x00, 0x00]);
5             // Assume that the operation succeeds
6             return true;
7    }

extension.afterChangeUploadMode

Function: Triggered when the device enters the debug (live) mode

Note: This function must be used in combination with

beforeChangeDebugMode. The afterChangeDebugMode function is triggered only when beforeChangeDebugMode returns true.

Example:

1    afterChangeUploadMode(app, device) {
2    app.log(“${device.id} switch to online mode successfully”);
3    }

extension.onSelect

Function: Triggered when you click a sprite or device to switch to it in a project involving multiple devices

Note: This function is triggered again when the main extension of the device is loaded.

Example:

1    onSelect(app, device) {
2             app.log(“switch to ${device.id}”);
3    }

extension.onUnselect

Function: Triggered when you click a sprite or device to switch to it in a project involving multiple devices

Note: When you switch a sprite, the onUnselect function is triggered first, and then the onSelect function.

Example:

1    onUnselect(app, device) {
2             app.log(“from ${device.id} switch to other target”);
3    }

extension.onRead

Function: Triggered when device data is received

Note: This function is triggered only when data is obtained from a device.

Example:

1    onRead(app, device) {
2             let anyText = device.readText(false);
3             app.log(“text receive from ${device.id}: ${anyText}”);
4    }

extension.beforeCodeUpload

Function: Triggered in the upload mode when the code is ready to be uploaded to a device

Note: When this function is triggered, the code has not been uploaded to the device yet.

Example:

1    // The following obtains and modify the code before the code is uploaded to the device:
2    beforeCodeUpload(app, device) {
3          let {type, text} = device.getCode();
4          app.log(“upload code of ${type}: ${text}”);
5          return “# prepend before code\n” + text;
6    }

extension.afterCodeUpload

Function: Triggered in the upload mode when the uploading of the code is complete

Note: When this function is triggered, the code has been uploaded to the device.

Example:

1    onUnselect(app, device) { app.log(“congratulations! you’ve uploaded code to ${device.id}”); }