跳转到主内容

Chrome 扩展支持

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 does not support arbitrary Chrome extensions from the store, and it is a non-goal of the Electron project to be perfectly compatible with Chrome's implementation of Extensions.

加载扩展​

Electron 只支持加载未打包的扩展 (即不能使用 .crx 文件)。 插件会被安装到每一个会话。 To load an extension, call ses.extensions.loadExtension:

const { session } = require('electron')

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

加载的扩展将不会被自动记住;如果在应用程序运行时未调用 loadExtension,则不会加载扩展。

注意,仅能在持久 session 中加载扩展。 尝试将扩展加载到内存 session 中会出现错误。

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

支持的扩展 API​

我们支持以下扩展 API,并需要注意一些警告。 其他API可能会得到额外支持,但对此处未列出的任何API的支持都是临时的,可能会被删除。

Supported Manifest Keys​

  • name
  • version
  • author
  • 权限
  • content_scripts
  • default_locale
  • devtools_page
  • short_name
  • host_permissions (Manifest V3)
  • manifest_version
  • background (Manifest V2)
  • minimum_chrome_version

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

chrome.devtools.inspectedWindow​

支持这些 API 的所有功能。

See official documentation for more information.

chrome.devtools.network​

支持这些 API 的所有功能。

See official documentation for more information.

chrome.devtools.panels​

支持这些 API 的所有功能。

See official documentation for more information.

chrome.extension​

支持 chrome.extension 的以下属性:

  • chrome.extension.lastError

支持 chrome.extension 的以下方法:

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

See official documentation for more information.

chrome.management​

支持 chrome.management 的以下方法:

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

支持 chrome.management 的以下事件:

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

See official documentation for more information.

chrome.runtime​

支持 chrome.runtime 的以下属性:

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

支持 chrome.runtime 的以下方法:

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

支持 chrome.runtime 的以下事件:

  • 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 的所有功能。

See official documentation for more information.

chrome.storage​

支持 chrome.storage 的以下方法:

  • chrome.storage.local

chrome.storage.sync and chrome.storage.managed are not supported.

See official documentation for more information.

chrome.tabs​

支持 chrome.tab 的以下方法:

  • chrome.tabs.sendMessage
  • chrome.tabs.reload
  • chrome.tabs.executeScript
  • chrome.tabs.query (部分支持)
    • supported properties: url, title, audible, active, muted.
  • chrome.tabs.update (部分支持)
    • 支持的属性:url,muted。

[!NOTE] In Chrome, passing -1 as a tab ID signifies the "currently active tab". 因为 Electron 没有这样的概念,通过 -1 作为选项卡ID不支持而且会报错。

See official documentation for more information.

chrome.webRequest​

支持这些 API 的所有功能。

[!NOTE] Electron's webRequest module takes precedence over chrome.webRequest if there are conflicting handlers.

See official documentation for more information.