Class: Extensions
Class: Extensions
Load and interact with extensions.
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.
Instances of the Extensions
class are accessed by using extensions
property of
a Session
.
实例事件
The following events are available on instances of Extensions
:
Event: 'extension-loaded'
返回:
event
Eventextension
Extension
在扩展插件加载完成后触发。 当一个扩展插件被添加到 "enabled" 的扩展插件集合内部时, 将自动触发 这包括:
- Extensions being loaded from
Extensions.loadExtension
. - 扩展插件正在被重新加载:
- 由于崩溃
- if the extension requested it (
chrome.runtime.reload()
).
Event: 'extension-unloaded'
返回:
event
Eventextension
Extension
当一个扩展插件被卸载后触发。 This occurs when
Session.removeExtension
is called.
Event: 'extension-ready'
返回:
event
Eventextension
Extension
当一个扩展插件加载完成,同时所有必要的浏览器状态也初始化完毕,允许启动插件背景页面时, 将触发此事件。
实例方法
The following methods are available on instances of Extensions
:
extensions.loadExtension(path[, options])
path
string - Path to a directory containing an unpacked Chrome extension
Returns Promise<Extension>
- resolves when the extension is loaded.
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.
Note that Electron does not support the full range of Chrome extensions APIs. See Supported Extensions APIs for more details on what is supported.
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) 的扩展。
Note: This API cannot be called before the ready
event of the app
module
is emitted.
Note: Loading extensions into in-memory (non-persistent) sessions is not supported and will throw an error.
extensions.removeExtension(extensionId)
extensionId
string - ID of extension to remove
卸载扩展。
Note: This API cannot be called before the ready
event of the app
module
is emitted.
extensions.getExtension(extensionId)
extensionId
string - ID of extension to query
Returns Extension | null
- The loaded extension with the given ID.
Note: This API cannot be called before the ready
event of the app
module
is emitted.
extensions.getAllExtensions()
Returns Extension[]
- A list of all loaded extensions.
Note: This API cannot be called before the ready
event of the app
module
is emitted.