Class: WebRequest
Class: WebRequest
Intercept and modify the contents of a request at various stages of its lifetime.
Процесс: Main
Этот класс не экспортируется из модуля 'electron'
. Он доступен только в качестве возвращаемого значения других методов в Electron API.
Instances of the WebRequest
class are accessed by using the webRequest
property of a Session
.
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.
⚠️ Only the last attached listener
will be used. Passing null
as listener
will unsubscribe from the event.
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.
For certain events the listener
is passed with a callback
, which should be called with a response
object when listener
has done its work.
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 })
})
Методы экземпляра
Для экземпляров WebRequest
доступны следующие методы:
webRequest.onBeforeRequest([filter, ]listener)
filter
WebRequestFilter (optional)listener
Function | null- Объект
details
id
Integerurl
stringmethod
stringwebContentsId
Integer (опционально)webContents
WebContents (optional)frame
WebFrameMain (optional)resourceType
string - Can bemainFrame
,subFrame
,stylesheet
,script
,image
,font
,object
,xhr
,ping
,cspReport
,media
,webSocket
orother
.referrer
stringtimestamp
DoubleuploadData
UploadData[]
callback
Functionresponse
Objectcancel
boolean (опционально)redirectURL
string (optional) - The original request is prevented from being sent or completed and is instead redirected to the given URL.
- Объект
The listener
will be called with listener(details, callback)
when a request is about to occur.
uploadData
является массивом объектов UploadData
.
The callback
has to be called with an response
object.
Some examples of valid 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/'