-
Notifications
You must be signed in to change notification settings - Fork 6
/
epm.js
43 lines (38 loc) · 1.12 KB
/
epm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module.exports = function installPostMessage (options) {
options = options || {}
options.channelName = options.channelName || '__postMessage'
// outside of webview
if (options.webview) {
var proxyIframe = document.createElement('iframe')
document.body.appendChild(proxyIframe)
proxyIframe.contentWindow.postMessage = function (msgStr) {
options.webview.send(options.channelName, msgStr)
}
options.webview.addEventListener('ipc-message', function (event) {
if (event.channel === options.channelName) {
var msgStr = event.args[0].data
dispatch(msgStr, proxyIframe.contentWindow)
}
})
return proxyIframe
}
// inside of webview
window.parent.postMessage = function (msgStr) {
ipc.sendToHost(options.channelName, {
data: msgStr
})
}
ipc.on(options.channelName, function (msgStr) {
dispatch(msgStr, window.parent)
})
}
function dispatch (msgStr, source) {
var message = new MessageEvent('message', {
view: window.parent,
bubbles: false,
cancelable: false,
data: msgStr,
source: source
})
window.dispatchEvent(message)
}