I am facing a challenge in extracting the source code from multiple webpages simultaneously. The links are stored in an array from a source text file. While I am able to successfully loop through the array and display the links, I encounter an issue when passing them through a function as they turn undefined after the initial iteration.
My main objective is to save the source code of each webpage into its own document. The first page is saved accurately, but the subsequent efforts result in undefined values. Despite hours of searching, I would greatly appreciate any guidance to lead me in the right direction.
var fs = require('fs');
var pageContent = fs.read('input.txt');
var arrdata = pageContent.split(/[\n]/);
var system = require('system');
var page = require('webpage').create();
var args = system.args;
var imagelink;
var content = " ";
function handle_page(file, imagelink){
page.open(file,function(){
var js = page.evaluate(function (){
return document;
});
fs.write(imagelink, page.content, 'w');
setTimeout(next_page(),500);
});
}
function next_page(imagelink){
var file = imagelink;
if(!file){phantom.exit(0);}
handle_page(file, imagelink);
}
for(var i in arrdata){
next_page(arrdata[i]);
}
Upon reflection, I now understand that the for loop runs only once, while the other two functions initiate their own loops, which explains the issue I am facing. I am still struggling to find a solution to make it work properly.