类:WebRequest
类:WebRequest
¥Class: WebRequest
在请求生命周期的各个阶段拦截并修改请求的内容。
¥Intercept and modify the contents of a request at various stages of its lifetime.
进程:主进程
该类不是从 '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.
WebRequest 类的实例可以通过使用 Session 的 webRequest 属性来访问。
¥Instances of the WebRequest class are accessed by using the webRequest
property of a Session.
WebRequest 的方法接受可选的 filter 和 listener。当 API 事件发生时,listener 将与 listener(details) 一起调用。details 对象描述请求。
¥The methods of WebRequest accept an optional filter and a listener. The
listener will be called with listener(details) when the API's event has
happened. The details object describes the request.
⚠️ 仅使用最后附加的 listener。将 null 作为 listener 传递将取消订阅该事件。
¥⚠️ Only the last attached listener will be used. Passing null as listener will unsubscribe from the event.
filter 对象有一个 urls 属性,它是一个 URL 模式数组,用于过滤掉与 URL 模式不匹配的请求。如果省略 filter,则所有请求都将被匹配。
¥The filter object has a urls property which is an Array of URL
patterns that will be used to filter out the requests that do not match the URL
patterns. If the filter is omitted then all requests will be matched.
对于某些事件,listener 与 callback 一起传递,当 listener 完成其工作时,应使用 response 对象调用 callback。
¥For certain events the listener is passed with a callback, which should be
called with a response object when listener has done its work.
为请求添加 User-Agent 标头的示例:
¥An example of adding User-Agent header for requests:
const { session } = require('electron')
// Modify the user agent for all requests to the following urls.
const filter = {
urls: ['https://*.github.com/*', '*://electron.github.io/*']
}
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
details.requestHeaders['User-Agent'] = 'MyAgent'
callback({ requestHeaders: details.requestHeaders })
})
实例方法
¥Instance Methods
以下方法可用于 WebRequest 的实例:
¥The following methods are available on instances of WebRequest:
webRequest.onBeforeRequest([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
uploadDataUploadData[]
-
-
callback函数¥
callbackFunction-
response对象¥
responseObject-
cancel布尔值(可选)¥
cancelboolean (optional) -
redirectURL字符串(可选) - 原始请求无法发送或完成,而是重定向到给定的 URL。¥
redirectURLstring (optional) - The original request is prevented from being sent or completed and is instead redirected to the given URL.
-
-
-
当请求即将发生时,listener 将与 listener(details, callback) 一起调用。
¥The listener will be called with listener(details, callback) when a request
is about to occur.
uploadData 是 UploadData 对象的数组。
¥The uploadData is an array of UploadData objects.
必须使用 response 对象调用 callback。
¥The callback has to be called with an response object.
有效 urls 的一些示例:
¥Some examples of valid urls:
'<all_urls>'
'http://foo:1234/'
'http://foo.com/'
'http://foo:1234/bar'
'*://*/*'
'*://example.com/*'
'*://example.com/foo/*'
'http://*.foo:1234/'
'file://foo:1234/bar'
'http://foo:*/'
'*://www.foo.com/'
webRequest.onBeforeSendHeaders([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
uploadDataUploadData[](可选)¥
uploadDataUploadData[] (optional) -
requestHeaders记录<字符串,字符串>¥
requestHeadersRecord<string, string>
-
-
callback函数¥
callbackFunction-
beforeSendResponse对象¥
beforeSendResponseObject-
cancel布尔值(可选)¥
cancelboolean (optional) -
requestHeadersRecord<string,字符串 | string[]>(可选) - 提供后,将使用这些标头发出请求。¥
requestHeadersRecord<string, string | string[]> (optional) - When provided, request will be made with these headers.
-
-
-
一旦请求标头可用,在发送 HTTP 请求之前,将使用 listener(details, callback) 调用 listener。这可能发生在与服务器建立 TCP 连接之后、发送任何 http 数据之前。
¥The listener will be called with listener(details, callback) before sending
an HTTP request, once the request headers are available. This may occur after a
TCP connection is made to the server, but before any http data is sent.
必须使用 response 对象调用 callback。
¥The callback has to be called with a response object.
webRequest.onSendHeaders([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
requestHeaders记录<字符串,字符串>¥
requestHeadersRecord<string, string>
-
-
在将请求发送到服务器之前,将使用 listener(details) 调用 listener,在触发此监听器时可以看到先前 onBeforeSendHeaders 响应的修改。
¥The listener will be called with listener(details) just before a request is
going to be sent to the server, modifications of previous onBeforeSendHeaders
response are visible by the time this listener is fired.
webRequest.onHeadersReceived([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
statusLine字符串¥
statusLinestring -
statusCode整数¥
statusCodeInteger -
responseHeaders记录<字符串, string[]>(可选)¥
responseHeadersRecord<string, string[]> (optional)
-
-
callback函数¥
callbackFunction-
headersReceivedResponse对象¥
headersReceivedResponseObject-
cancel布尔值(可选)¥
cancelboolean (optional) -
responseHeadersRecord<string,字符串 | string[]>(可选) - 当提供时,假定服务器已使用这些标头进行响应。¥
responseHeadersRecord<string, string | string[]> (optional) - When provided, the server is assumed to have responded with these headers. -
statusLine字符串(可选) - 当覆盖responseHeaders以更改标头状态时应提供,否则将使用原始响应标头的状态。¥
statusLinestring (optional) - Should be provided when overridingresponseHeadersto change header status otherwise original response header's status will be used.
-
-
-
当收到请求的 HTTP 响应标头时,将使用 listener(details, callback) 调用 listener。
¥The listener will be called with listener(details, callback) when HTTP
response headers of a request have been received.
必须使用 response 对象调用 callback。
¥The callback has to be called with a response object.
webRequest.onResponseStarted([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
responseHeaders记录<字符串, string[]>(可选)¥
responseHeadersRecord<string, string[]> (optional) -
fromCache布尔值 - 指示是否从磁盘缓存中获取响应。¥
fromCacheboolean - Indicates whether the response was fetched from disk cache. -
statusCode整数¥
statusCodeInteger -
statusLine字符串¥
statusLinestring
-
-
当收到响应正文的第一个字节时,将使用 listener(details) 调用 listener。对于 HTTP 请求,这意味着状态行和响应标头可用。
¥The listener will be called with listener(details) when first byte of the
response body is received. For HTTP requests, this means that the status line
and response headers are available.
webRequest.onBeforeRedirect([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
redirectURL字符串¥
redirectURLstring -
statusCode整数¥
statusCodeInteger -
statusLine字符串¥
statusLinestring -
ip字符串(可选) - 请求实际发送到的服务器 IP 地址。¥
ipstring (optional) - The server IP address that the request was actually sent to. -
fromCache布尔值¥
fromCacheboolean -
responseHeaders记录<字符串, string[]>(可选)¥
responseHeadersRecord<string, string[]> (optional)
-
-
当服务器发起的重定向即将发生时,listener 将与 listener(details) 一起调用。
¥The listener will be called with listener(details) when a server initiated
redirect is about to occur.
webRequest.onCompleted([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
responseHeaders记录<字符串, string[]>(可选)¥
responseHeadersRecord<string, string[]> (optional) -
fromCache布尔值¥
fromCacheboolean -
statusCode整数¥
statusCodeInteger -
statusLine字符串¥
statusLinestring -
error字符串¥
errorstring
-
-
当请求完成时,listener 将与 listener(details) 一起调用。
¥The listener will be called with listener(details) when a request is
completed.
webRequest.onErrorOccurred([filter, ]listener)
-
filter网络请求过滤器(可选)¥
filterWebRequestFilter (optional) -
listenerFunction | null-
details对象¥
detailsObject-
id整数¥
idInteger -
url字符串¥
urlstring -
method字符串¥
methodstring -
webContentsId整数(可选)¥
webContentsIdInteger (optional) -
webContents网页内容(可选)¥
webContentsWebContents (optional) -
frameWebFrameMain | null(可选) - 请求框架。如果在框架导航或被销毁后访问,则可能是null。¥
frameWebFrameMain | null (optional) - Requesting frame. May benullif accessed after the frame has either navigated or been destroyed. -
resourceType字符串 - 可以是mainFrame、subFrame、stylesheet、script、image、font、object、xhr、ping、cspReport、media、webSocket或other。¥
resourceTypestring - Can bemainFrame,subFrame,stylesheet,script,image,font,object,xhr,ping,cspReport,media,webSocketorother. -
referrer字符串¥
referrerstring -
timestamp双倍的¥
timestampDouble -
fromCache布尔值¥
fromCacheboolean -
error字符串 - 错误描述。¥
errorstring - The error description.
-
-
当发生错误时,listener 将与 listener(details) 一起调用。
¥The listener will be called with listener(details) when an error occurs.