PhantomJS - onResourceRequested()



This is invoked when the page requests a resource. It has two arguments requestData and networkRequest.

The RequestData object has the following details −

  • Id − The number of the requested resource.

  • Method − The http method.

  • URL − The URL of the requested resource.

  • Time − The date object containing the date of the request.

  • Headers − The list of the http headers.

The NetworkRequest object has the following details −

  • Abort () − It aborts the current network request. Aborting the current network request will invoke the onResourceError callback function.

  • ChangeUrl (newurl) − The requested URL can be changed to a new file using this function.

  • SetHeader − It has the key and the value.

Syntax

Its syntax is as follows −

page.onResourceRequested = function(requestData, networkRequest) {}

Example

var wpage = require('webpage').create(); 
wpage.onResourceRequested = function(requestdata , networkdata) { 
   console.log("Data from requestdata:"); 
   console.log(JSON.stringify(requestdata));   
   console.log("Data from networkdata"); 
   console.log(JSON.stringify(networkdata));   
} 
wpage.open('http://localhost/tasks/request.html', function(status) { 
});

The above program generates the following output.

Data from requestdata: 
{"headers":[{"name":"Accept","value":"text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8"},{"name":"User-Agent","value":"Mozilla/5.0 
(Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) 
PhantomJS/2.1.1 Safari/538.1"}], "id":1,"method":"GET","time":"2017-0507T13:25:36.454Z",
"url":"http://localhost/tasks/request.html"} 
Data from networkdata 
{"objectName":""}
phantomjs_webpage_module_events_callbacks.htm
Advertisements