Permalink
Browse files

better code organization for the 'dynamic' sample

  • Loading branch information...
1 parent 7b38c3a commit 0245dcfb93ad9e59523ff57068a027b0b617718d @n1k0 n1k0 committed Jun 10, 2012
Showing with 53 additions and 57 deletions.
  1. +24 −24 samples/dynamic.coffee
  2. +29 −33 samples/dynamic.js
@@ -1,32 +1,17 @@
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/"
"http://yahoo.com/"
"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
View
@@ -2,55 +2,47 @@ 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/",
"http://yahoo.com/",
"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);

0 comments on commit 0245dcf

Please sign in to comment.