类:扩展
类:扩展
¥Class: Extensions
加载扩展程序并与之交互。
¥Load and interact with extensions.
进程:主进程
该类不是从 'electron'
模块导出的。它仅可用作 Electron API 中其他方法的返回值。
¥Process: Main
This class is not exported from the 'electron'
module. It is only available as a return value of other methods in the Electron API.
通过使用 Session
的 extensions
属性来访问 Extensions
类的实例。
¥Instances of the Extensions
class are accessed by using extensions
property of
a Session
.
实例事件
¥Instance Events
以下事件在 Extensions
的实例上可用:
¥The following events are available on instances of Extensions
:
事件:'extension-loaded'
¥Event: 'extension-loaded'
返回:
¥Returns:
加载扩展后发出。每当将扩展添加到 "enabled" 扩展集时就会发生这种情况。这包括:
¥Emitted after an extension is loaded. This occurs whenever an extension is added to the "enabled" set of extensions. This includes:
-
正在从
Extensions.loadExtension
加载扩展。¥Extensions being loaded from
Extensions.loadExtension
. -
正在重新加载扩展:
¥Extensions being reloaded:
-
因车祸。
¥from a crash.
-
如果分机请求的话 (
chrome.runtime.reload()
)。¥if the extension requested it (
chrome.runtime.reload()
).
-
事件:'extension-unloaded'
¥Event: 'extension-unloaded'
返回:
¥Returns:
卸载扩展后发出。当调用 Session.removeExtension
时会发生这种情况。
¥Emitted after an extension is unloaded. This occurs when
Session.removeExtension
is called.
事件:'extension-ready'
¥Event: 'extension-ready'
返回:
¥Returns:
在加载扩展程序并初始化所有必要的浏览器状态以支持扩展程序后台页面的启动后发出。
¥Emitted after an extension is loaded and all necessary browser state is initialized to support the start of the extension's background page.
实例方法
¥Instance Methods
以下方法可用于 Extensions
的实例:
¥The following methods are available on instances of Extensions
:
extensions.loadExtension(path[, options])
-
path
字符串 - 包含解压的 Chrome 扩展程序的目录路径¥
path
string - Path to a directory containing an unpacked Chrome extension
返回 Promise<Extension>
- 加载扩展时解决。
¥Returns Promise<Extension>
- resolves when the extension is loaded.
如果无法加载扩展,此方法将引发异常。如果安装扩展时出现警告(例如,如果扩展请求 Electron 不支持的 API),那么它们将被记录到控制台。
¥This method will raise an exception if the extension could not be loaded. If there are warnings when installing the extension (e.g. if the extension requests an API that Electron does not support) then they will be logged to the console.
请注意,Electron 不支持所有 Chrome 扩展 API。有关支持内容的更多详细信息,请参阅 支持的扩展 API。
¥Note that Electron does not support the full range of Chrome extensions APIs. See Supported Extensions APIs for more details on what is supported.
请注意,在 Electron 的早期版本中,加载的扩展将被记住以供将来运行应用。这已不再是这种情况:如果你希望加载扩展,则必须在应用每次启动时调用 loadExtension
。
¥Note that in previous versions of Electron, extensions that were loaded would
be remembered for future runs of the application. This is no longer the case:
loadExtension
must be called on every boot of your app if you want the
extension to be loaded.
const { app, session } = require('electron')
const path = require('node:path')
app.whenReady().then(async () => {
await session.defaultSession.extensions.loadExtension(
path.join(__dirname, 'react-devtools'),
// allowFileAccess is required to load the devtools extension on file:// URLs.
{ allowFileAccess: true }
)
// Note that in order to use the React DevTools extension, you'll need to
// download and unzip a copy of the extension.
})
此 API 不支持加载打包的 (.crx) 扩展名。
¥This API does not support loading packed (.crx) extensions.
[!NOTE] 此 API 无法在
app
模块的ready
事件发出之前调用。¥[!NOTE] This API cannot be called before the
ready
event of theapp
module is emitted.
[!NOTE] 不支持将扩展加载到内存(非持久性)会话中,否则将引发错误。
¥[!NOTE] Loading extensions into in-memory (non-persistent) sessions is not supported and will throw an error.
extensions.removeExtension(extensionId)
-
extensionId
字符串 - 要删除的扩展 ID¥
extensionId
string - ID of extension to remove
卸载扩展。
¥Unloads an extension.
[!NOTE] 此 API 无法在
app
模块的ready
事件发出之前调用。¥[!NOTE] This API cannot be called before the
ready
event of theapp
module is emitted.
extensions.getExtension(extensionId)
-
extensionId
字符串 - 查询分机 ID¥
extensionId
string - ID of extension to query
返回 Extension | null
- 具有给定 ID 的已加载扩展。
¥Returns Extension | null
- The loaded extension with the given ID.
[!NOTE] 此 API 无法在
app
模块的ready
事件发出之前调用。¥[!NOTE] This API cannot be called before the
ready
event of theapp
module is emitted.
extensions.getAllExtensions()
返回 Extension[]
- 所有加载的扩展的列表。
¥Returns Extension[]
- A list of all loaded extensions.
[!NOTE] 此 API 无法在
app
模块的ready
事件发出之前调用。¥[!NOTE] This API cannot be called before the
ready
event of theapp
module is emitted.