8 月 20 日,Electron 32.0.0 正式发布!该版本包括了对 Chromium 128.0.6613.36、 V8 12.8和 Node.js 20.16.0 的升级。下面就来看看该版本都有哪些更新吧!
可以通过以下命令来安装最新版本:
npm install electron@latest
主要变化
亮点
更新了文档,新增了API版本历史记录功能
移除了Web文件API中非标准的File.path
扩展名。
优化了Web文件系统API,在尝试访问被阻止路径中的文件或目录时,失败路径与上游保持一致。
将以下导航相关API整合至webcontents.navigationHistory
:canGoBack
、goBack
、canGoForward
、goForward
、canGoToOffset
、goToOffset
、clear
。原导航 API 已弃用。
技术栈
新特性
新增了对实用程序进程中发起的身份验证请求的响应机制,通过app模块的login
事件实现。#43317
CPUUsage结构现包含cumulativeCPUUsage
属性,用于返回进程启动以来的累计CPU使用时间(秒)。
将以下导航API集成至webContents.navigationHistory
:canGoBack
、goBack
、canGoForward
、goForward
、canGoToOffset
、goToOffset
、clear
,同时原API已废弃。
WebContentsView现可接受已存在的webContents实例。
nativeTheme
新增prefersReducedTransparency
属性,用于检测用户是否通过系统辅助功能设置关闭了操作系统级别的透明度。
文件系统访问API在尝试打开受限路径中的文件或目录时,其失败处理流程与上游保持一致。
在Linux平台上支持Windows控制覆盖API。
网络请求现支持zstd
压缩算法。
重要变化
移除:File.path
Web文件对象的非标准path
属性曾在Electron早期版本中提供,以便于在渲染器中处理本地文件。但由于不符合标准并存在安全隐患,自 Electron 32 起已被移除,推荐使用webUtils.getPathForFile
方法替代。
// 旧版(渲染器)
const file = document.querySelector('input[type=file]');
alert(`上传文件路径是:${file.path}`);
// 新版(渲染器)
const file = document.querySelector('input[type=file]');
electron.showFilePath(file);
// 新版(预加载脚本)
const { contextBridge, webUtils } = require('electron');
contextBridge.exposeInMainWorld('electron', {
showFilePath(file) {
const path = webUtils.getPathForFile(file);
alert(`上传文件路径是:${path}`);
},
});
弃用:WebContents上的clearHistory、canGoBack、goBack、canGoForward、goForward、goToIndex、canGoToOffset、goToOffset
WebContents实例的导航 API 已弃用,现已迁移到WebContents的navigationHistory
属性,以提供更加结构化和用户友好的导航历史管理接口。
// 弃用API
win.webContents.clearHistory();
win.webContents.canGoBack();
win.webContents.goBack();
win.webContents.canGoForward();
win.webContents.goForward();
win.webContents.goToIndex(index);
win.webContents.canGoToOffset();
win.webContents.goToOffset(index);
// 更新后的API
win.webContents.navigationHistory.clear();
win.webContents.navigationHistory.canGoBack();
win.webContents.navigationHistory.goBack();
win.webContents.navigationHistory.canGoForward();
win.webContents.navigationHistory.goForward();
win.webContents.navigationHistory.canGoToOffset();
win.webContents.navigationHistory.goToOffset(index);
注意事项
根据项目的支持政策,Electron 29.x.y 已经达到了支持结束的状态,建议升级到更新的 Electron 版本。
编辑&整理:前端充电宝
参考:https://www.electronjs.org/blog/electron-32-0