Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate with <webview> in Electron

Tags:

electron

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.

like image 745
Francisco Ryan Tolmasky I Avatar asked Sep 11 '16 22:09

Francisco Ryan Tolmasky I


People also ask

How to use webview in Electron?

Use the webview tag to embed 'guest' content (such as web pages) in your Electron app. The guest content is contained within the webview container. An embedded page within your app controls how the guest content is laid out and rendered. Unlike an iframe , the webview runs in a separate process than your app.

What is Nodeintegration Electron?

Electron node integration refers to the ability of accessing Node. js resources from within the “renderer” thread (the UI). It is enabled by default in Quasar CLI, although Electron is encouraging developers to turn it off as a security precaution. As of “@quasar/app” v1. 3+, you can turn off the node integration.

What is BrowserView?

A BrowserView can be used to embed additional web content into a BrowserWindow . It is like a child window, except that it is positioned relative to its owning window. It is meant to be an alternative to the webview tag.

What is Webview tag?

Use the webview tag to actively load live content from the web over the network and embed it in your Chrome App. Your app can control the appearance of the webview and interact with the web content, initiate navigations in an embedded web page, react to error events that happen within it, and more (see Usage). webview.


2 Answers

Easiest way
Communication is

Note: (main.js or app.js or background.js or process.js ) no need to pass (directly pass component to component),i succesffully implemented in electron:3.1.10 for print html webview.

Window To Webview

example1.html

    <webview id="paper" style="width:300px;height:800px" src="file:///static/mywebview.html" nodeintegration></webview>

example1.js

 var webview = document.getElementById("paper");
  webview.send("ping",data); 

getting data from mycomponent or window(i send directly form component)

mywebview.html

<!---what data you want show----!>

mywebview.js

    const {
        ipcRenderer
    } = require('electron')                                                                            
  //data from window                                                                   
    ipcRenderer.on('ping', (e, data) => { console.log(data) })

webview to window

Webview to window(direct pass to component)

mywebview.js

 ipcRenderer.sendToHost("readyCompanyInfo",data) 

in my window eg i use vue (mycomponent.vue or mypage)

example1.html

const ipcRenderer = require("electron").ipcRenderer;
webview.addEventListener("ipc-message",(event)=>{

const {args,channel}=event;

if(channel=="readyCompanyInfo")
{
console.log(channel,args)
//here you can see data what u passed from webview to window
console.log(args[0])

}
})
like image 175
ßãlãjî Avatar answered Oct 21 '22 14:10

ßãlãjî


You can access Electron APIs in the webview preload script, including IPC, even when nodeIntegration is disabled. Your preload script can inject functions into the global namespace that will then be accessible within the page loaded in the webview. A simple example:

webview-preload.js:

const { ipcRenderer } = require('electron')    

global.pingHost = () => {
  ipcRenderer.sendToHost('ping')
}

webview-index.html:

<script>
  pingHost()
</script>

window-index.html:

<script>
  const webview = document.getElementById('mywebview')
  webview.addEventListener('ipc-message', event => {
    // prints "ping"
    console.log(event.channel)
  })
</script>
like image 36
Vadim Macagon Avatar answered Oct 21 '22 16:10

Vadim Macagon