Skip to main content

Chrome 扩展支持

🌐 Chrome Extension Support

Electron 支持 Chrome 扩展程序 API 的一个子集,主要是为了支持 DevTools 扩展和 Chromium 内部扩展,但它也恰好支持一些其他扩展功能。

🌐 Electron supports a subset of the Chrome Extensions API, primarily to support DevTools extensions and Chromium-internal extensions, but it also happens to support some other extension capabilities.

note

Electron 不支持来自商店的任意 Chrome 扩展,并且 Electron 项目的非目标是与 Chrome 的扩展实现完全兼容。

加载扩展

🌐 Loading extensions

Electron 仅支持加载未打包的扩展(即,.crx 文件不适用)。扩展是按每个 session 安装的。要加载扩展,请调用 ses.extensions.loadExtension

🌐 Electron only supports loading unpacked extensions (i.e., .crx files do not work). Extensions are installed per-session. To load an extension, call ses.extensions.loadExtension:

const { session } = require('electron')

session.defaultSession.loadExtension('path/to/unpacked/extension').then(({ id }) => {
// ...
})

已加载的扩展不会在退出后自动记住;如果应用运行时不调用 loadExtension,扩展将不会被加载。

🌐 Loaded extensions will not be automatically remembered across exits; if you do not call loadExtension when the app runs, the extension will not be loaded.

请注意,加载扩展仅支持在持久会话中进行。尝试在内存会话中加载扩展会导致错误。

🌐 Note that loading extensions is only supported in persistent sessions. Attempting to load an extension into an in-memory session will throw an error.

有关加载、卸载和查询活动扩展的更多信息,请参阅 session 文档。

🌐 See the session documentation for more information about loading, unloading, and querying active extensions.

支持的扩展 API

🌐 Supported Extensions APIs

我们支持以下扩展 API,但有一些注意事项。其他 API 可能也会受到支持,但对这里未列出的任何 API 的支持是临时的,可能会被移除。

🌐 We support the following extensions APIs, with some caveats. Other APIs may additionally be supported, but support for any APIs not listed here is provisional and may be removed.

支持的清单键

🌐 Supported Manifest Keys

  • name
  • version
  • author
  • permissions
  • content_scripts
  • default_locale
  • devtools_page
  • short_name
  • host_permissions(清单 V3)
  • manifest_version
  • background(清单 V2)
  • minimum_chrome_version

有关每个可能键的用途的更多信息,请参见 Manifest 文件格式

🌐 See Manifest file format for more information about the purpose of each possible key.

chrome.devtools.inspectedWindow

支持此 API 的所有功能。

🌐 All features of this API are supported.

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.devtools.network

支持此 API 的所有功能。

🌐 All features of this API are supported.

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.devtools.panels

支持此 API 的所有功能。

🌐 All features of this API are supported.

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.extension

支持以下 chrome.extension 属性:

🌐 The following properties of chrome.extension are supported:

  • chrome.extension.lastError

支持以下 chrome.extension 方法:

🌐 The following methods of chrome.extension are supported:

  • chrome.extension.getURL
  • chrome.extension.getBackgroundPage

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.management

支持以下 chrome.management 方法:

🌐 The following methods of chrome.management are supported:

  • chrome.management.getAll
  • chrome.management.get
  • chrome.management.getSelf
  • chrome.management.getPermissionWarningsById
  • chrome.management.getPermissionWarningsByManifest

支持以下 chrome.management 事件:

🌐 The following events of chrome.management are supported:

  • chrome.management.onEnabled
  • chrome.management.onDisabled

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.runtime

支持以下 chrome.runtime 属性:

🌐 The following properties of chrome.runtime are supported:

  • chrome.runtime.lastError
  • chrome.runtime.id

支持以下 chrome.runtime 方法:

🌐 The following methods of chrome.runtime are supported:

  • chrome.runtime.getBackgroundPage
  • chrome.runtime.getManifest
  • chrome.runtime.getPlatformInfo
  • chrome.runtime.getURL
  • chrome.runtime.connect
  • chrome.runtime.sendMessage
  • chrome.runtime.reload

支持以下 chrome.runtime 事件:

🌐 The following events of chrome.runtime are supported:

  • chrome.runtime.onStartup
  • chrome.runtime.onInstalled
  • chrome.runtime.onSuspend
  • chrome.runtime.onSuspendCanceled
  • chrome.runtime.onConnect
  • chrome.runtime.onMessage

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.scripting

支持此 API 的所有功能。

🌐 All features of this API are supported.

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.storage

支持以下 chrome.storage 方法:

🌐 The following methods of chrome.storage are supported:

  • chrome.storage.local

chrome.storage.syncchrome.storage.managed 受支持。

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.tabs

支持以下 chrome.tabs 方法:

🌐 The following methods of chrome.tabs are supported:

  • chrome.tabs.sendMessage
  • chrome.tabs.reload
  • chrome.tabs.executeScript
  • chrome.tabs.query(部分支持)
    • 支持的属性:urltitleaudibleactivemuted
  • chrome.tabs.update(部分支持)
    • 支持的属性:urlmuted
note

在 Chrome 中,将 -1 作为标签页 ID 传递表示“当前活动标签页”。由于 Electron 没有这样的概念,传递 -1 作为标签页 ID 是不支持的,并且会引发错误。

有关更多信息,请参见官方文档

🌐 See official documentation for more information.

chrome.webRequest

支持此 API 的所有功能。

🌐 All features of this API are supported.

note

如果存在冲突的处理程序,Electron 的 webRequest 模块优先于 chrome.webRequest

有关更多信息,请参见官方文档

🌐 See official documentation for more information.