nightmare-webrequest-addon v1.0.2
nightmare-webrequest-addon
The
webRequestAPI 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.
Install
npm install --save nightmare-webrequest-addonUsage
nightmare.onBeforeRequest([filter, ]listener)
filterObjectlistenerFunction
The listener will be called with listener(details, callback) when a request
is about to occur.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleuploadDataArray (optional)
callbackFunction
The uploadData is an array of data objects:
dataObjectbytesBuffer - Content being sent.fileString - Path of file being uploaded.
The callback has to be called with an response object:
responseObjectcancelBoolean (optional)redirectURLString (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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleuploadDataArray (optional)
See the example.
nightmare.onBeforeSendHeaders([filter, ]listener)
filterObjectlistenerFunction
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.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublerequestHeadersObject
callbackFunction
The callback has to be called with an response object:
responseObjectcancelBoolean (optional)requestHeadersObject (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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublerequestHeadersObject
See the example.
nightmare.onSendHeaders([filter, ]listener)
filterObjectlistenerFunction
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.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublerequestHeadersObject
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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublerequestHeadersObject
See the example.
nightmare.onHeadersReceived([filter,]listener)
filterObjectlistenerFunction
The listener will be called with listener(details, callback) when HTTP
response headers of a request have been received.
detailsObjectidStringurlStringmethodStringresourceTypeStringtimestampDoublestatusLineStringstatusCodeIntegerresponseHeadersObject
callbackFunction
The callback has to be called with an response object:
responseObjectcancelBooleanresponseHeadersObject (optional) - When provided, the server is assumed to have responded with these headers.statusLineString (optional) - Should be provided when overridingresponseHeadersto 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:
detailsObjectidStringurlStringmethodStringresourceTypeStringtimestampDoublestatusLineStringstatusCodeIntegerresponseHeadersObject
See the example.
nightmare.onResponseStarted([filter, ]listener)
filterObjectlistenerFunction
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.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleresponseHeadersObjectfromCacheBoolean - Indicates whether the response was fetched from disk cache.statusCodeIntegerstatusLineString
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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleresponseHeadersObjectfromCacheBoolean - Indicates whether the response was fetched from disk cache.statusCodeIntegerstatusLineString
See the example.
nightmare.onBeforeRedirect([filter, ]listener)
filterObjectlistenerFunction
The listener will be called with listener(details) when a server initiated
redirect is about to occur.
detailsObjectidStringurlStringmethodStringresourceTypeStringtimestampDoubleredirectURLStringstatusCodeIntegeripString (optional) - The server IP address that the request was actually sent to.fromCacheBooleanresponseHeadersObject
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:
detailsObjectidStringurlStringmethodStringresourceTypeStringtimestampDoubleredirectURLStringstatusCodeIntegeripString (optional) - The server IP address that the request was actually sent to.fromCacheBooleanresponseHeadersObject
See the example.
nightmare.onCompleted([filter, ]listener)
filterObjectlistenerFunction
The listener will be called with listener(details) when a request is
completed.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleresponseHeadersObjectfromCacheBooleanstatusCodeIntegerstatusLineString
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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoubleresponseHeadersObjectfromCacheBooleanstatusCodeIntegerstatusLineString
See the example.
nightmare.onErrorOccurred([filter, ]listener)
filterObjectlistenerFunction
The listener will be called with listener(details) when an error occurs.
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublefromCacheBooleanerrorString - 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:
detailsObjectidIntegerurlStringmethodStringresourceTypeStringtimestampDoublefromCacheBooleanerrorString - The error description.
See the example.
Thanks to @rosshinkley
This module is heavily inspired by nightmare-load-filter
License
MIT