类: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.
通过使用 Session
的 serviceWorkers
属性来访问 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
字符串 - 此消息的来源类型。可以是javascript
、xml
、network
、console-api
、storage
、rendering
、security
、deprecation
、worker
、violation
、intervention
、recommendation
或other
。¥
source
string - The type of source for this message. Can bejavascript
,xml
,network
,console-api
,storage
,rendering
,security
,deprecation
,worker
,violation
,intervention
,recommendation
orother
. -
level
数字 - 日志级别,从 0 到 3。按照顺序,它匹配verbose
、info
、warning
和error
。¥
level
number - The log level, from 0 to 3. In order it matchesverbose
,info
,warning
anderror
. -
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.
实例方法
¥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.getFromVersionID(versionId)
-
versionId
数字¥
versionId
number
返回 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.