MessageHandler

MessageHandler

Simple postMessage handler

Constructor

new MessageHandler()

Create new postMessage handler

Example
new MessageHandler().addListener(data => console.log(data)).listen()

Methods

addListener(f) → {this}

Add a callback function to call on all received message

Parameters
Name Type Description
f function

Function to add to the callback list

Returns
Type
this

listen() → {this}

Start the messageHandler listener

Returns
Type
this

logMessages(f) → {this}

Change the logger function, by default the message are printed in the console

Parameters
Name Type Description
f function

The new log function

Returns
Type
this

removeListener(f) → {this}

Remove a callback function

Parameters
Name Type Description
f function

Function to remove from the callback list

Returns
Type
this

stop() → {this}

Stop the messageHandler listener

Returns
Type
this

waitForMessage(filteropt) → {Promise.<String>}

Return a promise that resolve with the next message data.

Example
const mh = new MessageHandler().listen()
const data = await mh.waitForMessage((data, origin) => origin == "https://victim.example")
console.log(data) 
Parameters
Name Type Attributes Description
filter function <optional>

A filter function, taking data and origin as parameter and returning a boolean whether or not resolving to the message

Returns

message data

Type
Promise.<String>