90 lines
2.5 KiB
JavaScript
90 lines
2.5 KiB
JavaScript
/*jslint node:true,browser:true*/
|
|
var fs = require('fs');
|
|
var cookieFile = 'cookies.json';
|
|
var credentialFile = 'credential.json';
|
|
|
|
|
|
var casper = require('casper').create({
|
|
pageSettings: {
|
|
loadImages: false,//The script is much faster when this field is set to false
|
|
loadPlugins: false,
|
|
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
|
|
}
|
|
});
|
|
|
|
|
|
function loadCredentials() {
|
|
var credentials;
|
|
var txt;
|
|
console.log("loadCredential");
|
|
if (fs.exists(credentialFile)) {
|
|
txt = fs.read(credentialFile);
|
|
credentials = JSON.parse(txt);
|
|
}
|
|
console.log("/loadCredential");
|
|
return credentials;
|
|
}
|
|
|
|
function readCookies(page) {
|
|
console.log("readCookies");
|
|
var cookies = JSON.parse(fs.read(cookieFile));
|
|
page.cookies = cookies;
|
|
console.log(cookies);
|
|
}
|
|
|
|
function saveCookies(page) {
|
|
console.log("saveCookies");
|
|
var cookies = JSON.stringify(page.cookies);
|
|
console.log(cookies);
|
|
fs.write(cookieFile, cookies, 'w');
|
|
}
|
|
|
|
//First step is to open Facebook
|
|
casper.start().thenOpen("https://facebook.com", function() {
|
|
readCookies(this.page);
|
|
console.log("Facebook website opened");
|
|
saveCookies(this.page);
|
|
});
|
|
|
|
|
|
//Now we have to populate username and password, and submit the form
|
|
casper.then(function(){
|
|
var credentials;
|
|
credentials = loadCredentials();
|
|
readCookies(this.page);
|
|
console.log("Login using username " + credentials.email + " and password");
|
|
|
|
this.evaluate(function(credentials){
|
|
document.getElementById("email").value = credentials.email;
|
|
document.getElementById("pass").value = credentials.pass;
|
|
document.getElementById("loginbutton").children[0].click();
|
|
}, credentials);
|
|
saveCookies(this.page);
|
|
});
|
|
|
|
//Wait to be redirected to the Home page, and then make a screenshot
|
|
casper.then(function(){
|
|
console.log("Make a screenshot and save it as AfterLogin.png");
|
|
this.wait(6000);//Wait a bit so page loads (there are a lot of ajax calls and that is why we are waiting 6 seconds)
|
|
this.capture('AfterLogin.png');
|
|
});
|
|
|
|
/*
|
|
//Get all images greater than 100x100 pixels
|
|
casper.then(function(){
|
|
var images = this.evaluate(function(){
|
|
var facebookImages = document.getElementsByTagName('img');
|
|
var allSrc = [];
|
|
var i;
|
|
for(i = 0; i < facebookImages.length; i++) {
|
|
if(facebookImages[i].height >= 100 && facebookImages[i].width >= 100){
|
|
allSrc.push(facebookImages[i].src);
|
|
}
|
|
}
|
|
return JSON.stringify(allSrc);
|
|
});
|
|
console.log(images);
|
|
});
|
|
*/
|
|
|
|
casper.run();
|