How to communicate event listening between webview and main process rendering process in Electron
It is believed that many inexperienced people are at a loss about how webview in Electron communicates with the main process rendering process for event monitoring. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Webview calls the load page method to send via .executeJavaScript (code [, userGesture, callback]) or webview.send (), while listening with ipcRenderer.on () on the guest page
Attention, attention, attention:
If the guest page needs to call the method of the page where the webview resides, you need to add the attribute nodeintegration= "true" to the webview
The guest page (the resource page loaded by webview) is the page specified by src in webview (browser page)
If (window.require ('electron')) {let ipcRenderer = window.require (' electron'). IpcRenderer; ipcRenderer & & ipcRenderer.on ('webmsg', (e, msg) = > {console.log (message received by msg,'');}); ipcRenderer & & ipcRenderer.sendToHost ('I have received the message');}
Page where webview is located (client page)
Let webview = document.getElementById ('test')
Webview.addEventListener ('ipc-message', (event) = > {/ / ipc-message listening, information from the page loaded by webview
Console.log (event.channel) / / finally receive the message output sub-page information
})
If the setting does not take effect, please confirm here whether the property of nodeintegration= "true" is set on the webview tag.
After reading the above, have you mastered the method of event listening communication between webview and the main process rendering process in Electron? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!