Commit 96fc2436 96fc243672d7b0b912cd7a88464f21b16bdc3c1d by Nicolas Perriault

added samples repository

1 parent bebd54d8
phantom.injectJs('casper.js');
var logo;
new phantom.Casper({
verbose: true
}).start('http://www.google.fr/', function(self) {
// download the google logo image as base64
logo = self.base64encode('http://www.google.fr/images/srpr/logo3w.png', 'google_logo.png');
}).run(function(self) {
self.echo(logo).exit();
});
phantom.injectJs("casper.js");
var articles = [];
/**
* Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
*/
phantom.Casper.extend({
/**
* Adds a new navigation step for casper; basically it will:
*
* 1. open an url,
* 2. on loaded, will fetch all contents retrieved through the provided
* CSS3 selector and return them in a formatted object.
*/
fetchTexts: function(location, selector) {
return this.thenOpen(location, function(self) {
var texts = self.evaluate(function() {
var elements = document.querySelectorAll('%selector%');
return Array.prototype.map.call(elements, function(e) {
return e.innerText;
});
}, {
selector: selector.replace("'", "\'")
});
articles = articles.concat(texts);
});
},
/**
* Echoes a JSON output of the fetched results and exits phantomjs.
*/
renderJSON: function(what) {
return this.echo(JSON.stringify(what, null, ' ')).exit();
}
});
var casper = new phantom.Casper({
loadImages: false,
loadPlugins: false,
logLevel: "debug",
verbose: true,
});
casper.start()
.fetchTexts('http://www.liberation.fr/', 'h3') // all article titles are stored in <h3>
.fetchTexts('http://www.lemonde.fr/', 'h2.article') // all article titles are stored in <h2 class="article">
.run(function(self) {
self.renderJSON(articles);
})
;
phantom.injectJs('casper.js');
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return {
title: e.innerText,
href: e.getAttribute('href')
};
});
}
var links = [];
var casper = new phantom.Casper({
logLevel: "debug",
loadImages: false,
loadPlugins: false,
verbose: true
})
.start('http://google.fr/')
.then(function(self) {
// search for 'casperjs' from google form
self.fill('form[name=f]', {
q: 'casperjs'
}, true);
})
.then(function(self) {
// aggregate results for the 'casperjs' search
links = self.evaluate(getLinks);
// now search for 'phantomjs' by fillin the form again
self.fill('form[name=f]', {
q: 'phantomjs'
}, true);
})
.then(function(self) {
// aggregate results for the 'phantomjs' search
links = links.concat(self.evaluate(getLinks));
})
.run(function(self) {
// echo results in some pretty fashion
self.echo(links.map(function(i) {
return i.title + ' (' + i.href + ')';
}).join('\n')).exit();
})
;