1.0.1 • Published 2 years ago

framebus-sw v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Framebus-sw

Framebus-sw, a forked project from Framebus 5.1.2, allows you to easily send messages across frames (and iframes) with with either broadcasting to all or only to the especified frame.

Usage references: Framebus (https://github.com/braintree/framebus)

Change

Added broadcastMode to FramebusOptions.

Option broadcastMode decided which window the message will be post to. Options such self, parent, children will verify window origin with target origin before post message, so that can avoid the error throw from postMessage when it does not.

type FramebusOptions = {
  origin?: string, // default: "*"
  broadcastMode?: string, // default "all
  channel?: string, // no default
  verifyDomain?: (url: string) => boolean, // no default
};

If Verify Origin is No, it will throw error to web console when window origin does not match with target origin.

OptionsVerify OriginDescription
allNoBroadcast the message to all iframes from the top to down.
selfYesOnly post the message back to window itself
parentYesOnly post the message to the window.parent.
topNoOnly post the message to window.top.
childrenYesPost to all accessible iframe in the window excluded the window itself.

Examples

    const Framebus = require('framebus-sw');

    // default bus instance
    const bus = new Framebus();
    const defaultBus = new Framebus({ broadcastMode: 'all' });

    // bus instance only posts messages to itself
    const selfBus = new Framebus({
        origin: location.origin,
        broadcastMode: 'self'
    });

    // bus instance only posts messages to its verified parent
    const parentBus = new Framebus({
        origin: 'http://parent_origin.com'
        broadcastMode: 'parent'
    });

    // bus instance only posts messages to the verified children
    const childrenBus = new Framebus({
        origin: 'http://children_1.com'
        broadcastMode: 'children'
    });