We started working on the WorkAdventure scripting API with this in mind, but at some point, maybe you will find that
a feature is missing in the API. This article is here to explain to you how to add this feature.
## How to extend the scripting API?
Extending the scripting API means modifying the core of WorkAdventure. You can of course run these
modifications on your self-hosted instance.
But if you want to share it with the wider community, I strongly encourage you to start by [opening an issue](https://github.com/thecodingmachine/workadventure/issues)
on GitHub before starting the development. Check with the core maintainers that they are willing to merge your idea
before starting developing it. Once a new function makes it into the scripting API, it is very difficult to make it
evolve (or to deprecate), so the design of the function you add needs to be carefully considered.
## How does it work?
Scripts are executed in the browser, inside an iframe.
![](images/scripting_1.svg)
The iframe allows WorkAdventure to isolate the script in a sandbox. Because the iframe is sandbox (or on a different
domain than the WorkAdventure server), scripts cannot directly manipulate the DOM of WorkAdventure. They also cannot
directly access Phaser objects (Phaser is the game engine used in WorkAdventure). This is by-design. Since anyone
can contribute a map, we cannot allow anyone to run any code in the scope of the WorkAdventure server (that would be
The `sendToWorkadventure` function is a utility function that dispatches the message to the main frame.
In WorkAdventure, the message is received in the [`IframeListener` listener class](http://github.com/thecodingmachine/workadventure/blob/1e6ce4dec8697340e2c91798864b94da9528b482/front/src/Api/IframeListener.ts#L200-L203).
This class is in charge of analyzing the JSON messages received and dispatching them to the right place in the WorkAdventure application.
The message callback implemented in `IframeListener` is a giant (and disgusting) `if` statement branching to the correct
part of the code depending on the `type` property.
**src/Api/IframeListener.ts**
```typescript
// ...
} else if (payload.type === "setProperty" && isSetPropertyEvent(payload.data)) {
this._setPropertyStream.next(payload.data);
} else if (payload.type === "chat" && isChatEvent(payload.data)) {
scriptUtils.sendAnonymousChat(payload.data);
} else if (payload.type === "openPopup" && isOpenPopupEvent(payload.data)) {
this._openPopupStream.next(payload.data);
} else if (payload.type === "closePopup" && isClosePopupEvent(payload.data)) {
// ...
```
In this particular case, we call `scriptUtils.sendAnonymousChat` that is doing the work of displaying the chat message.
## Scripting API entry point
The `WA` object originates from the scripting API. This script is hosted on the front server, at `https://[front_WA_server]/iframe_api.js.`.
The entry point for this script is the file `front/src/iframe_api.ts`.
All the other files dedicated to the iframe API are located in the `src/Api/iframe` directory.
Of course, messaging can go the other way around and WorkAdventure can also send messages to the iframes.
We use the [`IFrameListener.postMessage`](http://github.com/thecodingmachine/workadventure/blob/ab075ef6f4974766a3e2de12a230ac4df0954b58/front/src/Api/IframeListener.ts#L455-L459) function for this.
Finally, there is a last type of utility function (a quite powerful one). It is quite common to need to call a function
From this type guard, the library can automatically generate the `ChatEvent` type that we can refer in our code.
The advantage of this technique is that, **at runtime**, WorkAdventure can verify that the JSON message received
over the postMessage API is indeed correctly formatted.
If you are not familiar with Typescript type guards, you can read [an introduction to type guards in the Typescript documentation](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards).
### Typing one way messages
For "one-way" messages (from the iframe to WorkAdventure), the `sendToWorkadventure` method expects the passed
object to be of type `IframeEvent<keyof IframeEventMap>`.
Note: I'd like here to thank @jonnytest1 for helping set up this type system. It rocks ;)
The `IFrameEvent` type is defined in `front/src/Api/Events/IframeEvent.ts`: