Commit 45719c88 45719c882fdd4e03b85b0190d28c1d22f926653e by Nicolas Perriault

Merge remote-tracking branch 'alfetopito/master'

2 parents fb5d67f9 f262278f
1 if (!phantom.casperLoaded) {
2 console.log('This script is intended to work with CasperJS, using its executable.');
3 phantom.exit(1);
4 }
5
6 if (phantom.casperArgs.args.length !== 1) {
7 console.log('You must provide the maximum number of pages to visit');
8 phantom.exit(1);
9 }
10
11 // If we don't set a limit, it could go on forever
12 var upTo = Number(phantom.casperArgs.args[0], 10);
13
14 var casper = new phantom.Casper({
15 verbose: true
16 });
17
18 // Fetch all <a> elements from the page and return
19 // the ones which contains a href starting with 'http://'
20 function searchLinks() {
21 var filter = Array.prototype.filter,
22 map = Array.prototype.map;
23 return map.call(filter.call(document.querySelectorAll('a'), function (a) {
24 return /^http:\/\/.*/i.test(a.getAttribute('href'));
25 }), function (a) {
26 return a.getAttribute('href');
27 });
28 }
29
30 // The base links array
31 var links = [
32 'http://google.com/',
33 'http://yahoo.com/',
34 'http://bing.com/'
35 ];
36
37 // Just opens the page and prints the title
38 var start = function (self, link) {
39 self.start(link, function (self) {
40 self.echo('Page title: ' + self.getTitle());
41 })
42 };
43
44 // Get the links, and add them to the links array
45 // (It could be done all in one step, but it is intentionally splitted)
46 var addLinks = function (self, link) {
47 self.then(function(self) {
48 var found = self.evaluate(searchLinks);
49 self.echo(found.length + " links found on " + link);
50 links = links.concat(found);
51 });
52 };
53
54 casper.start().then(function(self) {
55 self.echo('Starting');
56 });
57
58 var currentLink = 0;
59
60 // As long as it has a next link, and is under the maximum limit, will keep running
61 function check(self) {
62 if (links[currentLink] && currentLink < upTo) {
63 self.echo('--- Link ' + currentLink + ' ---');
64 start(self, links[currentLink]);
65 addLinks(self, links[currentLink]);
66 currentLink++;
67 self.run(check);
68 } else {
69 self.echo('All done.').exit();
70 }
71 }
72
73 casper.run(check);