WebContents.send vs webContents.executeJavaScript
In my web application it is used ipcRender.send
to ask a listening electron ipcMain.on
for a value from the system. The process is asynchronous, so as soon as an electron receives a value from the system, it then accesses the application. What is a cleaner approach to value transfer? Which approach should be used and why?
webContents.send
listen to the event
Appendixwindow.myFunction = data => setState(data)
ipcRenderer.on('my-function', (ev, data) => window.myFunction(data));
electron
mainWindow.webContents.send('my-function', value)
webContents.executeJavaScript
function call
Appendixwindow.myFunction = data => setState(data)
electron
mainWindow.webContents.executeJavaScript(`myFunction(${data});`)
source to share
I think the main difference is that it is ipcRenderer.on
more flexible and scalable as it allows you to use a module ipcRenderer
that is an instance EventEmitter
. He can add, remove listeners (subscribers). It also allows you to send both synchronous and asynchronous messages.
webContents.executeJavaScript
from what I've found just allows you to remove restrictions from some HTML API methods that can only be invoked with a user gesture.
For example requestFullScreen
:
webContents.executeJavaScript(code[, userGesture])
Setting userGesture
to not true
will remove this limitation.
source to share