How to return Javascript value to main process?


#1

I am very new to Electron, just tried to follow the Documentation.
I was able to run a html in Electron and it worked fine.

But I want to get value from JavaScript execude code inside Electron. main process. Is it possible?

My main is this index.js here:

var app = require ('app')
var BrowserWindow = require ('browser-window')
app.on('ready', function(){
    var mainWindow = new BrowserWindow({
        width:800,
        height:600
    })
    mainWindow.loadUrl('https://html5test.com/')
    var script = 'console.log("clientWidth: " + document.body.clientWidth)' 
    // OK, display the width of the visible area
    mainWindow.webContents.executeJavaScript(script)
    // How to get this value in main process?
    var clientWidth = ??????????
})

#2

You should see the IPC section of the documentation. It’s exactly what you’re looking for.


#3

You could use ‘ipc’ module, you can see an example here


#4

You can use screen module to get information about the available displays. It works in both the main and renderer processes.