Commit 0245dcfb 0245dcfb93ad9e59523ff57068a027b0b617718d by Nicolas Perriault

better code organization for the 'dynamic' sample

1 parent 7b38c3a6
casper = require("casper").create
verbose: true
# If we don't set a limit, it could go on forever
upTo = ~~casper.cli.get(0) || 10
###
Fetch all <a> elements from the page and return
the ones which contains a href starting with 'http://'
###
searchLinks = ->
filter = Array::filter
map = Array::map
map.call filter.call(document.querySelectorAll("a"), (a) ->
(/^http:\/\/.*/i).test a.getAttribute("href")
), (a) ->
a.getAttribute "href"
# The base links array
links = [
"http://google.com/"
......@@ -23,10 +8,10 @@ links = [
"http://bing.com/"
]
# Just opens the page and prints the title
start = (link) ->
@start link, ->
@echo "Page title: #{ @getTitle() }"
currentLink = 0;
# If we don't set a limit, it could go on forever
upTo = ~~casper.cli.get(0) || 10
###
Get the links, and add them to the links array
......@@ -38,12 +23,22 @@ addLinks = (link) ->
@echo "#{found.length} links found on #{link}"
links = links.concat found
casper.start()
casper.then ->
@echo "Starting"
###
Fetch all <a> elements from the page and return
the ones which contains a href starting with 'http://'
###
searchLinks = ->
filter = Array::filter
map = Array::map
map.call filter.call(document.querySelectorAll("a"), (a) ->
(/^http:\/\/.*/i).test a.getAttribute("href")
), (a) ->
a.getAttribute "href"
currentLink = 0;
# Just opens the page and prints the title
start = (link) ->
@start link, ->
@echo "Page title: #{ @getTitle() }"
# As long as it has a next link, and is under the maximum limit, will keep running
check = ->
......@@ -57,4 +52,9 @@ check = ->
@echo "All done."
@exit()
casper.start()
casper.then ->
@echo "Starting"
casper.run check
......
......@@ -2,24 +2,6 @@ var casper = require("casper").create({
verbose: true
});
// If we don't set a limit, it could go on forever
var upTo = ~~casper.cli.get(0) || 10;
/*
Fetch all <a> elements from the page and return
the ones which contains a href starting with 'http://'
*/
var searchLinks = function() {
var filter, map;
filter = Array.prototype.filter;
map = Array.prototype.map;
return map.call(filter.call(document.querySelectorAll("a"), function(a) {
return /^http:\/\/.*/i.test(a.getAttribute("href"));
}), function(a) {
return a.getAttribute("href");
});
};
// The base links array
var links = [
"http://google.com/",
......@@ -27,30 +9,40 @@ var links = [
"http://bing.com/"
];
// Just opens the page and prints the title
var start = function(link) {
this.start(link, function() {
this.echo('Page title: ' + this.getTitle());
});
};
// If we don't set a limit, it could go on forever
var upTo = ~~casper.cli.get(0) || 10;
var currentLink = 0;
// Get the links, and add them to the links array
// (It could be done all in one step, but it is intentionally splitted)
var addLinks = function(link) {
function addLinks(link) {
this.then(function() {
var found = this.evaluate(searchLinks);
this.echo(found.length + " links found on " + link);
links = links.concat(found);
});
};
}
casper.start();
casper.then(function() {
this.echo("Starting");
});
// Fetch all <a> elements from the page and return
// the ones which contains a href starting with 'http://'
function searchLinks() {
var filter, map;
filter = Array.prototype.filter;
map = Array.prototype.map;
return map.call(filter.call(document.querySelectorAll("a"), function(a) {
return (/^http:\/\/.*/i).test(a.getAttribute("href"));
}), function(a) {
return a.getAttribute("href");
});
}
var currentLink = 0;
// Just opens the page and prints the title
function start(link) {
this.start(link, function() {
this.echo('Page title: ' + this.getTitle());
});
}
// As long as it has a next link, and is under the maximum limit, will keep running
function check() {
......@@ -64,6 +56,10 @@ function check() {
this.echo("All done.");
this.exit();
}
};
}
casper.start().then(function() {
this.echo("Starting");
});
casper.run(check);
......