Electron实战(二):将Node.js和UI能力(app/BrowserWindow/dialog)等注入html

文章目录

      • 设置webPreferences参数
      • 安装`@electron/remote`
      • main进程中初始化
      • html中使用dialog
      • 踩坑
      • 参考文档

上一篇:Electron实战(一):环境搭建/Hello World/打包exe

设置webPreferences参数

为了能够在html/js中访问Node.js提供fs等模块,需要在new BrowserWindow(config)的时候,设置一些参数:

//LuckyTools/index.js
const mainWindow = new BrowserWindow({icon: iconPath,width: 800,height: 600,webPreferences: {// 赋予此窗口页面中的JavaScript访问Node.js环境的能力nodeIntegration: true,//Election 14之前,需要打开remote模块,方便main进程调用UI渲染进程的dialog/menu等enableRemoteModule: true, // 官网似乎说是默认false,但是这里必须设置contextIsolationcontextIsolation: false,}
});

Election 14之前,只需要在webPreferences设置中打开remote模块,即可在main进程调用UI渲染进程的dialog/menu等。

安装@electron/remote

Electron 14开始remote模块已被移除,为了能够在html中访问Electron的UI能力(app/BrowserWindow/dialog),需要额外单独安装electron/remote包。

npm install @electron/remote --save

安装后,在html中引用UI模块的方式也有变化,官方给的代码是:

// Deprecated in Electron 12:
const { BrowserWindow } = require('electron').remote// Replace with:
const { BrowserWindow } = require('@electron/remote')

main进程中初始化

// In the main process:
require('@electron/remote/main').initialize();
// 必须启用,html中才能访问
require("@electron/remote/main").enable(mainWindow.webContents);

最终,完整的index.js文件内容如下:

//app 模块,控制整个应用程序的事件生命周期。
//BrowserWindow 模块,它创建和管理程序的窗口。
const { app, BrowserWindow } = require('electron')
const path = require('path')
const iconPath = path.join(__dirname, './src/res/icon.ico')//在 Electron 中,只有在 app 模块的 ready 事件被激发后才能创建浏览器窗口
app.on('ready', () => {require('@electron/remote/main').initialize();//创建一个窗口const mainWindow = new BrowserWindow({icon: iconPath,// width: 800,// height: 600,webPreferences: {// 赋予此窗口页面中的JavaScript访问Node.js环境的能力nodeIntegration: true,// 打开remote模块,方便main进程调用UI渲染进程的dialog/menu等enableRemoteModule: true,   // 官网似乎说是默认false,但是这里必须设置contextIsolationcontextIsolation: false,}});// 必须启用,html中才能访问require("@electron/remote/main").enable(mainWindow.webContents);// mainWindow.setMenu(null);//窗口加载html文件mainWindow.loadFile('./src/index.html')
})

html中使用dialog

//test.html
var fs = require("fs");
var elec = require('electron');
console.log(elec);
var EUI = require('@electron/remote');
console.log(EUI);
var dialog = EUI.dialog;//可以直接使用dialog模块了var filePath = dialog.showOpenDialogSync({title: '选择文件',properties: ['openFile']
});
if (!filePath) {console.log("cancel: ", filePath);
}
fs.readFile(filePath[0], { encoding: 'utf-8' }, (err, data) => {if (err) throw err;console.log(data);
});

打印出EUI发现,所有UI相关的模块都被注入进来了:
在这里插入图片描述

踩坑

不知道是不是网络原因,笔者安装@electron/remote报错:

npm install @electron/remote --save
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/electron-packger - Not found
npm ERR! 404
npm ERR! 404  'electron-packger@^24.9.1' is not in this registry.
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.npm ERR! A complete log of this run can be found in: D:\env\nodejs\node_cache\_logs\2024-02-04T03_52_08_843Z-debug-0.log

接着尝试安装cnpm:npm install -g cnpm --registry=https://registry.npm.taobao.org

结果依然报错:

PS D:\dev\web\work\LuckyTools> npm install -g cnpm --registry=https://registry.npm.taobao.org
npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npm.taobao.org/cnpm failed, reason: certificate has expirednpm ERR! A complete log of this run can be found in: D:\env\nodejs\node_cache\_logs\2024-02-04T06_08_20_084Z-debug-0.log

解决方式:

#清除npm缓存
npm cache clean --force
#取消ssl验证
npm config set strict-ssl false
#之后再npm install 你想安装的东西

查看cnpm版本:

 cnpm --version
cnpm@9.4.0 (D:\env\nodejs\node_global\node_modules\cnpm\lib\parse_argv.js)
npm@9.9.2 (D:\env\nodejs\node_global\node_modules\cnpm\node_modules\npm\index.js)
node@20.10.0 (D:\env\nodejs\node.exe)
npminstall@7.12.0 (D:\env\nodejs\node_global\node_modules\cnpm\node_modules\npminstall\lib\index.js)
prefix=D:\env\nodejs\node_global
win32 x64 10.0.22621
registry=https://registry.npmmirror.com

再安装:

cnpm install @electron/remote --save
# 略去一些输出,终于安装成功了
dependencies:
+ @electron/remote ^2.1.2

参考文档

Electron 14开始remote模块已被移除,可用@electron/remote包代替:https://www.electronjs.org/docs/latest/breaking-changes#removed-remote-module

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/460010.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NLP_语言模型的雏形 N-Gram 模型

文章目录 N-Gram 模型1.将给定的文本分割成连续的N个词的组合(N-Gram)2.统计每个N-Gram在文本中出现的次数,也就是词频3.为了得到一个词在给定上下文中出现的概率,我们可以利用条件概率公式计算。具体来讲,就是计算给定前N-1个词时&#xff0…

c++之说_13|模板 折叠表达式

折叠表达式 可以通过形参包的的实际参数&#xff08;不是类型&#xff09; 展开式子 这是这里说的几种 实际上并还有一些写法 先介绍这几种吧 #include <cstdio> template<typename T,T... n> struct integer_sequence {T val; }; template<int idx,typenam…

[NOI2014] 起床困难综合症

[NOI2014] 起床困难综合症 题目描述 21 21 21 世纪&#xff0c;许多人得了一种奇怪的病&#xff1a;起床困难综合症&#xff0c;其临床表现为&#xff1a;起床难&#xff0c;起床后精神不佳。作为一名青春阳光好少年&#xff0c;atm 一直坚持与起床困难综合症作斗争。通过研究…

windows上卸载完程序后,清理残余文件,无法删除的情况处理

现象&#xff1a;通常在卸载完软件后&#xff0c;要删除残余文件或者移动残余文件时候&#xff0c;会弹出来 原因&#xff1a; 因为文件被其他程序已经加载&#xff0c;处理的目标是找到使用这个文件的进程&#xff0c;然后kill掉。类似于linux上的lsof命令查找到进程号&…

C++新版本特性

目录: 前言 C11的常用新特性 auto类型推导&#xff1a; auto的限制&#xff1a; auto的应用&#xff1a; decltype类型推导&#xff1a; decltype的实际应用&#xff1a; 使用using 定义别名&#xff1a; 支持函数模板的默认模板参数 : tuple元组&#xff1a; 列表初…

Linux操作系统基础(三):虚拟机与Linux系统安装

文章目录 虚拟机与Linux系统安装 一、系统的安装方式 二、虚拟机概念 三、虚拟机的安装 四、Linux系统安装 1、解压人工智能虚拟机 2、找到解压目录中的node1.vmx 3、启动操作系统 虚拟机与Linux系统安装 一、系统的安装方式 Linux操作系统也有两种安装方式&#xf…

从零开始手写mmo游戏从框架到爆炸(十)— 集成springboot-jpa与用户表

导航&#xff1a;从零开始手写mmo游戏从框架到爆炸&#xff08;零&#xff09;—— 导航-CSDN博客 集成springboot-jpa&#xff0c;不用mybatis框架一个是方便对接不同的数据源。第二个目前规划的游戏内容可能对数据库的依赖不是很大&#xff0c;jpa应该肯定能满足要求了…

阿里云服务器价格表2024最新版CPU内存带宽报价

2024年2月阿里云服务器租用价格表更新&#xff0c;云服务器ECS经济型e实例2核2G、3M固定带宽99元一年、ECS u1实例2核4G、5M固定带宽、80G ESSD Entry盘优惠价格199元一年&#xff0c;轻量应用服务器2核2G3M带宽轻量服务器一年61元、2核4G4M带宽轻量服务器一年165元12个月、2核…

2024.2.7日总结(小程序开发4)

页面导航 页面导航是页面之间的相互跳转&#xff1a; <a>链接location.href 小程序中实现页面导航的两种方式&#xff1a; 声明式导航 在页面上声明一个<navigator>导航组件通过点击<navigator>组件实现页面跳转 编程式导航 调用小程序的导航API&…

[C#] 如何使用ScottPlot.WPF在WPF桌面程序中绘制图表

什么是ScottPlot.WPF&#xff1f; ScottPlot.WPF 是一个开源的数据可视化库&#xff0c;用于在 WPF 应用程序中创建高品质的绘图和图表。它是基于 ScottPlot 库的 WPF 版本&#xff0c;提供了简单易用的 API&#xff0c;使开发人员能够通过简单的代码创建各种类型的图表&#…

极值图论基础

目录 一&#xff0c;普通子图禁图 二&#xff0c;Turan问题 三&#xff0c;Turan定理、Turan图 1&#xff0c;Turan定理 2&#xff0c;Turan图 四&#xff0c;以完全二部图为禁图的Turan问题 1&#xff0c;最大边数的上界 2&#xff0c;最大边数的下界 五&#xff0c;…

CentOS7集群配置免密登录

准备工作 提前开启三台虚拟机hadoop102、hadoop103,hadoop104,关于三台虚拟机的安装可以参考&#xff1a;https://mp.csdn.net/mp_blog/creation/editor/136010108 配置免密登录 一、分别修改三台机器的hosts,配置主机映射关系 vim /etc/hosts 文件中输入以下内容&#xf…