0.3.1 • Published 5 years ago
cross-origin-courier v0.3.1
This project is under active development.
Until major version released, the API is a subject to change.
cross-origin-courier
A wrapper over Window.postMessage() and MessageChannel to facilitate messaging between cross origin contexts.
The library exposes a connection function that returns a Promise. On a successful connection, the Promise is resolved with the courier object to be used to send and listen to messages. Note: the child frame should call the connection function first.
Setup
NPM
npm i cross-origin-courierimport connect from 'cross-origin-courier';
// ...
connect().then(courier => {
courier.listen(handler);
courier.send({ answer: 42 });
});
function handler(data) {
// do your stuff
}CDN
<script src="https://unpkg.com/cross-origin-courier"></script>Loaded this way, the connection function is exposed under the following long-ish name
window.createCrossOrigConnection().then(courier => {
// ...
});Options
const options = {
passphrase: 'foo-bar-baz-qux',
isParent: true,
targetOrigin: '*',
};
createConnection(options).then(courier => {
// ...
});| Option | Type | Default | Description |
|---|---|---|---|
passphrase | string | 'DEFAULT_PASSPHRASE' | todo |
targetOrigin | string | '*' | todo |
isParent | boolean | false | todo |
Courier API
| Method | Arguments | Description |
|---|---|---|
send | data<any> | Sends data to the counterparty |
listen | dataHandler<(data: any) => void> | Sets a handler for the counterparty messages |
Documentation
Please find the full docs here
License
Copyright © 2021, Sergey Chernykh. Released under the MIT License.