1.1.1 • Published 8 months ago

iframe-message-bridge v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

iframe-message-bridge

Version npm

A small tool for communicating between window and iframe.

Usage

Basic

npm install iframe-message-bridge

Example

// top
import { Bridge } from 'iframe-bridge-promised';

const iframe = document.getElementById('iframe');

const bridge = new Bridge(iframe.contentWindow);

bridge.on('ready', () => {
  // basic
  bridge.post('say');
  // async
  bridge.post('delay').then(() => {
    console.log('complete');
  });
  // with payload
  bridge
    .post('greet', {
      name: 'John',
    })
    .then((response) => {
      // Vivian
      console.log(response.name);
    });
});
// iframe
import { Bridge } from 'iframe-bridge-promised';

const bridge = new Bridge(window.parent);

bridge.post('ready');

bridge.on('say', () => {
  console.log('Hello');
});

bridge.on('delay', () => {
  return Promise((resolve) => {
    setTimeout(resolve, 2000);
  });
});

bridge.on('greet', (payload) => {
  console.log(payload.name); // John
  return Promise((resolve) => {
    setTimeout(() => {
      resolve({
        name: 'Vivian',
      });
    }, 2000);
  });
});

Options

targetWindow required

The target window object.

prefix default: 'iframe-message-bridge'

The prefix of event name. only used internally to prevent irrelevant postMessage event.

timeout default: 20000

The timeout of post event promise.

targetOrigin default: '*'

see postMessage

transfer default: undefined

see postMessage

1.1.1

8 months ago

1.1.1-alpha.1

8 months ago

1.1.0

8 months ago

1.1.0-alpha.2

8 months ago

1.1.0-alpha.1

8 months ago

1.0.4

8 months ago

1.0.4-alpha.1

8 months ago

1.0.3-alpha.4

8 months ago

1.0.3-alpha.3

8 months ago

1.0.3-alpha.2

8 months ago

1.0.3-alpha.1

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.0-alpha.5

8 months ago

1.0.1

8 months ago

1.0.0-alpha.4

8 months ago

1.0.0

8 months ago

1.0.0-alpha.3

8 months ago

1.0.0-alpha.2

8 months ago

1.0.0-alpha.1

8 months ago

1.0.0-alpha.0

8 months ago