Merge branch 'develop' of github.com:thecodingmachine/workadventure into metadataScriptingApi
This commit is contained in:
commit
cd2d090a8b
348
docs/maps/scripting.md
Normal file
348
docs/maps/scripting.md
Normal file
@ -0,0 +1,348 @@
|
|||||||
|
{.alert.alert-danger}
|
||||||
|
This feature is "_experimental_". We may apply changes in the near future to the way it works when we gather some feedback.
|
||||||
|
|
||||||
|
{.section-title.accent.text-primary}
|
||||||
|
# Scripting WorkAdventure maps
|
||||||
|
|
||||||
|
Do you want to add a bit of intelligence to your map? Scripts allow you to create maps with special features.
|
||||||
|
|
||||||
|
You can for instance:
|
||||||
|
|
||||||
|
* Create FTUE (First Time User Experience) scenarios where a first-time user will be displayed a notification popup.
|
||||||
|
* Create NPC (non playing characters) and interact with those characters using the chat.
|
||||||
|
* Organize interactions between an iframe and your map (for instance, walking on a special zone might add a product in the cart of an eCommerce website...)
|
||||||
|
* etc...
|
||||||
|
|
||||||
|
Please note that scripting in WorkAdventure is at an early stage of development and that more features might be added in the future. You can actually voice your opinion about useful features by adding [an issue on Github](https://github.com/thecodingmachine/workadventure/issues).
|
||||||
|
|
||||||
|
{.alert.alert-warning}
|
||||||
|
**Beware:** Scripts are executed in the browser of the current user only. Generally speaking, scripts cannot be used to trigger a change that will be displayed on other users screen.
|
||||||
|
|
||||||
|
## Scripting language
|
||||||
|
|
||||||
|
Client-side scripting is done in **Javascript** (or any language that transpiles to Javascript like _Typescript_).
|
||||||
|
|
||||||
|
There are 2 ways you can use the scripting language:
|
||||||
|
|
||||||
|
* **In the map**: By directly referring a Javascript file inside your map, in the `script` property of your map.
|
||||||
|
* **In an iFrame**: By placing your Javascript script into an iFrame, your script can communicate with the WorkAdventure game
|
||||||
|
|
||||||
|
## Adding a script in the map
|
||||||
|
|
||||||
|
Create a `script` property in your map.
|
||||||
|
|
||||||
|
In Tiled, in order to access your map properties, you can click on _"Map > Map properties"_.
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<figure class="figure">
|
||||||
|
<img src="https://workadventu.re/img/docs/admin/map_properties.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
<figcaption class="figure-caption">The Map properties menu</figcaption>
|
||||||
|
</figure>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
Create a `script` property (a "string"), and put the URL of your script.
|
||||||
|
|
||||||
|
You can put relative URLs. If your script file is next to your map, you can simply write the name of the script file here.
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<figure class="figure">
|
||||||
|
<img src="https://workadventu.re/img/docs/script_property.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
<figcaption class="figure-caption">The script property</figcaption>
|
||||||
|
</figure>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
Start by testing this with a simple message sent to the chat.
|
||||||
|
|
||||||
|
**script.js**
|
||||||
|
```javascript
|
||||||
|
WA.sendChatMessage('Hello world', 'Mr Robot');
|
||||||
|
```
|
||||||
|
|
||||||
|
The `WA` objects contains a number of useful methods enabling you to interact with the WorkAdventure game. For instance, `WA.sendChatMessage` opens the chat and adds a message in it.
|
||||||
|
|
||||||
|
In your browser console, when you open the map, the chat message should be displayed right away.
|
||||||
|
|
||||||
|
## Adding a script in an iFrame
|
||||||
|
|
||||||
|
In WorkAdventure, you can easily [open an iFrame using the `openWebsite` property on a layer](special-zones). However, by default, the iFrame is not allowed to communicate with WorkAdventure.
|
||||||
|
|
||||||
|
This is done to improve security. In order to be able to execute a script that communicates with WorkAdventure inside an iFrame, you have to **explicitly allow the iFrame to use the "iFrame API"**.
|
||||||
|
|
||||||
|
In order to allow communication with WorkAdventure, you need to add an additional property: `openWebsiteAllowApi`. This property must be _boolean_ and you must set it to "true".
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<figure class="figure">
|
||||||
|
<img src="https://workadventu.re/img/docs/open_website_allow_api.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
<figcaption class="figure-caption">The `openWebsiteAllowApi` property</figcaption>
|
||||||
|
</figure>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
In your iFrame HTML page, you now need to import the _WorkAdventure client API Javascript library_. This library contains the `WA` object that you can use to communicate with WorkAdventure.
|
||||||
|
|
||||||
|
The library is available at `https://play.workadventu.re/iframe_api.js`.
|
||||||
|
|
||||||
|
_Note:_ if you are using a self-hosted version of WorkAdventure, use `https://[front_domain]/iframe_api.js`
|
||||||
|
|
||||||
|
**iframe.html**
|
||||||
|
```html
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<script src="https://play.workadventu.re/iframe_api.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
You can now start by testing this with a simple message sent to the chat.
|
||||||
|
|
||||||
|
**iframe.html**
|
||||||
|
```html
|
||||||
|
...
|
||||||
|
<script>
|
||||||
|
WA.sendChatMessage('Hello world', 'Mr Robot');
|
||||||
|
</script>
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
Let's now review the complete list of methods available in this `WA` object.
|
||||||
|
|
||||||
|
## Using Typescript
|
||||||
|
|
||||||
|
View the dedicated page about [using Typescript with the scripting API](using-typescript).
|
||||||
|
|
||||||
|
## Available features in the client API
|
||||||
|
|
||||||
|
### Sending a message in the chat
|
||||||
|
|
||||||
|
sendChatMessage(message: string, author: string): void
|
||||||
|
|
||||||
|
Sends a message in the chat. The message is only visible in the browser of the current user.
|
||||||
|
|
||||||
|
* **message**: the message to be displayed in the chat
|
||||||
|
* **author**: the name displayed for the author of the message. It does not have to be a real user.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.sendChatMessage('Hello world', 'Mr Robot');
|
||||||
|
```
|
||||||
|
|
||||||
|
### Listening to messages from the chat
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
onChatMessage(callback: (message: string) => void): void
|
||||||
|
```
|
||||||
|
|
||||||
|
Listens to messages typed by the current user and calls the callback. Messages from other users in the chat cannot be listened to.
|
||||||
|
|
||||||
|
* **callback**: the function that will be called when a message is received. It contains the message typed by the user.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.onChatMessage((message => {
|
||||||
|
console.log('The user typed a message', message);
|
||||||
|
}));
|
||||||
|
```
|
||||||
|
|
||||||
|
### Detecting when the user enters/leaves a zone
|
||||||
|
|
||||||
|
```
|
||||||
|
onEnterZone(name: string, callback: () => void): void
|
||||||
|
onLeaveZone(name: string, callback: () => void): void
|
||||||
|
```
|
||||||
|
|
||||||
|
Listens to the position of the current user. The event is triggered when the user enters or leaves a given zone. The name of the zone is stored in the map, on a dedicated layer with the `zone` property.
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<figure class="figure">
|
||||||
|
<img src="https://workadventu.re/img/docs/trigger_event.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
<figcaption class="figure-caption">The `zone` property, applied on a layer</figcaption>
|
||||||
|
</figure>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
* **name**: the name of the zone, as defined in the `zone` property.
|
||||||
|
* **callback**: the function that will be called when a user enters or leaves the zone.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.onEnterZone('myZone', () => {
|
||||||
|
WA.sendChatMessage("Hello!", 'Mr Robot');
|
||||||
|
})
|
||||||
|
|
||||||
|
WA.onLeaveZone('myZone', () => {
|
||||||
|
WA.sendChatMessage("Goodbye!", 'Mr Robot');
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Opening a popup
|
||||||
|
|
||||||
|
In order to open a popup window, you must first define the position of the popup on your map.
|
||||||
|
|
||||||
|
You can position this popup by using a "rectangle" object in Tiled that you will place on an "object" layer.
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<img src="https://workadventu.re/img/docs/screen_popup_tiled.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
</div>
|
||||||
|
<div class="col">
|
||||||
|
<img src="https://workadventu.re/img/docs/screen_popup_in_game.png" class="figure-img img-fluid rounded" alt="" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
```
|
||||||
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup
|
||||||
|
```
|
||||||
|
|
||||||
|
* **targetObject**: the name of the rectangle object defined in Tiled.
|
||||||
|
* **message**: the message to display in the popup.
|
||||||
|
* **buttons**: an array of action buttons defined underneath the popup.
|
||||||
|
|
||||||
|
Action buttons are `ButtonDescriptor` objects containing these properties.
|
||||||
|
|
||||||
|
* **label (_string_)**: The label of the button.
|
||||||
|
* **className (_string_)**: The visual type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled".
|
||||||
|
* **callback (_(popup: Popup)=>void_)**: Callback called when the button is pressed.
|
||||||
|
|
||||||
|
Please note that `openPopup` returns an object of the `Popup` class. Also, the callback called when a button is clicked is passed a `Popup` object.
|
||||||
|
|
||||||
|
The `Popup` class that represents an open popup contains a single method: `close()`. This will obviously close the popup when called.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
class Popup {
|
||||||
|
/**
|
||||||
|
* Closes the popup
|
||||||
|
*/
|
||||||
|
close() {};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let helloWorldPopup;
|
||||||
|
|
||||||
|
// Open the popup when we enter a given zone
|
||||||
|
helloWorldPopup = WA.onEnterZone('myZone', () => {
|
||||||
|
WA.openPopup("popupRectangle", 'Hello world!', [{
|
||||||
|
label: "Close",
|
||||||
|
className: "primary",
|
||||||
|
callback: (popup) => {
|
||||||
|
// Close the popup when the "Close" button is pressed.
|
||||||
|
popup.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}]);
|
||||||
|
|
||||||
|
// Close the popup when we leave the zone.
|
||||||
|
WA.onLeaveZone('myZone', () => {
|
||||||
|
helloWorldPopup.close();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Disabling / restoring controls
|
||||||
|
|
||||||
|
```
|
||||||
|
disablePlayerControls(): void
|
||||||
|
restorePlayerControls(): void
|
||||||
|
```
|
||||||
|
|
||||||
|
These 2 methods can be used to completely disable player controls and to enable them again.
|
||||||
|
|
||||||
|
When controls are disabled, the user cannot move anymore using keyboard input. This can be useful in a "First Time User Experience" part, to display an important message to a user before letting him/her move again.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.onEnterZone('myZone', () => {
|
||||||
|
WA.disablePlayerControls();
|
||||||
|
WA.openPopup("popupRectangle", 'This is an imporant message!', [{
|
||||||
|
label: "Got it!",
|
||||||
|
className: "primary",
|
||||||
|
callback: (popup) => {
|
||||||
|
WA.restorePlayerControls();
|
||||||
|
popup.close();
|
||||||
|
}
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Opening a web page in a new tab
|
||||||
|
|
||||||
|
```
|
||||||
|
openTab(url: string): void
|
||||||
|
```
|
||||||
|
|
||||||
|
Opens the webpage at "url" in your browser, in a new tab.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.openTab('https://www.wikipedia.org/');
|
||||||
|
```
|
||||||
|
|
||||||
|
### Opening a web page in the current tab
|
||||||
|
|
||||||
|
```
|
||||||
|
goToPage(url: string): void
|
||||||
|
```
|
||||||
|
|
||||||
|
Opens the webpage at "url" in your browser in place of WorkAdventure. WorkAdventure will be completely unloaded.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.goToPage('https://www.wikipedia.org/');
|
||||||
|
```
|
||||||
|
|
||||||
|
### Opening/closing a web page in an iFrame
|
||||||
|
|
||||||
|
```
|
||||||
|
openCoWebSite(url: string): void
|
||||||
|
closeCoWebSite(): void
|
||||||
|
```
|
||||||
|
|
||||||
|
Opens the webpage at "url" in an iFrame (on the right side of the screen) or close that iFrame.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
WA.openCoWebSite('https://www.wikipedia.org/');
|
||||||
|
// ...
|
||||||
|
WA.closeCoWebSite();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Load a sound from an url
|
||||||
|
|
||||||
|
```
|
||||||
|
loadSound(url: string): Sound
|
||||||
|
```
|
||||||
|
|
||||||
|
Load a sound from an url
|
||||||
|
|
||||||
|
Please note that `loadSound` returns an object of the `Sound` class
|
||||||
|
|
||||||
|
The `Sound` class that represents a loaded sound contains two methods: `play(soundConfig : SoundConfig|undefined)` and `stop()`
|
||||||
|
|
||||||
|
The parameter soundConfig is optional, if you call play without a Sound config the sound will be played with the basic configuration.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var mySound = WA.loadSound("Sound.ogg");
|
||||||
|
var config = {
|
||||||
|
volume : 0.5,
|
||||||
|
loop : false,
|
||||||
|
rate : 1,
|
||||||
|
detune : 1,
|
||||||
|
delay : 0,
|
||||||
|
seek : 0,
|
||||||
|
mute : false
|
||||||
|
}
|
||||||
|
mySound.play(config);
|
||||||
|
// ...
|
||||||
|
mySound.stop();
|
||||||
|
```
|
@ -1,7 +1,58 @@
|
|||||||
export interface IframeEvent {
|
|
||||||
type: string;
|
|
||||||
data: unknown;
|
import { ButtonClickedEvent } from './ButtonClickedEvent';
|
||||||
|
import { ChatEvent } from './ChatEvent';
|
||||||
|
import { ClosePopupEvent } from './ClosePopupEvent';
|
||||||
|
import { EnterLeaveEvent } from './EnterLeaveEvent';
|
||||||
|
import { GoToPageEvent } from './GoToPageEvent';
|
||||||
|
import { OpenCoWebSiteEvent } from './OpenCoWebSiteEvent';
|
||||||
|
import { OpenPopupEvent } from './OpenPopupEvent';
|
||||||
|
import { OpenTabEvent } from './OpenTabEvent';
|
||||||
|
import { UserInputChatEvent } from './UserInputChatEvent';
|
||||||
|
import { LayerEvent } from './LayerEvent';
|
||||||
|
|
||||||
|
|
||||||
|
export interface TypedMessageEvent<T> extends MessageEvent {
|
||||||
|
data: T
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IframeEventMap = {
|
||||||
|
//getState: GameStateEvent,
|
||||||
|
// updateTile: UpdateTileEvent
|
||||||
|
chat: ChatEvent,
|
||||||
|
openPopup: OpenPopupEvent
|
||||||
|
closePopup: ClosePopupEvent
|
||||||
|
openTab: OpenTabEvent
|
||||||
|
goToPage: GoToPageEvent
|
||||||
|
openCoWebSite: OpenCoWebSiteEvent
|
||||||
|
closeCoWebSite: null
|
||||||
|
disablePlayerControl: null
|
||||||
|
restorePlayerControl: null
|
||||||
|
displayBubble: null
|
||||||
|
removeBubble: null
|
||||||
|
showLayer: LayerEvent
|
||||||
|
hideLayer: LayerEvent
|
||||||
|
}
|
||||||
|
export interface IframeEvent<T extends keyof IframeEventMap> {
|
||||||
|
type: T;
|
||||||
|
data: IframeEventMap[T];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export const isIframeEventWrapper = (event: any): event is IframeEvent<keyof IframeEventMap> => typeof event.type === 'string';
|
||||||
|
|
||||||
|
export interface IframeResponseEventMap {
|
||||||
|
userInputChat: UserInputChatEvent
|
||||||
|
enterEvent: EnterLeaveEvent
|
||||||
|
leaveEvent: EnterLeaveEvent
|
||||||
|
buttonClickedEvent: ButtonClickedEvent
|
||||||
|
// gameState: GameStateEvent
|
||||||
|
}
|
||||||
|
export interface IframeResponseEvent<T extends keyof IframeResponseEventMap> {
|
||||||
|
type: T;
|
||||||
|
data: IframeResponseEventMap[T];
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
export const isIframeEventWrapper = (event: any): event is IframeEvent => typeof event.type === 'string';
|
export const isIframeResponseEventWrapper = (event: { type?: string }): event is IframeResponseEvent<keyof IframeResponseEventMap> => typeof event.type === 'string';
|
@ -1,17 +1,17 @@
|
|||||||
import {Subject} from "rxjs";
|
import { Subject } from "rxjs";
|
||||||
import {ChatEvent, isChatEvent} from "./Events/ChatEvent";
|
import { ChatEvent, isChatEvent } from "./Events/ChatEvent";
|
||||||
import {IframeEvent, isIframeEventWrapper} from "./Events/IframeEvent";
|
|
||||||
import {UserInputChatEvent} from "./Events/UserInputChatEvent";
|
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
import {HtmlUtils} from "../WebRtc/HtmlUtils";
|
import { HtmlUtils } from "../WebRtc/HtmlUtils";
|
||||||
import {EnterLeaveEvent} from "./Events/EnterLeaveEvent";
|
import { EnterLeaveEvent } from "./Events/EnterLeaveEvent";
|
||||||
import {isOpenPopupEvent, OpenPopupEvent} from "./Events/OpenPopupEvent";
|
import { isOpenPopupEvent, OpenPopupEvent } from "./Events/OpenPopupEvent";
|
||||||
import {isOpenTabEvent, OpenTabEvent} from "./Events/OpenTabEvent";
|
import { isOpenTabEvent, OpenTabEvent } from "./Events/OpenTabEvent";
|
||||||
import {ButtonClickedEvent} from "./Events/ButtonClickedEvent";
|
import { ButtonClickedEvent } from "./Events/ButtonClickedEvent";
|
||||||
import {ClosePopupEvent, isClosePopupEvent} from "./Events/ClosePopupEvent";
|
import { ClosePopupEvent, isClosePopupEvent } from "./Events/ClosePopupEvent";
|
||||||
import {scriptUtils} from "./ScriptUtils";
|
import { scriptUtils } from "./ScriptUtils";
|
||||||
import {GoToPageEvent, isGoToPageEvent} from "./Events/GoToPageEvent";
|
import { GoToPageEvent, isGoToPageEvent } from "./Events/GoToPageEvent";
|
||||||
import {isOpenCoWebsite, OpenCoWebSiteEvent} from "./Events/OpenCoWebSiteEvent";
|
import { isOpenCoWebsite, OpenCoWebSiteEvent } from "./Events/OpenCoWebSiteEvent";
|
||||||
|
import { IframeEventMap, IframeEvent, IframeResponseEvent, IframeResponseEventMap, isIframeEventWrapper, TypedMessageEvent } from "./Events/IframeEvent";
|
||||||
|
import { UserInputChatEvent } from "./Events/UserInputChatEvent";
|
||||||
import {isLayerEvent, LayerEvent} from "./Events/LayerEvent";
|
import {isLayerEvent, LayerEvent} from "./Events/LayerEvent";
|
||||||
|
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ class IframeListener {
|
|||||||
private readonly scripts = new Map<string, HTMLIFrameElement>();
|
private readonly scripts = new Map<string, HTMLIFrameElement>();
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
window.addEventListener("message", (message) => {
|
window.addEventListener("message", (message: TypedMessageEvent<IframeEvent<keyof IframeEventMap>>) => {
|
||||||
// Do we trust the sender of this message?
|
// Do we trust the sender of this message?
|
||||||
// Let's only accept messages from the iframe that are allowed.
|
// Let's only accept messages from the iframe that are allowed.
|
||||||
// Note: maybe we could restrict on the domain too for additional security (in case the iframe goes to another domain).
|
// Note: maybe we could restrict on the domain too for additional security (in case the iframe goes to another domain).
|
||||||
@ -80,11 +80,9 @@ class IframeListener {
|
|||||||
|
|
||||||
const payload = message.data;
|
const payload = message.data;
|
||||||
if (isIframeEventWrapper(payload)) {
|
if (isIframeEventWrapper(payload)) {
|
||||||
if (payload.type ==='showLayer' && isLayerEvent(payload.data)) {
|
if (payload.type === 'showLayer' && isLayerEvent(payload.data)) {
|
||||||
console.log('showLayer 2');
|
|
||||||
this._showLayerStream.next(payload.data);
|
this._showLayerStream.next(payload.data);
|
||||||
} else if (payload.type === 'hideLayer' && isLayerEvent(payload.data)) {
|
} else if (payload.type === 'hideLayer' && isLayerEvent(payload.data)) {
|
||||||
console.log('hideLayer 2');
|
|
||||||
this._hideLayerStream.next(payload.data);
|
this._hideLayerStream.next(payload.data);
|
||||||
} else if (payload.type === 'chat' && isChatEvent(payload.data)) {
|
} else if (payload.type === 'chat' && isChatEvent(payload.data)) {
|
||||||
this._chatStream.next(payload.data);
|
this._chatStream.next(payload.data);
|
||||||
@ -93,10 +91,10 @@ class IframeListener {
|
|||||||
} else if (payload.type === 'closePopup' && isClosePopupEvent(payload.data)) {
|
} else if (payload.type === 'closePopup' && isClosePopupEvent(payload.data)) {
|
||||||
this._closePopupStream.next(payload.data);
|
this._closePopupStream.next(payload.data);
|
||||||
}
|
}
|
||||||
else if(payload.type === 'openTab' && isOpenTabEvent(payload.data)) {
|
else if (payload.type === 'openTab' && isOpenTabEvent(payload.data)) {
|
||||||
scriptUtils.openTab(payload.data.url);
|
scriptUtils.openTab(payload.data.url);
|
||||||
}
|
}
|
||||||
else if(payload.type === 'goToPage' && isGoToPageEvent(payload.data)) {
|
else if (payload.type === 'goToPage' && isGoToPageEvent(payload.data)) {
|
||||||
scriptUtils.goToPage(payload.data.url);
|
scriptUtils.goToPage(payload.data.url);
|
||||||
}
|
}
|
||||||
else if (payload.type === 'openCoWebSite' && isOpenCoWebsite(payload.data)) {
|
else if (payload.type === 'openCoWebSite' && isOpenCoWebsite(payload.data)) {
|
||||||
@ -106,19 +104,19 @@ class IframeListener {
|
|||||||
|
|
||||||
scriptUtils.openCoWebsite(payload.data.url, scriptUrl || foundSrc);
|
scriptUtils.openCoWebsite(payload.data.url, scriptUrl || foundSrc);
|
||||||
}
|
}
|
||||||
else if(payload.type === 'closeCoWebSite') {
|
else if (payload.type === 'closeCoWebSite') {
|
||||||
scriptUtils.closeCoWebSite();
|
scriptUtils.closeCoWebSite();
|
||||||
}
|
}
|
||||||
else if (payload.type === 'disablePlayerControl'){
|
else if (payload.type === 'disablePlayerControl') {
|
||||||
this._disablePlayerControlStream.next();
|
this._disablePlayerControlStream.next();
|
||||||
}
|
}
|
||||||
else if (payload.type === 'restorePlayerControl'){
|
else if (payload.type === 'restorePlayerControl') {
|
||||||
this._enablePlayerControlStream.next();
|
this._enablePlayerControlStream.next();
|
||||||
}
|
}
|
||||||
else if (payload.type === 'displayBubble'){
|
else if (payload.type === 'displayBubble') {
|
||||||
this._displayBubbleStream.next();
|
this._displayBubbleStream.next();
|
||||||
}
|
}
|
||||||
else if (payload.type === 'removeBubble'){
|
else if (payload.type === 'removeBubble') {
|
||||||
this._removeBubbleStream.next();
|
this._removeBubbleStream.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,7 +145,7 @@ class IframeListener {
|
|||||||
const iframe = document.createElement('iframe');
|
const iframe = document.createElement('iframe');
|
||||||
iframe.id = this.getIFrameId(scriptUrl);
|
iframe.id = this.getIFrameId(scriptUrl);
|
||||||
iframe.style.display = 'none';
|
iframe.style.display = 'none';
|
||||||
iframe.src = '/iframe.html?script='+encodeURIComponent(scriptUrl);
|
iframe.src = '/iframe.html?script=' + encodeURIComponent(scriptUrl);
|
||||||
|
|
||||||
// We are putting a sandbox on this script because it will run in the same domain as the main website.
|
// We are putting a sandbox on this script because it will run in the same domain as the main website.
|
||||||
iframe.sandbox.add('allow-scripts');
|
iframe.sandbox.add('allow-scripts');
|
||||||
@ -171,8 +169,8 @@ class IframeListener {
|
|||||||
'\n' +
|
'\n' +
|
||||||
'<html lang="en">\n' +
|
'<html lang="en">\n' +
|
||||||
'<head>\n' +
|
'<head>\n' +
|
||||||
'<script src="'+window.location.protocol+'//'+window.location.host+'/iframe_api.js" ></script>\n' +
|
'<script src="' + window.location.protocol + '//' + window.location.host + '/iframe_api.js" ></script>\n' +
|
||||||
'<script src="'+scriptUrl+'" ></script>\n' +
|
'<script src="' + scriptUrl + '" ></script>\n' +
|
||||||
'</head>\n' +
|
'</head>\n' +
|
||||||
'</html>\n';
|
'</html>\n';
|
||||||
|
|
||||||
@ -189,14 +187,14 @@ class IframeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getIFrameId(scriptUrl: string): string {
|
private getIFrameId(scriptUrl: string): string {
|
||||||
return 'script'+crypto.createHash('md5').update(scriptUrl).digest("hex");
|
return 'script' + crypto.createHash('md5').update(scriptUrl).digest("hex");
|
||||||
}
|
}
|
||||||
|
|
||||||
unregisterScript(scriptUrl: string): void {
|
unregisterScript(scriptUrl: string): void {
|
||||||
const iFrameId = this.getIFrameId(scriptUrl);
|
const iFrameId = this.getIFrameId(scriptUrl);
|
||||||
const iframe = HtmlUtils.getElementByIdOrFail<HTMLIFrameElement>(iFrameId);
|
const iframe = HtmlUtils.getElementByIdOrFail<HTMLIFrameElement>(iFrameId);
|
||||||
if (!iframe) {
|
if (!iframe) {
|
||||||
throw new Error('Unknown iframe for script "'+scriptUrl+'"');
|
throw new Error('Unknown iframe for script "' + scriptUrl + '"');
|
||||||
}
|
}
|
||||||
this.unregisterIframe(iframe);
|
this.unregisterIframe(iframe);
|
||||||
iframe.remove();
|
iframe.remove();
|
||||||
@ -244,7 +242,7 @@ class IframeListener {
|
|||||||
/**
|
/**
|
||||||
* Sends the message... to all allowed iframes.
|
* Sends the message... to all allowed iframes.
|
||||||
*/
|
*/
|
||||||
private postMessage(message: IframeEvent) {
|
private postMessage(message: IframeResponseEvent<keyof IframeResponseEventMap>) {
|
||||||
for (const iframe of this.iframes) {
|
for (const iframe of this.iframes) {
|
||||||
iframe.contentWindow?.postMessage(message, '*');
|
iframe.contentWindow?.postMessage(message, '*');
|
||||||
}
|
}
|
||||||
|
@ -877,19 +877,16 @@ ${escapedMessage}
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
this.iframeSubscriptionList.push(iframeListener.showLayerStream.subscribe((layerEvent)=>{
|
this.iframeSubscriptionList.push(iframeListener.showLayerStream.subscribe((layerEvent)=>{
|
||||||
console.log('showLayer 3');
|
|
||||||
this.setLayerVisibility(layerEvent.name, true);
|
this.setLayerVisibility(layerEvent.name, true);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.iframeSubscriptionList.push(iframeListener.hideLayerStream.subscribe((layerEvent)=>{
|
this.iframeSubscriptionList.push(iframeListener.hideLayerStream.subscribe((layerEvent)=>{
|
||||||
console.log('hideLayer 3');
|
|
||||||
this.setLayerVisibility(layerEvent.name, false);
|
this.setLayerVisibility(layerEvent.name, false);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private setLayerVisibility(layerName: string, visible: boolean): void {
|
private setLayerVisibility(layerName: string, visible: boolean): void {
|
||||||
console.log('visibility');
|
|
||||||
const layer = this.Layers.find((layer) => layer.layer.name === layerName);
|
const layer = this.Layers.find((layer) => layer.layer.name === layerName);
|
||||||
if (layer === undefined) {
|
if (layer === undefined) {
|
||||||
console.warn('Could not find layer "' + layerName + '" when calling WA.hideLayer / WA.showLayer');
|
console.warn('Could not find layer "' + layerName + '" when calling WA.hideLayer / WA.showLayer');
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
import {ChatEvent, isChatEvent} from "./Api/Events/ChatEvent";
|
import { ChatEvent } from "./Api/Events/ChatEvent";
|
||||||
import {isIframeEventWrapper} from "./Api/Events/IframeEvent";
|
import { isIframeResponseEventWrapper } from "./Api/Events/IframeEvent";
|
||||||
import {isUserInputChatEvent, UserInputChatEvent} from "./Api/Events/UserInputChatEvent";
|
import { isUserInputChatEvent, UserInputChatEvent } from "./Api/Events/UserInputChatEvent";
|
||||||
import {Subject} from "rxjs";
|
import { Subject } from "rxjs";
|
||||||
import {EnterLeaveEvent, isEnterLeaveEvent} from "./Api/Events/EnterLeaveEvent";
|
import { EnterLeaveEvent, isEnterLeaveEvent } from "./Api/Events/EnterLeaveEvent";
|
||||||
import {OpenPopupEvent} from "./Api/Events/OpenPopupEvent";
|
import { OpenPopupEvent } from "./Api/Events/OpenPopupEvent";
|
||||||
import {isButtonClickedEvent} from "./Api/Events/ButtonClickedEvent";
|
import { isButtonClickedEvent } from "./Api/Events/ButtonClickedEvent";
|
||||||
import {ClosePopupEvent} from "./Api/Events/ClosePopupEvent";
|
import { ClosePopupEvent } from "./Api/Events/ClosePopupEvent";
|
||||||
import {OpenTabEvent} from "./Api/Events/OpenTabEvent";
|
import { OpenTabEvent } from "./Api/Events/OpenTabEvent";
|
||||||
import {GoToPageEvent} from "./Api/Events/GoToPageEvent";
|
import { GoToPageEvent } from "./Api/Events/GoToPageEvent";
|
||||||
import {OpenCoWebSiteEvent} from "./Api/Events/OpenCoWebSiteEvent";
|
import { OpenCoWebSiteEvent } from "./Api/Events/OpenCoWebSiteEvent";
|
||||||
import {LayerEvent} from "./Api/Events/LayerEvent";
|
import {LayerEvent} from "./Api/Events/LayerEvent";
|
||||||
|
|
||||||
interface WorkAdventureApi {
|
interface WorkAdventureApi {
|
||||||
@ -53,7 +53,7 @@ interface ButtonDescriptor {
|
|||||||
/**
|
/**
|
||||||
* The type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled"
|
* The type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled"
|
||||||
*/
|
*/
|
||||||
className?: "normal"|"primary"|"success"|"warning"|"error"|"disabled",
|
className?: "normal" | "primary" | "success" | "warning" | "error" | "disabled",
|
||||||
/**
|
/**
|
||||||
* Callback called if the button is pressed
|
* Callback called if the button is pressed
|
||||||
*/
|
*/
|
||||||
@ -92,7 +92,6 @@ window.WA = {
|
|||||||
}, '*');
|
}, '*');
|
||||||
},
|
},
|
||||||
showLayer(layer: string) : void {
|
showLayer(layer: string) : void {
|
||||||
console.log('showLayer');
|
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
'type' : 'showLayer',
|
'type' : 'showLayer',
|
||||||
'data' : {
|
'data' : {
|
||||||
@ -101,7 +100,6 @@ window.WA = {
|
|||||||
}, '*');
|
}, '*');
|
||||||
},
|
},
|
||||||
hideLayer(layer: string) : void {
|
hideLayer(layer: string) : void {
|
||||||
console.log('hideLayer');
|
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
'type' : 'hideLayer',
|
'type' : 'hideLayer',
|
||||||
'data' : {
|
'data' : {
|
||||||
@ -109,38 +107,38 @@ window.WA = {
|
|||||||
} as LayerEvent
|
} as LayerEvent
|
||||||
}, '*');
|
}, '*');
|
||||||
},
|
},
|
||||||
disablePlayerControl() : void {
|
disablePlayerControl(): void {
|
||||||
window.parent.postMessage({'type' : 'disablePlayerControl'},'*');
|
window.parent.postMessage({ 'type': 'disablePlayerControl' }, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
restorePlayerControl() : void {
|
restorePlayerControl(): void {
|
||||||
window.parent.postMessage({'type' : 'restorePlayerControl'},'*');
|
window.parent.postMessage({ 'type': 'restorePlayerControl' }, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
displayBubble() : void {
|
displayBubble(): void {
|
||||||
window.parent.postMessage({'type' : 'displayBubble'},'*');
|
window.parent.postMessage({ 'type': 'displayBubble' }, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
removeBubble() : void {
|
removeBubble(): void {
|
||||||
window.parent.postMessage({'type' : 'removeBubble'},'*');
|
window.parent.postMessage({ 'type': 'removeBubble' }, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
openTab(url : string) : void{
|
openTab(url: string): void {
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
"type" : 'openTab',
|
"type": 'openTab',
|
||||||
"data" : {
|
"data": {
|
||||||
url
|
url
|
||||||
} as OpenTabEvent
|
} as OpenTabEvent
|
||||||
},'*');
|
}, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
goToPage(url : string) : void{
|
goToPage(url: string): void {
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
"type" : 'goToPage',
|
"type": 'goToPage',
|
||||||
"data" : {
|
"data": {
|
||||||
url
|
url
|
||||||
} as GoToPageEvent
|
} as GoToPageEvent
|
||||||
},'*');
|
}, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
openCoWebSite(url : string) : void{
|
openCoWebSite(url : string) : void{
|
||||||
@ -149,13 +147,13 @@ window.WA = {
|
|||||||
"data" : {
|
"data" : {
|
||||||
url
|
url
|
||||||
} as OpenCoWebSiteEvent
|
} as OpenCoWebSiteEvent
|
||||||
},'*');
|
}, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
closeCoWebSite() : void{
|
closeCoWebSite(): void {
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
"type" : 'closeCoWebSite'
|
"type": 'closeCoWebSite'
|
||||||
},'*');
|
}, '*');
|
||||||
},
|
},
|
||||||
|
|
||||||
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup {
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup {
|
||||||
@ -226,9 +224,9 @@ window.addEventListener('message', message => {
|
|||||||
|
|
||||||
const payload = message.data;
|
const payload = message.data;
|
||||||
|
|
||||||
console.log(payload);
|
console.debug(payload);
|
||||||
|
|
||||||
if (isIframeEventWrapper(payload)) {
|
if (isIframeResponseEventWrapper(payload)) {
|
||||||
const payloadData = payload.data;
|
const payloadData = payload.data;
|
||||||
if (payload.type === 'userInputChat' && isUserInputChatEvent(payloadData)) {
|
if (payload.type === 'userInputChat' && isUserInputChatEvent(payloadData)) {
|
||||||
userInputChatStream.next(payloadData);
|
userInputChatStream.next(payloadData);
|
||||||
@ -240,7 +238,7 @@ window.addEventListener('message', message => {
|
|||||||
const callback = popupCallbacks.get(payloadData.popupId)?.get(payloadData.buttonId);
|
const callback = popupCallbacks.get(payloadData.popupId)?.get(payloadData.buttonId);
|
||||||
const popup = popups.get(payloadData.popupId);
|
const popup = popups.get(payloadData.popupId);
|
||||||
if (popup === undefined) {
|
if (popup === undefined) {
|
||||||
throw new Error('Could not find popup with ID "'+payloadData.popupId+'"');
|
throw new Error('Could not find popup with ID "' + payloadData.popupId + '"');
|
||||||
}
|
}
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback(popup);
|
callback(popup);
|
||||||
|
@ -36,19 +36,5 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<!--<button id="showLayer">Show Layer</button>
|
|
||||||
<script>
|
|
||||||
document.getElementById('showLayer').onclick = () => {
|
|
||||||
WA.showLayer('Metadata');
|
|
||||||
console.log('show');
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
<button id="hideLayer">Hide Layer</button>
|
|
||||||
<script>
|
|
||||||
document.getElementById('hideLayer').onclick = () => {
|
|
||||||
WA.hideLayer('Metadata');
|
|
||||||
console.log('hide');
|
|
||||||
}
|
|
||||||
</script>-->
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user