重要的API变更·续1

Sending any objects that aren’t native JS types, such as DOM objects (e.g. Element, Location, DOMMatrix), Node.js objects (e.g. process.env, Stream), or Electron objects (e.g. WebContents, BrowserWindow, WebFrame) is deprecated. In Electron 8, these objects will be serialized as before with a DeprecationWarning message, but starting in Electron 9, sending these kinds of objects will throw a ‘could not be cloned’ error.

Deprecated: <​webview>.getWebContents()

This API is implemented using the remote module, which has both performance and security implications. Therefore its usage should be explicit.

// Deprecatedwebview.getWebContents()// Replace withconst { remote } = require('electron')remote.webContents.fromId(webview.getWebContentsId())

However, it is recommended to avoid using the remote module altogether.

// mainconst { ipcMain, webContents } = require('electron')const getGuestForWebContents = (webContentsId, contents) => {  const guest = webContents.fromId(webContentsId)  if (!guest) {    throw new Error(`Invalid webContentsId: ${webContentsId}`)  }  if (guest.hostWebContents !== contents) {    throw new Error(`Access denied to webContents`)  }  return guest}ipcMain.handle('openDevTools', (event, webContentsId) => {  const guest = getGuestForWebContents(webContentsId, event.sender)  guest.openDevTools()})// rendererconst { ipcRenderer } = require('electron')ipcRenderer.invoke('openDevTools', webview.getWebContentsId())

Deprecated: webFrame.setLayoutZoomLevelLimits()

Chromium has removed support for changing the layout zoom level limits, and it is beyond Electron’s capacity to maintain it. The function will emit a warning in Electron 8.x, and cease to exist in Electron 9.x. The layout zoom level limits are now fixed at a minimum of 0.25 and a maximum of 5.0, as defined here.

计划重写的 API (7.0)

Deprecated: Atom.io Node Headers URL

这是在构建原生 node 模块时在 .npmrc 文件中指定为 disturl 的 url 或是 --dist-url 命令行标志. Both will be supported for the foreseeable future but it is recommended that you switch.

过时的: https://atom.io/download/electron

替换为: https://electronjs.org/headers

API Changed: session.clearAuthCache() no longer accepts options

The session.clearAuthCache API no longer accepts options for what to clear, and instead unconditionally clears the whole cache.

// Deprecatedsession.clearAuthCache({ type: 'password' })// Replace withsession.clearAuthCache()

API Changed: powerMonitor.querySystemIdleState is now powerMonitor.getSystemIdleState

// Removed in Electron 7.0powerMonitor.querySystemIdleState(threshold, callback)// Replace with synchronous APIconst idleState = powerMonitor.getSystemIdleState(threshold)

API Changed: powerMonitor.querySystemIdleTime is now powerMonitor.getSystemIdleState

// Removed in Electron 7.0powerMonitor.querySystemIdleTime(callback)// Replace with synchronous APIconst idleTime = powerMonitor.getSystemIdleTime()

API Changed: webFrame.setIsolatedWorldInfo replaces separate methods

// Removed in Electron 7.0webFrame.setIsolatedWorldContentSecurityPolicy(worldId, csp)webFrame.setIsolatedWorldHumanReadableName(worldId, name)webFrame.setIsolatedWorldSecurityOrigin(worldId, securityOrigin)// Replace withwebFrame.setIsolatedWorldInfo(  worldId,  {    securityOrigin: 'some_origin',    name: 'human_readable_name',    csp: 'content_security_policy'  })

Removed: marked property on getBlinkMemoryInfo

This property was removed in Chromium 77, and as such is no longer available.

Behavior Changed: webkitdirectory attribute for <​input type="file"/> now lists directory contents

The webkitdirectory property on HTML file inputs allows them to sel​ect folders. Previous versions of Electron had an incorrect implementation where the event.target.files of the input returned a FileList that returned one File corresponding to the selected folder.

As of Electron 7, that FileList is now list of all files contained within the folder, similarly to Chrome, Firefox, and Edge (link to MDN docs).

As an illustration, take a folder with this structure:

folder├── file1├── file2└── file3

In Electron <=6, this would return a FileList with a File object for:

path/to/folder

In Electron 7, this now returns a FileList with a File object for:

/path/to/folder/file3/path/to/folder/file2/path/to/folder/file1

Note that webkitdirectory no longer exposes the path to the selected folder. If you require the path to the selected folder rather than the folder contents, see the dialog.showOpenDialog API (link).

计划重写的 API (6.0)

API Changed: win.setMenu(null) is now win.removeMenu()

// 不推荐win.setMenu(null)// 替换为win.removeMenu()

API Changed: contentTracing.getTraceBufferUsage() is now a promise

// DeprecatedcontentTracing.getTraceBufferUsage((percentage, value) => {  // do something})// Replace withcontentTracing.getTraceBufferUsage().then(infoObject => {  // infoObject has percentage and value fields})

API Changed: electron.screen in the renderer process should be accessed via remote

// 不推荐require('electron').screen// 替换为require('electron').remote.screen

API Changed: require()ing node builtins in sandboxed renderers no longer implicitly loads the remote version

// 不推荐require('child_process')// 替换为require('electron').remote.require('child_process')// 不推荐require('fs')// 替换为require('electron').remote.require('fs')// 不推荐require('os')// 替换为require('electron').remote.require('os')// 不推荐require('path')// 替换为require('electron').remote.require('path')

Deprecated: powerMonitor.querySystemIdleState replaced with powerMonitor.getSystemIdleState

// DeprecatedpowerMonitor.querySystemIdleState(threshold, callback)// Replace with synchronous APIconst idleState = powerMonitor.getSystemIdleState(threshold)

Deprecated: powerMonitor.querySystemIdleTime replaced with powerMonitor.getSystemIdleTime

// DeprecatedpowerMonitor.querySystemIdleTime(callback)// Replace with synchronous APIconst idleTime = powerMonitor.getSystemIdleTime()

Deprecated: app.enableMixedSandbox() is no longer needed

// Deprecatedapp.enableMixedSandbox()

Mixed-sandbox mode is now enabled by default.

Deprecated: Tray.setHighlightMode

Under macOS Catalina our former Tray implementation breaks. Apple’s native substitute doesn’t support changing the highlighting behavior.

// Deprecatedtray.setHighlightMode(mode)// API will be removed in v7.0 without replacement.

计划重写的 API (5.0)

Default Changed: nodeIntegration and webviewTag default to false, contextIsolation defaults to true

不推荐使用以下 webPreferences 选项默认值,以支持下面列出的新默认值。

属性 不推荐使用的默认值 新的默认值
contextIsolation false true
nodeIntegration true false
webviewTag nodeIntegration 未设置过则是 true false

例如,重新启用 webviewTag

const w = new BrowserWindow({  webPreferences: {    webviewTag: true  }})

Behavior Changed: nodeIntegration in child windows opened via nativeWindowOpen

Child windows opened with the nativeWindowOpen option will always have Node.js integration disabled, unless nodeIntegrationInSubFrames is true.

API Changed: Registering privileged schemes must now be done before app ready

Renderer process APIs webFrame.registerURLSchemeAsPrivileged and webFrame.registerURLSchemeAsBypassingCSP as well as browser process API protocol.registerStandardSchemes have been removed. 新的 API protocol.registerSchemeasviliged 已被添加,并用于注册具有必要权限的自定义 scheme。 自定义 scheme 需要在 app 触发 ready 事件之前注册。

Deprecated: webFrame.setIsolatedWorld* replaced with webFrame.setIsolatedWorldInfo