當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PhantomJS onResourceRequested()用法及代碼示例


當頁麵請求資源時調用它。它有兩個參數requestDatanetworkRequest

RequestData 對象具有以下詳細信息 -

  • Id− 請求資源的編號。

  • Method− http 方法。

  • URL− 請求資源的 URL。

  • Time− 包含請求日期的日期對象。

  • Headers− http 標頭列表。

NetworkRequest 對象具有以下詳細信息 -

  • Abort ()− 中止當前網絡請求。中止當前網絡請求將調用 onResourceError 回調函數。

  • ChangeUrl (newurl)− 可以使用此函數將請求的 URL 更改為新文件。

  • SetHeader- 它有鍵和值。

用法

它的語法如下 -

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

示例

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) { 
});

上述程序生成以下內容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 - onResourceRequested()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。