I would like to get data from one process to another in electron and I can't figure out how to do this. I have the following code:
// I create a new browser window to load url
var win = new BrowserWindow({ width: 800, height: 600, show: false });
win.loadURL('chrome://gpu');
win.webContents.on('dom-ready', function() {
console.log("dom is ready");
});
// Here I want to get content of the loaded page and log it.
I tried ipc, but I can figure out how to use it.
I would like to get data from one process to another in electron and I can't figure out how to do this. I have the following code:
// I create a new browser window to load url
var win = new BrowserWindow({ width: 800, height: 600, show: false });
win.loadURL('chrome://gpu');
win.webContents.on('dom-ready', function() {
console.log("dom is ready");
});
// Here I want to get content of the loaded page and log it.
I tried ipc, but I can figure out how to use it.
Share Improve this question edited Jun 11, 2020 at 6:54 frogatto 29.3k13 gold badges89 silver badges134 bronze badges asked Jan 26, 2016 at 16:45 Bill LumbertBill Lumbert 5,0134 gold badges22 silver badges30 bronze badges1 Answer
Reset to default 13If you only want to log the contents you can write them to the Main process stdout directly using Electron's remote.process
directly from the Renderer, but if you want to send the contents to the Main process IPC is probably the best way (you could also use files, sockets etc.).
Here is a very quick example of how you could do this all from your main.js file (but I'd suggest to use a separate file for the Renderer code and require it using BrowserWindow's 'preload' option, this is just for illustrative purposes).
var electron = require('electron');
var ipc = electron.ipcMain;
var BrowserWindow = electron.BrowserWindow;
var win = new BrowserWindow({ width: 800, height: 600, show: false });
win.webContents.on('dom-ready', () => {
win.webContents.executeJavaScript(`
require('electron').ipcRenderer.send('gpu', document.body.innerHTML);
`);
});
ipc.on('gpu', (_, gpu) => {
console.log(gpu)
})
win.loadURL('chrome://gpu');