如何递归使用phantomjs netlog.js以“.js”捕获所有网址响应并输出回复?

时间:2016-06-14 15:34:04

标签: javascript http phantomjs response

我目前已将netlog.js代码编辑为如下所示:

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    address;

var resUrl;

if (system.args.length === 1) {
    console.log('Usage: netlog.js <some URL>');
    phantom.exit(1);
} else {
    address = system.args[1];

    page.onResourceRequested = function (req) {
       console.log('requested: ' + JSON.stringify(req, undefined, 4));
    };


    page.onResourceReceived = function (res) {
        resUrl = res["url"]  
        if (resUrl.indexOf(".js") !== -1){
            console.log('\nJS Call URL:' + resUrl);
            var page2 = require('webpage').create(), address2 = resUrl;
            page2.onResourceRequested = function (req2){
                console.log('\tLevel 2 requested: ' + JSON.stringify(req2, undefined, 4));
            };
            page2.onResouceReceived = function (req2){
                console.log('\t Level 2 URL:' + req2["url"]);
            };
        }
        else{
            console.log('URL:' + resUrl);
        }
    };

    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        }
        phantom.exit();
    });
}

但是,它只输出1级URL响应。我希望它能在这些带有.js的URL上重新运行netlog.js,因为这是我尝试使用if (resUrl.indexOf(".js") !== -1)向前的代码。我无法理解为什么不会创建第二个网页实例,并且能够重新运行代码。

0 个答案:

没有答案