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