1.0.0 • Published 1 year ago

@with/hey v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@with/hey

npm version npm downloads

Install

npm i @with/hey

Usage

In your parent window

import hey from '@with/hey'

const connection = hey({
  isParent: true,
  remoteWindow: iframe.contentWindow,
  remoteOrigin: '*',
  methods: {
    helloFromParent( message ) {
      return 'pong from parent'
    }
  }
})

// will wait until child emit `ready` event
await connection.waitFor( 'ready' )

await connection.callRemote( 'helloFromChild', 'ping' ) // -> pong

In your remote window(most likely a child iframe)

import hey from '@with/hey'

const connection = hey({
  remoteWindow: window.parent,
  remoteOrigin: '*',
  methods: {
    hello( message ) {
      return 'world'
    }
  }
})

await connection.setMethod( 'helloFromChild', function helloFromChild( message ) {
  return 'pong'
} )

connection.emit( 'ready' )

await connection.callRemote( 'helloFromParent', 'ping' ) // -> pong from parent

Modify remoteOrigin option for security purpose if needed

License

MIT © fengzilong

1.0.0

1 year ago