Giter VIP home page Giter VIP logo

nightmare-webrequest-addon's Introduction

nightmare-webrequest-addon

js-standard-style npm version travis-ci

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.

Install

npm install --save nightmare-webrequest-addon

Usage

nightmare.onBeforeRequest([filter, ]listener)

  • filter Object
  • listener Function

The listener will be called with listener(details, callback) when a request is about to occur.

  • details Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • uploadData Array (optional)
  • callback Function

The uploadData is an array of data objects:

  • data Object
    • bytes Buffer - Content being sent.
    • file String - Path of file being uploaded.

The callback has to be called with an response object:

  • response Object
    • cancel 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • uploadData Array (optional)

See the example.

nightmare.onBeforeSendHeaders([filter, ]listener)

  • filter Object
  • listener 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • requestHeaders Object
  • callback Function

The callback has to be called with an response object:

  • response Object
    • cancel 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • requestHeaders Object

See the example.

nightmare.onSendHeaders([filter, ]listener)

  • filter Object
  • listener 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • requestHeaders 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • requestHeaders Object

See the example.

nightmare.onHeadersReceived([filter,]listener)

  • filter Object
  • listener Function

The listener will be called with listener(details, callback) when HTTP response headers of a request have been received.

  • details Object
    • id String
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • statusLine String
    • statusCode Integer
    • responseHeaders Object
  • callback Function

The callback has to be called with an response object:

  • response Object
    • cancel Boolean
    • responseHeaders Object (optional) - When provided, the server is assumed to have responded with these headers.
    • statusLine String (optional) - Should be provided when overriding responseHeaders 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 Object
    • id String
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • statusLine String
    • statusCode Integer
    • responseHeaders Object

See the example.

nightmare.onResponseStarted([filter, ]listener)

  • filter Object
  • listener 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • responseHeaders Object
    • fromCache Boolean - Indicates whether the response was fetched from disk cache.
    • statusCode Integer
    • statusLine 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • responseHeaders Object
    • fromCache Boolean - Indicates whether the response was fetched from disk cache.
    • statusCode Integer
    • statusLine String

See the example.

nightmare.onBeforeRedirect([filter, ]listener)

  • filter Object
  • listener Function

The listener will be called with listener(details) when a server initiated redirect is about to occur.

  • details Object
    • id String
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • redirectURL String
    • statusCode Integer
    • ip String (optional) - The server IP address that the request was actually sent to.
    • fromCache Boolean
    • responseHeaders 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 Object
    • id String
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • redirectURL String
    • statusCode Integer
    • ip String (optional) - The server IP address that the request was actually sent to.
    • fromCache Boolean
    • responseHeaders Object

See the example.

nightmare.onCompleted([filter, ]listener)

  • filter Object
  • listener Function

The listener will be called with listener(details) when a request is completed.

  • details Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • responseHeaders Object
    • fromCache Boolean
    • statusCode Integer
    • statusLine 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • responseHeaders Object
    • fromCache Boolean
    • statusCode Integer
    • statusLine String

See the example.

nightmare.onErrorOccurred([filter, ]listener)

  • filter Object
  • listener Function

The listener will be called with listener(details) when an error occurs.

  • details Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • fromCache Boolean
    • error 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 Object
    • id Integer
    • url String
    • method String
    • resourceType String
    • timestamp Double
    • fromCache Boolean
    • error String - The error description.

See the example.

Thanks to @rosshinkley

This module is heavily inspired by nightmare-load-filter

License

MIT

nightmare-webrequest-addon's People

Contributors

dependabot[bot] avatar kyungw00k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

imlida r0bson lekev

nightmare-webrequest-addon's Issues

calling 'onBeforeRequest' callback from nodejs side

I want to make some processing of 'details' on nodejs side and then decide whether I want to cancel that request or not. It is only possible to access details through 'onBeforeRequest' event now. But there is no access to 'callback' inside that event. Is it possible to add such functionality? If yes, can you give me a hint how to achieve this? thanks.

get to know when the ajax calls are made and done

how to know when ajax calls are made and done .....then how to check the page is fully loaded or not
am new to nightmare js
my code is here

var Nightmare = require('nightmare');
require('nightmare-webrequest-addon');

var name = "vineeth";
var cancel = false;

var nightmare = Nightmare({
show: true,
switches: {
'proxy-server': 'localhost:3128' , // set the proxy server here ...
'ignore-certificate-errors' : true
},

});

nightmare.on('onBeforeRequest', function(details, callback){
if(details && details.hasOwnProperty("resourceType")){
console.log(details.resourceType)
}
//callback(details);
})

nightmare.onBeforeRequest([ "*.css" ],function(details, callback){
if(details && details.hasOwnProperty("resourceType") && details.resourceType == "image"){
callback({ cancel : true } )
}
else{
callback({ cancel : false } )
}
})

nightmare
.goto('https://www.flipkart.com/zesture-cotton-floral-double-bedsheet/p/itmepqvqyyzznfsn?pid=BDSEPQVQCGMQRY7Y&srno=b_1_2&otracker=hp_reco_Discounts%20for%20You_2_Min%2050%25%20off_vdm%2Fuj4%2F64i_1&lid=LSTBDSEPQVQCGMQRY7YJBFIYW&fm=personalisedRecommendation/discount&iid=513dcd7a-61cb-4633-845f-bff06e73fddd.BDSEPQVQCGMQRY7Y.SEARCH')
// .wait(10000)
.end()
.then(function(ip) { // This will log the Proxy's IP
console.log('proxy IP:', ip);
});

version 1.0.1 broke compatibility with Nightmare 2.x

Hello,
you seem to have updated nightmare in dependencies. This, sadly, causes Nightmare 2.x users to have two copies of Nightmare installed - one by themselves, another by nightmare-webrequest-addon. Obviously, everything stops working.

I suggest a following fix:
`"nightmare": "^2.3.4 || ^3.0.0"

Or even, on top of that, move nightmare to peerDependencies to completely stop npm from installing nightmare and expect the user to do so manually. peerDependencies will make sure that compatible version of Nightmare is installed and the user will be warned if they run incompatible version.

This normally would be fine, but in this case, you've updated only bugfix version number, suggesting that this is a change that won't break anything for anyone. Should you want to deprecate nightmare 2.x for some reason, You should definitely go and release version 2.0.0. However, I don't see a reason for you to do so - it should work fine for older Nightmare as well.

'onBeforeRequest' with filter is not working

My code:

const filter = {
    urls: ['http://abc.com']
}
player.nightmare
.onBeforeRequest(filter, function(detail, callback) {
    callback({
        cancel: true
    });
})
.onErrorOccurred({ urls: ['http://*/*', 'https://*/*'] })
.on('onErrorOccurred', function(details) {
    console.log(details);
    if (details.resourceType == "mainFrame" && details.error == "net::ERR_BLOCKED_BY_CLIENT") {
        console.log("fail to load website: ", details.url);
        player.nightmare.end();
    }
}).goto("https://youtube.com").wait(30000);

it will return error:

{
    "message": "navigation error",
    "code": -20,
    "details": "ERR_BLOCKED_BY_CLIENT",
    "url": "https://youtube.com/"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.