Electron 如何在渲染进程中去创建新弹窗

在Electron中要想在渲染进程中弹出新窗体,一般有以下两种方式

  • 引入remote模块,在页面中使用remote中的BrowserWindow对象来创建窗体
  • 使用渲染进程和主线程的通信,利用渲染进程发信给主线程,统一由主线程来创建新窗体

实现过程

remote模块创建窗体

安装@electron/remote模块

shell 复制代码
npm i -D @electron/remote

主进程

@electron/remote/main 必须在主进程中初始化,然后才能从渲染进程中使用它

javascript 复制代码
require('@electron/remote/main').initialize()

在中electron >= 14.0.0,您必须使用新的enableAPI来分别启用每个所需的远程模块WebContents

javascript 复制代码
const { app } = require("electron");
const mainWin = new BrowserWindow({
   ...,
   webPreferences: {
   	 nodeIntegration: true, //允许渲染进程内启用Node集成
     contextIsolation: false, //是否在独立 JavaScript 环境中运行 Electron API和指定的preload 脚本. 默认为 true
   },
 });
app.on('browser-window-created', (_, window) => {
    require("@electron/remote/main").enable(window.webContents)
})

electron < 14.0.0 中,@electron/remote尊重enableRemoteModuleWebPreferences 值。您必须传递{ webPreferences: { enableRemoteModule: true } }给窗体的构造函数,BrowserWindow该构造函数应授予@electron/remote使用权限

javascript 复制代码
const { BrowserWindow } = require("electron");
const mainWin = new BrowserWindow({
   ...,
   webPreferences: {
   	 nodeIntegration: true, //允许渲染进程内启用Node集成
     contextIsolation: false, //是否在独立 JavaScript 环境中运行 Electron API和指定的preload 脚本. 默认为 true
     enableRemoteModule: true
   },
 });

渲染进程

调用@electron/remote创建新窗体 同步方式

javascript 复制代码
const { BrowserWindow } = require("@electron/remote");
let win = new BrowserWindow({width:800,height:600})
win.loadURL('https://wwww.baidu.com');

主进程和渲染进程通信创建窗体

渲染进程

javascript 复制代码
const { ipcRenderer } = require("electron");
ipcRenderer.send("openWindow");

主进程

javascript 复制代码
const { ipcMain, BrowserWindow } = require("electron");
ipcMain.on('openWindow',(ev,target)=>{
  const newWin = new BrowserWindow({
    width: 600,
    height: 400,
  });
  newWin.loadURL('https://wwww.baidu.com');
})

注意

  1. 上面的例子中是通过{ webPreferences: { nodeIntegration: true ,contextIsolation: false} }给窗体的构造函数来解决渲染进程不能引入nodeElectron模块的问题,不过这样子会有安全风险 ,一般来说可以用webPreferences 中 preload来暴露nodeElectron的Api
  2. @electron/remote由于这需要通过 npm 的模块而不是内置模块,因此如果您remote从渲染进程中使用,则需要适当配置捆绑器以打包@electron/remote 预加载脚本中的代码。当然,使用@electron/remote会让沙盒的效率大大降低。在@electron/remotenpm官网^1^中也明确说明不推荐使用该模块

Footnotes

  1. @electron/remotenpm官网
相关推荐
怕浪猫2 天前
Electron 开发实战(十六):总结与展望|生态现状、框架对比、行业趋势与学习指南
前端·javascript·electron
古德new3 天前
鸿蒙PC使用electron迁移:Joplin Electron 桌面适配全记录
华为·electron·harmonyos
三声三视3 天前
Electron 在鸿蒙 PC 上跑 webview,我是怎么把首屏从 4.2s 干到 1.1s 的
华为·electron·harmonyos·鸿蒙
「、皓子~3 天前
海狸IM 2.0 正式发布:六端齐发,开源 IM 迈入新阶段
flutter·electron·开源软件·ai编程·交友·im
JOJO数据科学3 天前
JupyterLab Electron 鸿蒙 PC 适配全记录:从 Python 原生崩溃到 node-static 本地工作台
python·electron·harmonyos
悟空瞎说4 天前
深度排查:Electron MAS 包播放 HDR 视频引发界面卡死问题全解析
electron
不良使4 天前
鸿蒙PC迁移:使用Electron`logseq-master-ohos` 鸿蒙适配全记录
jvm·electron·harmonyos
JOJO数据科学4 天前
pgAdmin4 Electron 鸿蒙 PC 适配全记录:从白屏到连接 PostgreSQL
postgresql·electron·harmonyos
「、皓子~4 天前
海狸IM 2.0 开放能力说明:OAuth2 接入与群推送机器人
人工智能·架构·electron·机器人·开源·交友·im
小鹏linux4 天前
鸿蒙PC使用 Electron 迁移:LX Music 桌面版适配全记录
华为·electron·harmonyos