showing results for - "ipcrenderer main js"
Lucia
21 Aug 2018
1const {
2  app,
3  BrowserWindow,
4  ipcMain
5} = require("electron");
6const path = require("path");
7const fs = require("fs");
8
9// Keep a global reference of the window object, if you don't, the window will
10// be closed automatically when the JavaScript object is garbage collected.
11let win;
12
13async function createWindow() {
14
15  // Create the browser window.
16  win = new BrowserWindow({
17    width: 800,
18    height: 600,
19    webPreferences: {
20      nodeIntegration: false, // is default value after Electron v5
21      contextIsolation: true, // protect against prototype pollution
22      enableRemoteModule: false, // turn off remote
23      preload: path.join(__dirname, "preload.js") // use a preload script
24    }
25  });
26
27  // Load app
28  win.loadFile(path.join(__dirname, "dist/index.html"));
29
30  // rest of code..
31}
32
33app.on("ready", createWindow);
34
35ipcMain.on("toMain", (event, args) => {
36  fs.readFile("path/to/file", (error, data) => {
37    // Do something with file contents
38
39    // Send result back to renderer process
40    win.webContents.send("fromMain", responseObj);
41  });
42});
43