gpt4 book ai didi

javascript - PhantomJS 将所​​有 onNavigationRequested 回调推送到数组

转载 作者:行者123 更新时间:2023-12-03 02:40:47 28 4
gpt4 key购买 nike

我有一个 phantom js 脚本,它检查每个重定向并通过 page.onNavigationRequested 回调方法将其显示在控制台中。

但是当我想捕获从 page.onNavigationRequested 回调方法返回的所有 URL 并将它们推送到数组并最终在脚本末尾显示所有 URL 时,它只显示第一个重定向 URL。

您能检查一下脚本和建议吗?

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));

提前谢谢您。

最佳答案

您的脚本有两个问题:

  1. 在第一个 url 打开后,您让 PhantomJS 过早退出。它没有时间遵循重定向。

  2. 您从上到下编写脚本就好像程序流是线性/同步的,而在 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 );
}
});

关于javascript - PhantomJS 将所​​有 onNavigationRequested 回调推送到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48332872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com