假设我在 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/