main.js
// 导入 app, BrowserWindow 模块
const { app, BrowserWindow, ipcMain } = require('electron')
app.on('ready', () => {
let mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
ipcMain.on('message', (event, arg) => {
console.log('arg:' + arg)
event.reply('reply', 'hello from main process')
})
})
renderer.js
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
// 引入模块
const { ipcRenderer} = require('electron');
const { BrowserWindow } = require('electron').remote;
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('node-version').innerHTML = process.versions.node
document.getElementById('send').addEventListener('click', () => {
ipcRenderer.send('message', 'hello from renderer')
let win = new BrowserWindow({ width: 800, height: 600})
win.loadURL('https://baidu.com')
})
ipcRenderer.on('reply', (event, arg) => {
document.getElementById('message').innerHTML = arg
})
})