Commit 0245dcfb 0245dcfb93ad9e59523ff57068a027b0b617718d by Nicolas Perriault

better code organization for the 'dynamic' sample

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