跳转到主内容

Chrome 扩展支持

Electron 支持 Chrome 扩展API的子集, 主要是支持 DevTools 扩展和 Chromium-internal 扩展,但它同时也支持一些其他扩展能。

注意:Electron 不支持商店中的任意 Chrome 扩展,Electron 项目的目标不是与 Chrome 的扩展实现完全兼容。

加载扩展

Electron 只支持加载未打包的扩展 (即不能使用 .crx 文件)。 插件会被安装到每一个会话。 要加载扩展,请调用 ses.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 (部分支持)
    • 支持的属性:urlmuted

注意: 在 Chrome 中,通过 -1 作为标签ID表示“当前活动的标签”。 因为 Electron 没有这样的概念,通过 -1 作为选项卡ID不支持而且会报错。

See official documentation for more information.

chrome.webRequest

支持这些 API 的所有功能。

注意: 如果有冲突需要处理,Electron 的 webRequest 模块将优先于 chrome.web

See official documentation for more information.