Electron 4.0.0
Electron 团队非常高兴地宣布,Electron 4 的稳定版本现已发布!你可以从 electronjs.org 安装它,也可以通过 npm install electron@latest
从 npm 安装它。此版本包含升级、修复和新功能,我们迫不及待地想看到你使用它们构建的作品。阅读更多有关此版本的详细信息,并在探索过程中分享你的任何反馈!
¥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 的大部分功能由构成 Electron 的核心组件 Chromium、Node.js 和 V8 提供。因此,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
模块。该模块可以在 BrowserWindow
和 webview
标签下禁用:
¥You now have the ability to disable the remote
module for security reasons. The module can be disabled for BrowserWindow
s 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
如果你不想完全禁用 remote
模块,此功能非常有用。你的渲染器进程或 webview
,但希望对通过 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()
来阻止模块加载。require 发生的 WebContents
实例 作为第二个参数传递,模块名称作为第三个参数传递。相同的事件也会在 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)
,用于在页面处于后台时启用或禁用计时器和动画的节流。
¥WebContents
instances now have a method setBackgroundThrottling(allowed)
to enable or disable throttling of timers and animations when the page is backgrounded.
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
-ed 已禁用 Node.js 集成 Windows
¥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
当创建一个包含 webPreferences
选项集的新 BrowserWindow
时,以下 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:
属性 | 已弃用的默认值 | 新默认值 |
---|---|---|
contextIsolation | false | true |
nodeIntegration | true | false |
webviewTag | 如果设置,则为 nodeIntegration 的值,否则为 true | false |
请注意:目前,一个已知 bug (#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!
了解更多关于上下文隔离、Node 集成以及 Electron 安全文档 中的 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])
medialCapitalAsWordStart
和 wordStart
选项已被弃用,因为它们已被上游移除。
¥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 测试周期中的参与。要了解有关应用反馈计划的更多信息并参与未来的 Beta 版,请参阅 查看我们关于该程序的博客文章。
¥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.