Skip to content

echo008/iframe-rpc-util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iframe-rpc-util

npm

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)
})

If you like it, please star it

About

iframe rpc postmessage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published