I have a <webview>
in my Electron app. I'd like to have safe "foreign" communication, the way I would with an iframe
through postMessage
. So for example:
webview.executeJavaScript("window.parent.postMessage('all done!')");
Is my only choice for communication with this subwebview to turn on nodeIntegration
so that I can use sendToHost
? Turning on all of nodeIntegration
just for this one feature seems like overkill.
You can access Electron APIs in the
webview
preload script, including IPC, even whennodeIntegration
is disabled. Your preload script can inject functions into the global namespace that will then be accessible within the page loaded in thewebview
. A simple example:webview-preload.js
:webview-index.html
:window-index.html
: