Javascript PhantomJS将所有onNavigationRequested回调推送到数组
我有一个幻影js脚本,它检查每个重定向,并通过page.onNavigationRequested回调方法在控制台中显示它 但是,当我想捕获从page.onNavigationRequested回调方法返回的所有URL并将它们推送到数组中,最后在脚本末尾显示所有URL时,它只显示第一个重定向URL 你能检查一下剧本和建议吗Javascript PhantomJS将所有onNavigationRequested回调推送到数组,javascript,node.js,phantomjs,Javascript,Node.js,Phantomjs,我有一个幻影js脚本,它检查每个重定向,并通过page.onNavigationRequested回调方法在控制台中显示它 但是,当我想捕获从page.onNavigationRequested回调方法返回的所有URL并将它们推送到数组中,最后在脚本末尾显示所有URL时,它只显示第一个重定向URL 你能检查一下剧本和建议吗 var page = require('webpage').create(); var sys = require('system'); var fs = require('
var page = require('webpage').create();
var sys = require('system');
var fs = require('fs');
var response = {};
var arrayOfResponses = [];
var pageUrl = 'http://example.com/r1.php';
phantom.onError = function (msg, trace) {
phantom.exit(1);
};
function forceExit(){
phantom.exit(0);
}
page.onNavigationRequested = function(url, type, willNavigate, main) {
arrayOfResponses.push(url) ;
}
response.content = arrayOfResponses;
page.open(pageUrl, function(status) {
if ( status !== 'success' ) {
phantom.exit( 1 );
} else {
phantom.exit( 0 );
}
}, 100);
setTimeout(forceExit,2000);
console.log(JSON.stringify(response));
并提前感谢您。您的脚本有两个问题: 在打开第一个url后,会过早地退出PhantomJS。它没有时间跟踪重定向 您自上而下地编写脚本,就好像程序流是线性/同步的,而在javascript中它不是-onNavigationRequested可以被多次调用 因此,考虑到这一点,让我们重写脚本以收集所有重定向,并在2秒钟内没有新重定向时退出
var page = require('webpage').create();
var response = {};
var arrayOfResponses = [];
var pageUrl = 'http://admin.weeqo.com/redirect/r1.php';
var exitTimeout;
// This will be called if no redirects are requested in 2 seconds
function forceExit(){
// Just for fun we'll note the final URL
var curURL = page.evaluate(function(){
return document.location.href
});
console.log("Final URL is " + curURL);
// Prepare and output the report:
response.content = arrayOfResponses;
console.log("List of all requested URLs: " + JSON.stringify(response));
// Now we can exit safely
phantom.exit(0);
}
// This is called before each redirect
page.onNavigationRequested = function(url, type, willNavigate, main) {
// Clear timeout so that script is not shut down
// because we have a new redirect
if(exitTimeout) {
clearTimeout(exitTimeout);
}
arrayOfResponses.push(url);
console.log("Navigation requested: " + url);
// Create timeout that will shut down the script
// in two seconds unless cancelled
exitTimeout = setTimeout(forceExit, 2000);
}
// open the first page
page.open(pageUrl, function(status) {
// We only care for errors because
// who knows how many time will pass before
// we hit the last redirect
if ( status !== 'success' ) {
phantom.exit( 1 );
}
});