Всем привет! Уже около 2 месяцев борюсь с проблемой у себя в скрипе phantomjs, а именно скрипт зависает после некоторого времени работы. Всегда по-разному, может и не зависать, дело случая. Это небольшой скрипт (с комментариями), в котором происходит добавление пользователей в друзья. Также прежде скрипт авторизуется, используя логин и пароль и после этого начинает добавлять. На сайте используется HTTPS (может в этом может быть проблема?).
var page, doSearch, displayResults, authorize, addFriend,
addFriendLink = 'http://www.modelmayhem.com/classic_mystuff/friends/add_friend';
var fs = require('fs');
var text = fs.read('./general.txt');
var addFriendList = text.split('\n');
// create list of links
for(var i = 0; i < addFriendList.length; i++){
addFriendList[i] = addFriendLink + addFriendList[i];
}
// start phantomjs
var i=0;
page = require('webpage').create();
page.settings.resourceTimeout = 30000;
page.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
setTimeout(reload, 1000);
page.exit(1);
};
// if page loaded then running that function
addFriend = function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
try{
setTimeout(function(){
$('input[type="submit"]').trigger('click');
return true;
}, 10);
} catch(e) {console.log(e); location.reload();}
});
});
};
// if user redirect to auth page then running that function
authorize = function(){
console.log('Log in...');
page.evaluate(function() {
var email = '1231232@gmail.com';
var password = '321312';
$('input[name="email"]').val(email);
$('input[name="password"]').val(password);
$('input[type="checkbox"]').trigger('click');
$('input[value="Login"]').trigger('click');
return true;
});
};
// if some problems then load another page, current page push to the end of pagesList (var addFriendList)
function reload(){
addFriendList.push(addFriendList[i]);
i++;
if(i == addFriendList.length -1)
page.close();
page.openUrl(addFriendList[i], null, null);
}
page.onLoadFinished = function(status) {
clearTimeout(timer);
if (status === 'success') {
// check if auth page
if(page.url == 'https://secure.modelmayhem.com/login' || page.url == 'http://www.modelmayhem.com/login'){
console.log('logging');
authorize();
}
// check if loaded correct page
else if(page.url == addFriendList[i] || page.url == addFriendList[i-1] || page.url == addFriendList[i+1]){
console.log('adding friends');
addFriend();
}
// check if redirect to my profile page
else if(page.url == 'http://www.modelmayhem.com/mystuff_v2'){
console.log('opening link for friend adding...');
page.open(addFriendList[i]);
}
// when pagelist is end
else{
i++;
// this is not works
if(i == addFriendList.length )
process.exit();
page.exit();
}
console.log('Next one...');
page.open(addFriendList[i]);
}
}
// if problems with loading page, then run reload() in 1s
else {
setTimeout(reload, 1000);
}
};
page.onConsoleMessage = function(msg) {
console.log(msg);
};
// phantomjsEvent fires when timer is end
page.onResourceTimeout = function(request) {
setTimeout(reload, 1000);
};
// when pageLoading started event
page.onLoadStarted = function() {
// I run my own timer, becouse phantomjs's timer don't work
timer = setTimeout(function(){
console.log('time is off');
setTimeout(reload, 1000);
},27000);
};
// running script
page.open(addFriendList[i]);