javascript - PhantomJS 上的阻止请求

标签 javascript request phantomjs

假设我在 PhantomJS 脚本上有一个 page.onResourceRequested(request) 函数。如何根据请求中的某些值阻止请求?

最佳答案

哦,我刚刚想通了。 onResourceRequest 可以接受两个参数,第一个是 requestData,第二个是 networkRequest,它允许对请求进行操作。

Quote from PhantomJS API documentation:

This callback is invoked when the page requests a resource. The first argument to the callback is the requestData metadata object. The second argument is the networkRequest object itself.

The networkRequest object contains these functions:

abort() : aborts the current network request. Aborting the current network request will invoke onResourceError callback.

changeUrl(newUrl) : changes the current URL of the network request. By calling networkRequest.changeUrl(newUrl), we can change the request url to the new url. This is an excellent and only way to provide alternative implementation of a remote resource. (see Example-2)

setHeader(key, value)

关于javascript - PhantomJS 上的阻止请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258002/

相关文章:

javascript - PhantomJs:我可以从具有动态设置内容的页面访问相关 Assets 吗?

Javascript 代码未使用 python 代码提取

javascript - 从函数内部调用外部函数 - Javascript

javascript - 将 JSON Key 设置为在循环内递增

node.js - Nodejs + mikeal/Request模块,如何关闭请求或增加MaxSockets

javascript - Angular.js 请求/响应 Node.js

Symfony 重定向请求

phantomjs - PhantomJS:调用电话时指定用户代理

javascript - PhoneGap 安卓市场发布

javascript - jQuery .push 到 .get 调用中的数组给出空结果