postis-plus
v1.0.1
Published
a extend about postis of postMessage
Downloads
0
Maintainers
Readme
postis-plus
the package base on postis.
Install
npm i postis-plus --save
Usage
Parent HTML which contains an iframe:
import postisPlus from "postis-plus";
const targetWindow = document.querySelectorAll("iframe")[0].contentWindow;
const transport = postisPlus({
window: targetWindow,
scope: "scope-for-message-changing-to-avoid-overlapping"
});
// addListener event of 'event'
transport.on('event', ({ name, data }) => {
switch (name) {
case 'eventName':
console.log(data)
break
}
})
// addListener event of 'resolve'
transport.on('resolve', ({ name, data }, handler) => {
switch (name) {
case 'eventName':
console.log(data)
...
handler(res)
break
}
})
In embedded child iframe:
import postisPlus from "postis-plus";
const targetWindow = window.parent;
const transport = postisPlus({
window: targetWindow,
scope: "scope-for-message-changing-to-avoid-overlapping"
});
// emit event of 'event'
transport.sendEvent({
name: 'eventName',
data: data
})
// emit event of 'resolve'
transport.sendResolve({ name: 'eventName',data: data })
.then((res) => {
console.log(res)
})