类:Extensions
类:Extensions
🌐 Class: Extensions
加载并使用扩展功能。
进程: 主进程
此类未从 'electron' 模块导出。它仅作为 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:
Event: 'extension-loaded'
返回:
🌐 Returns:
event活动extension扩展
在加载扩展后触发。每当将扩展添加到“已启用”扩展集合时,就会发生这种情况。这包括:
🌐 Emitted after an extension is loaded. This occurs whenever an extension is added to the "enabled" set of extensions. This includes:
- 正在从
Extensions.loadExtension加载扩展。 - 正在重新加载扩展:
- 因车祸。
- 如果扩展请求它(
chrome.runtime.reload())。
Event: 'extension-unloaded'
返回:
🌐 Returns:
event活动extension扩展
在扩展被卸载后触发。当调用 Session.removeExtension 时会发生此情况。
🌐 Emitted after an extension is unloaded. This occurs when
Session.removeExtension is called.
Event: 'extension-ready'
返回:
🌐 Returns:
event活动extension扩展
在扩展加载并且所有必要的浏览器状态初始化完成以支持扩展后台页面启动后触发。
🌐 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 扩展程序的目录的路径
返回 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 的全部功能。有关支持的详细信息,请参阅 Supported Extensions APIs。
🌐 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.
在 app 模块的 ready 事件被触发之前,无法调用此 API。
将扩展加载到内存(非持久)会话中则不适用 支持,会报错。
extensions.removeExtension(extensionId)
extensionId字符串 - 要移除的扩展 ID
卸载扩展。
🌐 Unloads an extension.
在 app 模块的 ready 事件被触发之前,无法调用此 API。
extensions.getExtension(extensionId)
extensionId字符串 - 要查询的扩展 ID
返回 Extension | null - 具有指定 ID 的已加载扩展。
🌐 Returns Extension | null - The loaded extension with the given ID.
在 app 模块的 ready 事件被触发之前,无法调用此 API。
extensions.getAllExtensions()
返回 Extension[] - 所有已加载扩展的列表。
🌐 Returns Extension[] - A list of all loaded extensions.
在 app 模块的 ready 事件被触发之前,无法调用此 API。