The
webRequest
API set allows to intercept and modify contents of a request at various stages of its lifetime. (from https://github.com/electron/electron/blob/master/docs/api/session.md#seswebrequest)
This is the wrapper of Electron API session.webRequest
.
npm install --save nightmare-webrequest-addon
filter
Objectlistener
Function
The listener
will be called with listener(details, callback)
when a request
is about to occur.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleuploadData
Array (optional)
callback
Function
The uploadData
is an array of data
objects:
data
Objectbytes
Buffer - Content being sent.file
String - Path of file being uploaded.
The callback
has to be called with an response
object:
response
Objectcancel
Boolean (optional)redirectURL
String (optional) - The original request is prevented from being sent or completed, and is instead redirected to the given URL.
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onBeforeRequest', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleuploadData
Array (optional)
See the example.
filter
Objectlistener
Function
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.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublerequestHeaders
Object
callback
Function
The callback
has to be called with an response
object:
response
Objectcancel
Boolean (optional)requestHeaders
Object (optional) - When provided, request will be made with these headers.
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onBeforeSendHeaders', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublerequestHeaders
Object
See the example.
filter
Objectlistener
Function
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.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublerequestHeaders
Object
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onBeforeSendHeaders', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublerequestHeaders
Object
See the example.
filter
Objectlistener
Function
The listener
will be called with listener(details, callback)
when HTTP
response headers of a request have been received.
details
Objectid
Stringurl
Stringmethod
StringresourceType
Stringtimestamp
DoublestatusLine
StringstatusCode
IntegerresponseHeaders
Object
callback
Function
The callback
has to be called with an response
object:
response
Objectcancel
BooleanresponseHeaders
Object (optional) - When provided, the server is assumed to have responded with these headers.statusLine
String (optional) - Should be provided when overridingresponseHeaders
to change header status otherwise original response header's status will be used.
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onHeadersReceived', callback)
The callback
has to be called with an details
object:
details
Objectid
Stringurl
Stringmethod
StringresourceType
Stringtimestamp
DoublestatusLine
StringstatusCode
IntegerresponseHeaders
Object
See the example.
filter
Objectlistener
Function
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.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleresponseHeaders
ObjectfromCache
Boolean - Indicates whether the response was fetched from disk cache.statusCode
IntegerstatusLine
String
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onResponseStarted', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleresponseHeaders
ObjectfromCache
Boolean - Indicates whether the response was fetched from disk cache.statusCode
IntegerstatusLine
String
See the example.
filter
Objectlistener
Function
The listener
will be called with listener(details)
when a server initiated
redirect is about to occur.
details
Objectid
Stringurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleredirectURL
StringstatusCode
Integerip
String (optional) - The server IP address that the request was actually sent to.fromCache
BooleanresponseHeaders
Object
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onBeforeRedirect', callback)
The callback
has to be called with an details
object:
details
Objectid
Stringurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleredirectURL
StringstatusCode
Integerip
String (optional) - The server IP address that the request was actually sent to.fromCache
BooleanresponseHeaders
Object
See the example.
filter
Objectlistener
Function
The listener
will be called with listener(details)
when a request is
completed.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleresponseHeaders
ObjectfromCache
BooleanstatusCode
IntegerstatusLine
String
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onCompleted', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoubleresponseHeaders
ObjectfromCache
BooleanstatusCode
IntegerstatusLine
String
See the example.
filter
Objectlistener
Function
The listener
will be called with listener(details)
when an error occurs.
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublefromCache
Booleanerror
String - The error description.
Note: listener
is Electron-side context, cannot access any variable/function outside of listener
If you want to use details
Object in the nodeJS-side, use nightmare.on('onErrorOccurred', callback)
The callback
has to be called with an details
object:
details
Objectid
Integerurl
Stringmethod
StringresourceType
Stringtimestamp
DoublefromCache
Booleanerror
String - The error description.
See the example.
Thanks to @rosshinkley
This module is heavily inspired by nightmare-load-filter
MIT