Skip to main content

类:ServiceWorkers

类:ServiceWorkers

¥Class: ServiceWorkers

查询并接收来自会话活动服务工作者的事件。

¥Query and receive events from a sessions active service workers.

进程:主进程
该类不是从 '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.

通过使用 SessionserviceWorkers 属性来访问 ServiceWorkers 类的实例。

¥Instances of the ServiceWorkers class are accessed by using serviceWorkers property of a Session.

例如:

¥For example:

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)
)
})

实例事件

¥Instance Events

以下事件在 ServiceWorkers 的实例上可用:

¥The following events are available on instances of ServiceWorkers:

事件:'console-message'

¥Event: 'console-message'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • messageDetails 对象 - 有关控制台消息的信息

    ¥messageDetails Object - Information about the console message

    • message 字符串 - 实际的控制台消息

      ¥message string - The actual console message

    • versionId 数字 - 发送日志消息的 Service Worker 的版本 ID

      ¥versionId number - The version ID of the service worker that sent the log message

    • source 字符串 - 此消息的来源类型。可以是 javascriptxmlnetworkconsole-apistoragerenderingsecuritydeprecationworkerviolationinterventionrecommendationother

      ¥source string - The type of source for this message. Can be javascript, xml, network, console-api, storage, rendering, security, deprecation, worker, violation, intervention, recommendation or other.

    • level 数字 - 日志级别,从 0 到 3。按照顺序,它匹配 verboseinfowarningerror

      ¥level number - The log level, from 0 to 3. In order it matches verbose, info, warning and error.

    • sourceUrl 字符串 - 消息来自的 URL

      ¥sourceUrl string - The URL the message came from

    • lineNumber 数字 - 触发此控制台消息的源的行号

      ¥lineNumber number - The line number of the source that triggered this console message

当服务工作线程将某些内容记录到控制台时发出。

¥Emitted when a service worker logs something to the console.

事件:'registration-completed'

¥Event: 'registration-completed'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • details 对象 - 有关注册服务工作进程的信息

    ¥details Object - Information about the registered service worker

    • scope 字符串 - Service Worker 注册的基本 URL

      ¥scope string - The base URL that a service worker is registered for

当 Service Worker 注册后发出。可能会在对 navigator.serviceWorker.register('/sw.js') 的调用成功解析后或加载 Chrome 扩展程序时发生。

¥Emitted when a service worker has been registered. Can occur after a call to navigator.serviceWorker.register('/sw.js') successfully resolves or when a Chrome extension is loaded.

事件:'running-status-changed' 实验

¥Event: 'running-status-changed' Experimental

返回:

¥Returns:

  • details 事件<>

    ¥details Event<>

    • versionId 数字 - 更新的服务工作者版本的 ID

      ¥versionId number - ID of the updated service worker version

    • runningStatus 字符串 - 运行状态。可能的值包括 startingrunningstoppingstopped

      ¥runningStatus string - Running status. Possible values include starting, running, stopping, or stopped.

当服务工作者的运行状态发生变化时发出。

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

实例方法

¥Instance Methods

以下方法可用于 ServiceWorkers 的实例:

¥The following methods are available on instances of ServiceWorkers:

serviceWorkers.getAllRunning()

返回 Record<number, ServiceWorkerInfo> - 一个 ServiceWorker 信息 对象,其中键是 Service Worker 版本 ID,值是有关该 Service Worker 的信息。

¥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 数字 - 服务工作者版本的 ID

    ¥versionId number - ID of the service worker version

返回 ServiceWorkerInfo - 有关该服务工作进程的信息

¥Returns ServiceWorkerInfo - Information about this service worker

如果 Service Worker 不存在或未运行,此方法将抛出异常。

¥If the service worker does not exist or is not running this method will throw an exception.

serviceWorkers.getFromVersionID(versionId) 已弃用

¥serviceWorkers.getFromVersionID(versionId) Deprecated

  • versionId 数字 - 服务工作者版本的 ID

    ¥versionId number - ID of the service worker version

返回 ServiceWorkerInfo - 有关该服务工作进程的信息

¥Returns ServiceWorkerInfo - Information about this service worker

如果 Service Worker 不存在或未运行,此方法将抛出异常。

¥If the service worker does not exist or is not running this method will throw an exception.

已弃用:使用新的 serviceWorkers.getInfoFromVersionID API。

¥Deprecated: Use the new serviceWorkers.getInfoFromVersionID API.

serviceWorkers.getWorkerFromVersionID(versionId) 实验性的

¥serviceWorkers.getWorkerFromVersionID(versionId) Experimental

  • versionId 数字 - 服务工作者版本的 ID

    ¥versionId number - ID of the service worker version

返回 ServiceWorkerMain | undefined - 与给定版本 ID 关联的服务工作线程的实例。如果没有关联版本,或者其运行状态已更改为 'stopped',则将返回 undefined

¥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) 实验性的

¥serviceWorkers.startWorkerForScope(scope) Experimental

  • scope 字符串 - 要启动的服务工作线程的范围。

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

返回 Promise<ServiceWorkerMain> - 启动服务工作线程时与其一起解析。

¥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)
}
}
})