googlelinks.js
1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var links = [];
var casper = new phantom.Casper();
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
casper.start('http://google.fr/', function(self) {
// search for 'casperjs' from google form
self.fill('form[name=f]', { q: 'casperjs' }, true);
});
casper.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);
});
casper.then(function(self) {
// aggregate results for the 'phantomjs' search
links = links.concat(self.evaluate(getLinks));
});
casper.run(function(self) {
// echo results in some pretty fashion
self.echo(links.length + ' links found:');
self.echo(' - ' + links.join('\n - ')).exit();
});