Skip to main content

Electron 4.0.0

· 13 min read

Electron 团队很高兴地宣布,Electron 4 的稳定版本现已发布!你可以从 electronjs.org 或通过 npm 使用 npm install electron@latest 安装。本次发布包含了大量升级、修复和新功能,我们迫不及待想看到你们用它们创造出什么。查看更多关于此版本的详细信息,并在探索进程中分享你的反馈!

🌐 The Electron team is excited to announce that the stable release of Electron 4 is now available! You can install it from electronjs.org or from npm via npm install electron@latest. The release is packed with upgrades, fixes, and new features, and we can't wait to see what you build with them. Read more for details about this release, and please share any feedback you have as you explore!


有什么新功能?

🌐 What's New?

Electron 的大部分功能由 Chromium、Node.js 和 V8 提供,这些是构成 Electron 的核心组件。因此,Electron 团队的一个关键目标是尽可能跟上这些项目的变化,为开发 Electron 应用的开发者提供访问新的 Web 和 JavaScript 功能的机会。为此,Electron 4 对每个组件都进行了重大版本升级;Electron v4.0.0 包括 Chromium 69.0.3497.106、Node 10.11.0 和 V8 6.9.427.24

🌐 A large part of Electron's functionality is provided by Chromium, Node.js, and V8, the core components that make up Electron. As such, a key goal for the Electron team is to keep up with changes to these projects as much as possible, providing developers who build Electron apps access to new web and JavaScript features. To this end, Electron 4 features major version bumps to each of these components; Electron v4.0.0 includes Chromium 69.0.3497.106, Node 10.11.0, and V8 6.9.427.24.

此外,Electron 4 对 Electron 特定的 API 也进行了更改。你可以在下面找到 Electron 4 主要更改的摘要;有关完整的更改列表,请查看 Electron v4.0.0 发布说明

🌐 In addition, Electron 4 includes changes to Electron-specific APIs. You can find a summary of the major changes in Electron 4 below; for the full list of changes, check out the Electron v4.0.0 release notes.

禁用 remote 模块

🌐 Disabling the remote Module

你现在可以出于安全原因禁用 remote 模块。该模块可以为 BrowserWindowwebview 标签禁用:

🌐 You now have the ability to disable the remote module for security reasons. The module can be disabled for BrowserWindows and for webview tags:

// BrowserWindow
new BrowserWindow({
webPreferences: {
enableRemoteModule: false
}
})

// webview tag
<webview src="http://www.google.com/" enableremotemodule="false"></webview>

有关更多信息,请参阅 BrowserWindow<webview> 标签 文档。

🌐 See the BrowserWindow and <webview> Tag documentation for more information.

过滤 remote.require() / remote.getGlobal() 请求

🌐 Filtering remote.require() / remote.getGlobal() Requests

如果你不想在渲染器进程或 webview 中完全禁用 remote 模块,但希望对哪些模块可以通过 remote.require 引入有更多控制,这个功能会很有用。

🌐 This feature is useful if you don't want to completely disable the remote module in your renderer process or webview but would like additional control over which modules can be required via remote.require.

当在渲染器进程中通过 remote.require 加载模块时,会在 app 模块 上触发一个 remote-require 事件。你可以在事件中(第一个参数)调用 event.preventDefault() 来阻止模块加载。发生 requireWebContents 实例 会作为第二个参数传入,而模块的名称会作为第三个参数传入。相同的事件也会在 WebContents 实例上触发,但在这种情况下,参数只有事件和模块名称。在两种情况下,你都可以通过设置 event.returnValue 的值来返回自定义值。

🌐 When a module is required via remote.require in a renderer process, a remote-require event is raised on the app module. You can call event.preventDefault() on the the event (the first argument) to prevent the module from being loaded. The WebContents instance where the require occurred is passed as the second argument, and the name of the module is passed as the third argument. The same event is also emitted on the WebContents instance, but in this case the only arguments are the event and the module name. In both cases, you can return a custom value by setting the value of event.returnValue.

// Control `remote.require` from all WebContents:
app.on('remote-require', function (event, webContents, requestedModuleName) {
// ...
});

// Control `remote.require` from a specific WebContents instance:
browserWin.webContents.on(
'remote-require',
function (event, requestedModuleName) {
// ...
},
);

以类似的方式,当调用 remote.getGlobal(name) 时,会触发一个 remote-get-global 事件。其工作原理与 remote-require 事件相同:调用 preventDefault() 可以防止返回全局对象,并设置 event.returnValue 来返回自定义值。

🌐 In a similar fashion, when remote.getGlobal(name) is called, a remote-get-global event is raised. This works the same way as the remote-require event: call preventDefault() to prevent the global from being returned, and set event.returnValue to return a custom value.

// Control `remote.getGlobal` from all WebContents:
app.on(
'remote-get-global',
function (event, webContents, requrestedGlobalName) {
// ...
},
);

// Control `remote.getGlobal` from a specific WebContents instance:
browserWin.webContents.on(
'remote-get-global',
function (event, requestedGlobalName) {
// ...
},
);

更多信息,请参阅以下文档:

🌐 For more information, see the following documentation:

JavaScript 访问“关于”面板

🌐 JavaScript Access to the About Panel

在 macOS 上,你现在可以调用 app.showAboutPanel() 来以编程方式显示“关于”面板,就像点击通过 {role: 'about'} 创建的菜单项一样。有关更多信息,请参阅 showAboutPanel 文档

🌐 On macOS, you can now call app.showAboutPanel() to programmatically show the About panel, just like clicking the menu item created via {role: 'about'}. See the showAboutPanel documentation for more information

控制 WebContents 后台节流

🌐 Controlling WebContents Background Throttling

WebContents 实例现在有一个方法 setBackgroundThrottling(allowed),可以在页面处于后台时启用或禁用计时器和动画的节流功能。

let win = new BrowserWindow(...)
win.webContents.setBackgroundThrottling(enableBackgroundThrottling)

请参阅 ‘setBackgroundThrottling’ 文档 以获取更多信息。

🌐 See the setBackgroundThrottling documentation for more information.

重大变化

🌐 Breaking Changes

不再支持 macOS 10.9

🌐 No More macOS 10.9 Support

Chromium 不再支持 macOS 10.9(OS X Mavericks),因此 Electron 4.0 及更高版本也不支持它

🌐 Chromium no longer supports macOS 10.9 (OS X Mavericks), and as a result Electron 4.0 and beyond does not support it either.

单实例锁定

🌐 Single Instance Locking

以前,要将你的应用设置为单实例应用(确保在任何给定时间只运行一个应用实例),你可以使用 app.makeSingleInstance() 方法。从 Electron 4.0 开始,你必须改用 app.requestSingleInstanceLock()。该方法的返回值指示此应用实例是否成功获得锁。如果未能获得锁,你可以假设你的应用的另一个实例已持有锁并且正在运行,然后立即退出。

🌐 Previously, to make your app a Single Instance Application (ensuring that only one instance of your app is running at any given time), you could use the app.makeSingleInstance() method. Starting in Electron 4.0, you must use app.requestSingleInstanceLock() instead. The return value of this method indicates whether or not this instance of your application successfully obtained the lock. If it failed to obtain the lock, you can assume that another instance of your application is already running with the lock and exit immediately.

关于使用 requestSingleInstanceLock() 的示例以及在各种平台上细微行为的信息,请参见 app.requestSingleInstanceLock() 及相关方法的文档second-instance 事件

🌐 For an example of using requestSingleInstanceLock() and information on nuanced behavior on various platforms, see the documentation for app.requestSingleInstanceLock() and related methods and the second-instance event.

win_delay_load_hook

在为 Windows 构建本地模块时,模块的 binding.gyp 中的 win_delay_load_hook 变量必须为 true(这是默认值)。如果没有这个钩子,本地模块将在 Windows 上加载失败,并显示类似 Cannot find module 的错误信息。参见本地模块指南 以获取更多信息。

🌐 When building native modules for windows, the win_delay_load_hook variable in the module's binding.gyp must be true (which is the default). If this hook is not present, then the native module will fail to load on Windows, with an error message like Cannot find module. See the native module guide for more information.

弃用

🌐 Deprecations

以下重大变更计划在 Electron 5.0 中进行,因此在 Electron 4.0 中已弃用。

🌐 The following breaking changes are planned for Electron 5.0, and thus are deprecated in Electron 4.0.

已为 nativeWindowOpen 版 Windows 禁用 Node.js 集成

🌐 Node.js Integration Disabled for nativeWindowOpen-ed Windows

从 Electron 5.0 开始,使用 nativeWindowOpen 选项打开的子窗口将始终禁用 Node.js 集成。

🌐 Starting in Electron 5.0, child windows opened with the nativeWindowOpen option will always have Node.js integration disabled.

webPreferences 默认值

🌐 webPreferences Default Values

在创建新的 BrowserWindow 并设置 webPreferences 选项时,以下 webPreferences 选项的默认值已被弃用,建议使用下面列出的新默认值:

🌐 When creating a new BrowserWindow with the webPreferences option set, the following webPreferences option defaults are deprecated in favor of new defaults listed below:

属性已弃用的默认值新默认值
contextIsolationfalsetrue
nodeIntegrationtruefalse
webviewTag如果设置了 nodeIntegration 则为其值,否则为 truefalse

请注意:目前存在一个已知的错误(#9736),如果 contextIsolation 启用,webview 标签将无法工作。请关注 GitHub 上的该问题以获取最新信息!

🌐 Please note: there is currently a known bug (#9736) that prevents the webview tag from working if contextIsolation is on. Keep an eye on the GitHub issue for up-to-date information!

Electron 安全文档中了解有关上下文隔离、Node 集成以及 webview 标签的更多信息。

🌐 Learn more about context isolation, Node integration, and the webview tag in the Electron security document.

Electron 4.0 仍将使用当前的默认值,但如果你没有为它们传递明确的值,你将看到弃用警告。为了让你的应用为 Electron 5.0 做好准备,请为这些选项使用明确的值。有关每个选项的详细信息,请参见 BrowserWindow 文档

🌐 Electron 4.0 will still use the current defaults, but if you don't pass an explicit value for them, you'll see a deprecation warning. To prepare your app for Electron 5.0, use explicit values for these options. See the BrowserWindow docs for details on each of these options.

webContents.findInPage(text[, options])

medialCapitalAsWordStartwordStart 选项已被弃用,因为它们已在上游移除。

🌐 The medialCapitalAsWordStart and wordStart options have been deprecated as they have been removed upstream.

应用反馈计划

🌐 App Feedback Program

我们在 Electron 3.0 开发进程中建立的应用反馈计划非常成功,因此我们在 4.0 的开发进程中也继续了这一计划。我们想向 Atlassian、Discord、MS Teams、OpenFin、Slack、Symphony、WhatsApp 以及其他参与 4.0 测试版周期的计划成员表示衷心的感谢。要了解更多关于应用反馈计划的信息并参与未来的测试版,请查看我们关于该计划的博客文章

🌐 The App Feedback Program we instituted during the development of Electron 3.0 was successful, so we've continued it during the development of 4.0 as well. We'd like to extend a massive thank you to Atlassian, Discord, MS Teams, OpenFin, Slack, Symphony, WhatsApp, and the other program members for their involvement during the 4.0 beta cycle. To learn more about the App Feedback Program and to participate in future betas, check out our blog post about the program.

下一步计划

🌐 What's Next

在短期内,你可以预期团队将继续专注于跟进构成 Electron 的主要组件的开发,包括 Chromium、Node 和 V8。虽然我们谨慎地不对发布日期作出承诺,但我们的计划是大约每季度随着这些组件的新版本发布 Electron 的新主版本。查看我们的版本说明文档以了解有关 Electron 版本控制的详细信息。

🌐 In the short term, you can expect the team to continue to focus on keeping up with the development of the major components that make up Electron, including Chromium, Node, and V8. Although we are careful not to make promises about release dates, our plan is release new major versions of Electron with new versions of those components approximately quarterly. See our versioning document for more detailed information about versioning in Electron.

有关即将发布的 Electron 版本中计划的重大更改的信息,请参阅我们的计划重大更改文档

🌐 For information on planned breaking changes in upcoming versions of Electron, see our Planned Breaking Changes doc.