我想实现这样的一个解决方案:使用PhantomJS打开一个网站,对其进行评估,并将输出保存到文件中以供处理。特别是扫描恶意脚本。我已经成功地实现了使用运行一次的PhantomJS的解决方案。例如,下面的代码完美地工作...
var system = require('system');
var page = require('webpage').create();
var lastReceived = new Date().getTime();
var requestCount = 0;
var responseCount = 0;
var requestIds = [];
var fileSystem = require('fs');
var startTime = new Date().getTime();
page.onResourceReceived = function (response) {
if(requestIds.indexOf(response.id) !== -1) {
lastReceived = new Date().getTime();
responseCount++;
requestIds[requestIds.indexOf(response.id)] = null;
}
};
page.onResourceRequested = function (request) {
if(requestIds.indexOf(request.id) === -1) {
requestIds.push(request.id);
requestCount++;
}
};
page.open('http://adserver.example.com/adserve/;ID=164857;size=300x250;setID=162909;type=iframe', function () {});
var checkComplete = function () {
// We don't allow it to take longer than 5 seconds but
// don't return until all requests are finished
if((new Date().getTime() - lastReceived > 300 && requestCount === responseCount) || new Date().getTime() - startTime > 5000) {
clearInterval(checkCompleteInterval);
console.log(page.content);
phantom.exit();
}
}
var checkCompleteInterval = setInterval(checkComplete, 1);
然而,我尝试创建一个不需要反复重新启动PhantomJS的自动化系统极其困难,并且存在相当大的开销。 我尝试使用命名管道从中读取数据,然后尝试打开传递过来的url,但由于某些原因无法正确打开。非常希望并深表感激能够得到任何关于此事的指导。