Commit 5c3db051 5c3db051a4aa6270f66c742825cf02b729f3cf02 by Nicolas Perriault

minor tweaks added to samples/dynamic.js

1 parent 45719c88
...@@ -9,20 +9,20 @@ if (phantom.casperArgs.args.length !== 1) { ...@@ -9,20 +9,20 @@ if (phantom.casperArgs.args.length !== 1) {
9 } 9 }
10 10
11 // If we don't set a limit, it could go on forever 11 // If we don't set a limit, it could go on forever
12 var upTo = Number(phantom.casperArgs.args[0], 10); 12 var upTo = ~~phantom.casperArgs.args[0];
13 13
14 var casper = new phantom.Casper({ 14 var casper = new phantom.Casper({
15 verbose: true 15 verbose: true
16 }); 16 });
17 17
18 // Fetch all <a> elements from the page and return 18 // Fetch all <a> elements from the page and return
19 // the ones which contains a href starting with 'http://' 19 // the ones which contains a href starting with 'http://'
20 function searchLinks() { 20 function searchLinks() {
21 var filter = Array.prototype.filter, 21 var filter = Array.prototype.filter,
22 map = Array.prototype.map; 22 map = Array.prototype.map;
23 return map.call(filter.call(document.querySelectorAll('a'), function (a) { 23 return map.call(filter.call(document.querySelectorAll('a'), function(a) {
24 return /^http:\/\/.*/i.test(a.getAttribute('href')); 24 return (/^http:\/\/.*/i).test(a.getAttribute('href'));
25 }), function (a) { 25 }), function(a) {
26 return a.getAttribute('href'); 26 return a.getAttribute('href');
27 }); 27 });
28 } 28 }
...@@ -35,16 +35,16 @@ var links = [ ...@@ -35,16 +35,16 @@ var links = [
35 ]; 35 ];
36 36
37 // Just opens the page and prints the title 37 // Just opens the page and prints the title
38 var start = function (self, link) { 38 var start = function(self, link) {
39 self.start(link, function (self) { 39 self.start(link, function(self) {
40 self.echo('Page title: ' + self.getTitle()); 40 self.echo('Page title: ' + self.getTitle());
41 }) 41 });
42 }; 42 };
43 43
44 // Get the links, and add them to the links array 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) 45 // (It could be done all in one step, but it is intentionally splitted)
46 var addLinks = function (self, link) { 46 var addLinks = function(link) {
47 self.then(function(self) { 47 this.then(function(self) {
48 var found = self.evaluate(searchLinks); 48 var found = self.evaluate(searchLinks);
49 self.echo(found.length + " links found on " + link); 49 self.echo(found.length + " links found on " + link);
50 links = links.concat(found); 50 links = links.concat(found);
...@@ -62,7 +62,7 @@ function check(self) { ...@@ -62,7 +62,7 @@ function check(self) {
62 if (links[currentLink] && currentLink < upTo) { 62 if (links[currentLink] && currentLink < upTo) {
63 self.echo('--- Link ' + currentLink + ' ---'); 63 self.echo('--- Link ' + currentLink + ' ---');
64 start(self, links[currentLink]); 64 start(self, links[currentLink]);
65 addLinks(self, links[currentLink]); 65 addLinks.call(self, links[currentLink]);
66 currentLink++; 66 currentLink++;
67 self.run(check); 67 self.run(check);
68 } else { 68 } else {
......