Skip to content

Commit

Permalink
moved var declaration to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
brikou committed May 21, 2012
1 parent b1ed9a7 commit f44d37b
Show file tree
Hide file tree
Showing 36 changed files with 373 additions and 284 deletions.
12 changes: 6 additions & 6 deletions samples/bbcshots.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nbLinks = 0
currentLink = 1
images = []

# helper to hide some element from remote DOM
### helper to hide some element from remote DOM ###
casper.hide = (selector) ->
@evaluate (selector) ->
document.querySelector(selector).style.display = "none"
Expand All @@ -17,7 +17,7 @@ casper.start "http://www.bbc.co.uk/", ->
nbLinks = @evaluate ->
return __utils__.findAll('#promo2_carousel_items_items li').length
@echo "#{nbLinks} items founds"
# hide navigation arrows
### hide navigation arrows ###
@hide ".nav_left"
@hide ".nav_right"
@mouse.move "#promo2_carousel"
Expand All @@ -28,10 +28,10 @@ casper.start "http://www.bbc.co.uk/", ->
@echo "Clicked on pause button"
@waitUntilVisible ".autoplay.nav_play", ->
@echo "Carousel has been paused"
# hide play button
### hide play button ###
@hide ".autoplay"

# Capture carrousel area
### Capture carrousel area ###
next = ->
image = "bbcshot#{currentLink}.png"
images.push image
Expand All @@ -45,13 +45,13 @@ next = ->
else
@then buildPage

# Building resulting page and image
### Building resulting page and image ###
buildPage = ->
@echo "Build result page"
fs = require "fs"
@viewport 624, 400
pageHtml = "<html><body style='background:black;margin:0;padding:0'>"
for image in images
images.forEach (image) ->
pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>"
pageHtml += "</body></html>"
fs.write "result.html", pageHtml, 'w'
Expand Down
29 changes: 18 additions & 11 deletions samples/bbcshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
Create a mosaic image from all headline photos on BBC homepage
*/

var casper = require("casper").create();
var nbLinks = 0;
var currentLink = 1;
var images = [];
var buildPage, casper, currentLink, images, nbLinks, next;

casper = require("casper").create();
nbLinks = 0;
currentLink = 1;
images = [];

/* helper to hide some element from remote DOM */
casper.hide = function(selector) {
this.evaluate(function(selector) {
document.querySelector(selector).style.display = "none";
Expand All @@ -20,7 +23,7 @@ casper.start("http://www.bbc.co.uk/", function() {
return __utils__.findAll('#promo2_carousel_items_items li').length;
});
this.echo(nbLinks + " items founds");
// hide navigation arrows
/* hide navigation arrows */
this.hide(".nav_left");
this.hide(".nav_right");
this.mouse.move("#promo2_carousel");
Expand All @@ -31,14 +34,16 @@ casper.start("http://www.bbc.co.uk/", function() {
this.echo("Clicked on pause button");
this.waitUntilVisible(".autoplay.nav_play", function() {
this.echo("Carousel has been paused");
// hide play button
/* hide play button */
this.hide(".autoplay");
});
});
});

var next = function next() {
var image = "bbcshot" + currentLink + ".png";
/* Capture carrousel area */
next = function() {
var image;
image = "bbcshot" + currentLink + ".png";
images.push(image);
this.echo("Processing image " + currentLink);
this.captureSelector(image, '.carousel_viewport');
Expand All @@ -53,11 +58,13 @@ var next = function next() {
}
};

var buildPage = function buildPage() {
/* Building resulting page and image */
buildPage = function() {
var fs, pageHtml;
this.echo("Build result page");
var fs = require("fs");
fs = require("fs");
this.viewport(624, 400);
var pageHtml = "<html><body style='background:black;margin:0;padding:0'>";
pageHtml = "<html><body style='background:black;margin:0;padding:0'>";
images.forEach(function(image) {
pageHtml += "<img src='file://" + fs.workingDirectory + "/" + image + "'><br>";
});
Expand Down
2 changes: 1 addition & 1 deletion samples/cliplay.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
casper = require("casper").create()
dump = require("utils").dump

# removing default options passed by the Python executable
### removing default options passed by the Python executable ###
casper.cli.drop "cli"
casper.cli.drop "casper-path"

Expand Down
8 changes: 5 additions & 3 deletions samples/cliplay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var casper = require("casper").create();
var dump = require("utils").dump;
var casper, dump;

// removing default options passed by the Python executable
casper = require("casper").create();
dump = require("utils").dump;

/* removing default options passed by the Python executable */
casper.cli.drop("cli");
casper.cli.drop("casper-path");

Expand Down
8 changes: 4 additions & 4 deletions samples/customevents.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
casper = require("casper").create()

# listening to a custom event
casper.on 'google.loaded', (title) ->
### listening to a custom event ###
casper.on "google.loaded", (title) ->
casper.echo "Google page title is #{title}"

casper.start "http://google.com/", ->
# emitting a custom event
@emit 'google.loaded', @getTitle()
### emitting a custom event ###
@emit "google.loaded", @getTitle()

casper.run()
12 changes: 7 additions & 5 deletions samples/customevents.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var casper = require("casper").create();
var casper;

// listening to a custom event
casper.on('google.loaded', function(title) {
casper = require("casper").create();

/* listening to a custom event */
casper.on("google.loaded", function(title) {
casper.echo("Google page title is " + title);
});

casper.start("http://google.com/", function() {
// emitting a custom event
this.emit('google.loaded', this.getTitle());
/* emitting a custom event */
this.emit("google.loaded", this.getTitle());
});

casper.run();
31 changes: 19 additions & 12 deletions samples/customlogging.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
###
A basic custom logging implementation. The idea is to (extremely) verbosely
log every received resource.
A basic custom logging implementation. The idea is to (extremely) verbosely log
every received resource.
###

casper = require("casper").create
# Every time a resource is received, a new log entry is added to the stack
# at the 'verbose' level.
###
Every time a resource is received, a new log entry is added to the stack at
the 'verbose' level.
###
onResourceReceived: (self, resource) ->
infos = []
props = ["url", "status", "statusText", "redirectURL", "bodySize"]
props = [
"url"
"status"
"statusText"
"redirectURL"
"bodySize"
]
infos.push resource[prop] for prop in props
infos.push "[#{h.name}: #{h.value}]" for h in resource.headers
@log infos.join(', '), 'verbose'
infos.push "[#{header.name}: #{header.value}]" for header in resource.headers
@log infos.join(", "), "verbose"
verbose: true # we want to see the log printed out to the console
logLevel: 'verbose' # of course we want to see logs to our new level :)
logLevel: "verbose" # of course we want to see logs to our new level :)

### add a new 'verbose' logging level at the lowest priority ###
casper.logLevels = ["verbose"].concat casper.logLevels

# add a new 'verbose' logging level at the lowest priority
casper.logLevels = ['verbose'].concat casper.logLevels

# test our new logger with google
### test our new logger with google ###
casper.start "http://www.google.com/"
casper.run()
62 changes: 35 additions & 27 deletions samples/customlogging.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
/*
A basic custom logging implementation. The idea is to (extremely) verbosely
log every received resource.
A basic custom logging implementation. The idea is to (extremely) verbosely log
every received resource.
*/

var casper = require("casper").create({
/**
* Every time a resource is received, a new log entry is added to the stack
* at the 'verbose' level.
*
* @param Object resource A phantomjs resource object
*/
var casper;

casper = require("casper").create({
/*
Every time a resource is received, a new log entry is added to the stack at
the 'verbose' level.
*/
onResourceReceived: function(self, resource) {
var infos = [
resource.url,
resource.status,
resource.statusText,
resource.redirectURL,
resource.bodySize
var header, infos, prop, props, _i, _j, _len, _len1, _ref;
infos = [];
props = [
"url",
"status",
"statusText",
"redirectURL",
"bodySize"
];
resource.headers.forEach(function(header) {
infos.push('[' + [header.name, header.value].join(', ') + ']');
});
self.log(infos.join(', '), 'verbose');
for (_i = 0, _len = props.length; _i < _len; _i++) {
prop = props[_i];
infos.push(resource[prop]);
}
_ref = resource.headers;
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
header = _ref[_j];
infos.push("[" + header.name + ": " + header.value + "]");
}
this.log(infos.join(", "), "verbose");
},
verbose: true, // we want to see the log printed out to the console
logLevel: 'verbose' // of course we want to see logs to our new level :)
verbose: true,
logLevel: "verbose"
});

// add a new 'verbose' logging level at the lowest priority
casper.logLevels = ['verbose'].concat(casper.logLevels);
/* add a new 'verbose' logging level at the lowest priority */
casper.logLevels = ["verbose"].concat(casper.logLevels);

// test our new logger with google
casper.start("http://www.google.com/").run(function(self) {
self.exit();
});
/* test our new logger with google */
casper.start("http://www.google.com/");

casper.run();
4 changes: 3 additions & 1 deletion samples/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Download the google logo image as base64
*/

var casper = require("casper").create({
var casper;

casper = require("casper").create({
verbose: true
});

Expand Down
34 changes: 20 additions & 14 deletions samples/dynamic.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
casper = require("casper").create verbose: true
casper = require("casper").create
verbose: true

# If we don't set a limit, it could go on forever
upTo = ~~casper.cli.get(0) || 10 # max 10 links
### 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://'
###
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
Expand All @@ -13,20 +16,22 @@ searchLinks = ->
), (a) ->
a.getAttribute "href"

# The base links array
### The base links array ###
links = [
'http://google.com/'
'http://yahoo.com/'
'http://bing.com/'
"http://google.com/"
"http://yahoo.com/"
"http://bing.com/"
]

# Just opens the page and prints the title
### Just opens the page and prints the title ###
start = (link) ->
@start link, ->
@echo "Page title: #{ @getTitle() }"

# Get the links, and add them to the links array
# (It could be done all in one step, but it is intentionally splitted)
###
Get the links, and add them to the links array
(It could be done all in one step, but it is intentionally splitted)
###
addLinks = (link) ->
@then ->
found = @evaluate searchLinks
Expand All @@ -35,11 +40,12 @@ addLinks = (link) ->

casper.start()

casper.then -> @echo "Starting"
casper.then ->
@echo "Starting"

currentLink = 0;

# As long as it has a next link, and is under the maximum limit, will keep running
### As long as it has a next link, and is under the maximum limit, will keep running ###
check = ->
if links[currentLink] && currentLink < upTo
@echo "--- Link #{currentLink} ---"
Expand Down
Loading

0 comments on commit f44d37b

Please sign in to comment.