跳转到主内容

Class: ServiceWorkers

Class: ServiceWorkers

从活跃的 service worker 会话中查询和接收事件

Process: Main
This class is not exported from the 'electron' module. 它只能作为 Electron API 中其他方法的返回值。

通过SessionserviceWorkers属性来访问ServiceWorkers的实例

例如:

const { session } = require('electron')

// Get all service workers.
console.log(session.defaultSession.serviceWorkers.getAllRunning())

// Handle logs and get service worker info
session.defaultSession.serviceWorkers.on('console-message', (event, messageDetails) => {
console.log(
'Got service worker message',
messageDetails,
'from',
session.defaultSession.serviceWorkers.getFromVersionID(messageDetails.versionId)
)
})

实例事件

ServiceWorkers实例中有下列事件:

Event: 'console-message'

返回:

  • event Event
  • messageDetails Object - 有关控制台消息的信息
    • message string - 实际控制台消息
    • versionId number - 发送消息日志的 service worker 的版本 ID
    • source string - 消息源的类型 可以是 javascript, xml, network, console-api, storage, rendering, security, deprecation, worker, violation, intervention, recommendationother
    • level number - 日志等级,从 0 到 3 。 按顺序匹配 verbose, info, warningerror.
    • sourceUrl string - 消息来源的URL
    • lineNumber number - 触发此控制台消息的源码的行号。

当一个service worker记录日志到控制台的时候将自动触发此事件

Event: 'registration-completed'

返回:

  • event Event
  • details Object - 有关 service worker 注册的信息
    • scope string - 当前 service worker 所注册在的URL

当一个service worker已经被注册完成的时候触发此事件。 注册完成分两种情况, 一个是调用navigator.serviceWorker.register('/sw.js')方法成功,另一个是当Chrome的扩展加载结束。

Event: 'running-status-changed' Experimental

返回:

  • details Event<>
    • versionId number - ID of the updated service worker version
    • runningStatus string - Running status. Possible values include starting, running, stopping, or stopped.

Emitted when a service worker's running status has changed.

实例方法

ServiceWorkers实例对象中,有以下方法:

serviceWorkers.getAllRunning()

Returns Record<number, ServiceWorkerInfo> - A ServiceWorkerInfo object where the keys are the service worker version ID and the values are the information about that service worker.

serviceWorkers.getInfoFromVersionID(versionId)

  • versionId number - ID of the service worker version

Returns ServiceWorkerInfo - Information about this service worker

如果 service worker 不存在或者停止运行,此方法将抛出异常。

serviceWorkers.getFromVersionID(versionId) 已废弃

  • versionId number - ID of the service worker version

Returns ServiceWorkerInfo - Information about this service worker

如果 service worker 不存在或者停止运行,此方法将抛出异常。

Deprecated: Use the new serviceWorkers.getInfoFromVersionID API.

serviceWorkers.getWorkerFromVersionID(versionId) 实验功能

  • versionId number - ID of the service worker version

Returns ServiceWorkerMain | undefined - Instance of the service worker associated with the given version ID. If there's no associated version, or its running status has changed to 'stopped', this will return undefined.

serviceWorkers.startWorkerForScope(scope) 实验功能

  • scope string - The scope of the service worker to start.

Returns Promise<ServiceWorkerMain> - Resolves with the service worker when it's started.

Starts the service worker or does nothing if already running.

const { app, session } = require('electron')
const { serviceWorkers } = session.defaultSession

// Collect service workers scopes
const workerScopes = Object.values(serviceWorkers.getAllRunning()).map((info) => info.scope)

app.on('browser-window-created', async (event, window) => {
for (const scope of workerScopes) {
try {
// Ensure worker is started
const serviceWorker = await serviceWorkers.startWorkerForScope(scope)
serviceWorker.send('window-created', { windowId: window.id })
} catch (error) {
console.error(`Failed to start service worker for ${scope}`)
console.error(error)
}
}
})