PhantomJS 监听资源加载:onResourceReceived 响应参数与用法


发布日期 : 2021-11-30 02:03:35 UTC

访问量: 10 次浏览

PhantomJS onResourceReceived()

此回调函数在页面请求的资源被接收时被调用。它以 response 作为参数。

response对象具有以下详细信息:

  • Id- 被请求资源的编号。
  • URL- 请求的URL。
  • Time- 包含响应日期的日期对象。
  • Headers- HTTP头部。
  • BodySize- 已接收内容的解压缩大小。
  • ContentType- 如果指定了,则为内容类型。
  • RedirectURL- 如果有重定向,则为重定向的URL。
  • Stage- 值为“start”和“end”。
  • Status- HTTP状态码,例如200。
  • StatusText- 200对应的HTTP状态文本,为“OK”。

语法

其语法如下:

page.onResourceReceived = function(response) {}

示例

下面的代码演示了使用 onResourceReceived() 方法。

var wpage = require('webpage').create();
wpage.onResourceReceived = function(response) {
console.log(JSON.stringify(response));
}
wpage.open('http://localhost/tasks/prompt.html', function(status) {
});

上述程序生成以下输出

{"body":"","bodySize":231,"contentType":"text/html","headers":[{"name":"Date",
"value":"Sun, 07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":"Sun,
07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e754eee893517e5\""},
{"name":"Accept-Ranges","value":"bytes"},{"name":"ContentLength","value":"231"},
{"name":"Keep-Alive","value":"timeout=5, max=100"},{"name":"Connection","value":
"Keep-Alive"},{"name":"ContentType","value":"text/html"}],"id":1,"redirectURL":null,
"stage":"start","status": 200,"statusText":"OK","time":"2017-05-07T12:59:17.440Z",
"url": "http://localhost/tasks/prompt.html"}

{"contentType":"text/html","headers":[{"name":"Date","value":"Sun,
07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":
"Sun, 07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e7-54eee893517e5\""},
{"name":"AcceptRanges","value":"bytes"},{"name":"Content-Length","value":"231"},
{"name":"KeepAlive","value":"timeout=5, max=100"},{"name":"Connection",
"value":"KeepAlive"},{"name":"Content-Type", "value":"text/html"}],
"id":1,"redirectURL":null,"stage":"end","status":200,"statusText":"OK",
"time":"2017-0507T12:59:17.486Z","url":"http://localhost/tasks/prompt.html"}