【问题标题】:PhantomJS: onResourceRequested won't fire for second page.open() on same domainPhantomJS:onResourceRequested 不会为同一域上的第二个 page.open() 触发
【发布时间】:2014-06-18 09:11:07
【问题描述】:

我有两个连续运行的 page.open 调用。当请求资源时,我将给定资源的 url 输出到控制台。

var page = require('webpage').create();

var resourceCounter = 0;

page.onResourceRequested = function (resource) {
  console.log(resourceCounter++ + '.\t', resource.url);
};

page.open('http://phantomjs.org');

setTimeout(function () {
  resourceCounter = 0;
  page.open('http://phantomjs.org/documentation');
}, 5000);

setTimeout(function(){
  phantom.exit();
}, 10000);

如果我尝试打开不同域上的两个 URL,一切都会按预期进行——我会看到页面请求资源的完整列表。但是,如果我两次请求同一个页面,或者来自同一个域的两个页面(如上面的示例),而不是看到两次列出的页面请求的资源,我会看到:

Resource requests for: http://phantomjs.org/
0    http://phantomjs.org/
1    http://phantomjs.org/
2    http://phantomjs.org/
3    http://fonts.googleapis.com/css?family=Droid+Sans:400,700
4    http://phantomjs.org/css/main.css
5    http://www.google-analytics.com/ga.js
6    https://s3.amazonaws.com/github/ribbons/forkme_left_white_ffffff.png
7    http://phantomjs.org/img/phantomjs-logo.png
8    http://phantomjs.org/img/icon-release.png
9    http://phantomjs.org/img/icon-ml.png
10   http://phantomjs.org/img/icon-bugs.png
11   http://www.google-analytics.com/__utm.gif?utmwv=5.5.0&utms=1&utmn=1675561797&utmhn=phantomjs.org&utmcs=UTF-8&utmsr=2560x1440&utmvp=400x300&utmsc=32-bit&utmul=en-us&utmje=0&utmfl=-&utmdt=PhantomJS%20%7C%20PhantomJS&utmhid=1661770756&utmr=-&utmp=%2F&utmht=1398973179893&utmac=UA-21665893-1&utmcc=__utma%3D205055825.123505717.1398973180.1398973180.1398973180.1%3B%2B__utmz%3D205055825.1398973180.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=q~

Resource requests for: http://phantomjs.org/documentation/
0    http://phantomjs.org/documentation/
1    http://www.google-analytics.com/__utm.gif?utmwv=5.5.0&utms=2&utmn=1424746795&utmhn=phantomjs.org&utmcs=UTF-8&utmsr=2560x1440&utmvp=400x300&utmsc=32-bit&utmul=en-us&utmje=0&utmfl=-&utmdt=Documentation%20%7C%20PhantomJS&utmhid=873610113&utmr=-&utmp=%2Fdocumentation%2F&utmht=1398973180268&utmac=UA-21665893-1&utmcc=__utma%3D205055825.123505717.1398973180.1398973180.1398973180.1%3B%2B__utmz%3D205055825.1398973180.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=q~

换句话说,通过page.onResourceRequested 为第二个 page.open() 调用捕获的结果将被截断。

注意:我在禁用缓存的情况下看到此行为。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript phantomjs


    【解决方案1】:

    由于内存缓存,--disk-cache=false 将无济于事。正如herehere 所讨论的,此功能尚不可用,将成为phantomJS 2 的一部分。您可以做的是下载当前开发(更多info here)并在您身边构建它,因为该功能已经there.

    【讨论】:

    • 谢谢——我找不到这个!
    • 我正在运行 PhantomJS 2,但我没有看到与 page.settings.clearMemoryCaches = true; 有任何区别。也许 API 发生了变化?
    • 哦不,你是对的..阅读this comment。看起来补丁被拒绝了...
    • 也许您可以应用该补丁以满足您的需要,但这可能会在将来的新版本中引起一些问题。只要留意它。希望这会有所帮助。
    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2021-01-26
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多