iframe-rpc-util
v1.0.4
Published
RPC between cross-origin iframe using postMessage
Downloads
5
Maintainers
Readme
iframe-rpc-util
RPC between cross-origin iframe using postMessage
Installation
$ npm install iframe-rpc-util
$ yarn add iframe-rpc-util
$ pnpm install iframe-rpc-util
Usage
Parent Iframe
<iframe src="xx" id="iframeNode"></iframe>
import iframeRpc from 'iframe-rpc-util'
// init rpc instance
const iframeProxy = iframeRpc('iframeNode')
// iframe init event
iframeProxy.onReady = (data) => {
// get rpc register function
iframeProxy.getKeys().then((data) => {
console.log('parent getKeys', data)
})
// call children function
iframeProxy.childrenFunc('parent params').then((data) => {
console.log('parent', data)
})
}
// register function
iframeProxy.parentFunc = (data) => {
// return remote data
return 'parent function'
}
Children Iframe
import iframeRpc from 'iframe-rpc-util'
const iframeProxy = iframeRpc()
// register function
iframeProxy.childrenFunc = (data) => {
// return remote data
return 'children function'
}
iframeProxy.parentFunc('children params').then((data) => {
console.log('children', data)
})