Commit ab96d89c ab96d89cfeddb9a251ee3b978fce3a85694e2778 by Brikou CARRE

[samples] js code revision (based on coffee version) + some fixes

1 parent ae6f9edf
### Create a mosaic image from all headline photos on BBC homepage
###
Create a mosaic image from all headline photos on BBC homepage
###
casper = require('casper').create()
nbLinks = 0
currentLink = 1
images = []
# helper to hide some element from remote DOM
casper.hide = (selector) ->
@evaluate (selector) ->
document.querySelector(selector).style.display = "none"
......@@ -28,20 +31,6 @@ casper.start 'http://www.bbc.co.uk/', ->
# hide play button
@hide '.autoplay'
# 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
pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>"
pageHtml += "</body></html>"
fs.write 'result.html', pageHtml, 'w'
@thenOpen "file://#{fs.workingDirectory}/result.html", ->
@echo 'Resulting image saved to result.png'
@capture 'result.png'
# Capture carrousel area
next = ->
image = "bbcshot#{currentLink}.png"
......@@ -56,6 +45,20 @@ next = ->
else
@then buildPage
# 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
pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>"
pageHtml += "</body></html>"
fs.write 'result.html', pageHtml, 'w'
@thenOpen "file://#{fs.workingDirectory}/result.html", ->
@echo 'Resulting image saved to result.png'
@capture 'result.png'
casper.then next
casper.run()
......
/**
* Create a mosaic image from all headline photos on BBC homepage
*
*/
var casper = require('casper').create(),
nbLinks = 0,
currentLink = 1,
images = [];
/*
Create a mosaic image from all headline photos on BBC homepage
*/
var casper = require('casper').create();
var nbLinks = 0;
var currentLink = 1;
var images = [];
// helper to hide some element from remote DOM
casper.hide = function(selector) {
this.evaluate(function(selector) {
document.querySelector(selector).style.display = "none";
}, { selector: selector });
}, {
selector: selector
});
};
casper.start('http://www.bbc.co.uk/', function() {
nbLinks = this.evaluate(function() {
return __utils__.findAll('#promo2_carousel_items_items li').length;
});
this.echo(nbLinks + ' items founds');
this.echo("" + nbLinks + " items founds");
// hide navigation arrows
this.hide('.nav_left');
this.hide('.nav_right');
......@@ -36,14 +37,13 @@ casper.start('http://www.bbc.co.uk/', function() {
});
});
// Capture carrousel area
var next = function() {
var image = 'bbcshot' + currentLink + '.png';
var image = "bbcshot" + currentLink + ".png";
images.push(image);
this.echo('Processing image ' + currentLink);
this.echo("Processing image " + currentLink);
this.captureSelector(image, '.carousel_viewport');
if (currentLink < nbLinks) {
this.click('.carousel_itemList_li[rel="' + currentLink + '"]');
this.click(".carousel_itemList_li[rel='" + currentLink + "']");
this.wait(1000, function() {
this.then(next);
currentLink++;
......@@ -53,18 +53,17 @@ var next = function() {
}
};
// Building resulting page and image
var buildPage = function() {
this.echo('Build result page');
var fs = require('fs');
this.viewport(624, 400);
var pageHtml = "<html><body style='background:black;margin:0;padding:0'>";
images.forEach(function(image) {
pageHtml += '<img src="file://' + fs.workingDirectory + '/' + image + '"><br>';
pageHtml += "<img src='file://" + fs.workingDirectory + "/" + image + "'><br>";
});
pageHtml += "</body></html>";
fs.write('result.html', pageHtml, 'w');
this.thenOpen('file://' + fs.workingDirectory + '/result.html', function() {
this.thenOpen("file://" + fs.workingDirectory + "/result.html", function() {
this.echo('Resulting image saved to result.png');
this.capture('result.png');
});
......
......@@ -8,10 +8,10 @@ upTo = ~~casper.cli.get(0) || 10 # max 10 links
searchLinks = ->
filter = Array::filter
map = Array::map
links = document.querySelectorAll 'a'
absolutes = filter.call links, (a) ->
/^http:\/\/.*/i.test a.getAttribute 'href'
return map.call absolutes, (a) -> a.getAttribute 'href'
map.call filter.call(document.querySelectorAll("a"), (a) ->
(/^http:\/\/.*/i).test a.getAttribute("href")
), (a) ->
a.getAttribute "href"
# The base links array
links = [
......@@ -21,7 +21,9 @@ links = [
]
# Just opens the page and prints the title
start = (link) -> @start link, -> @echo "Page title: #{ @getTitle() }"
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)
......
if (phantom.casperArgs.args.length !== 1) {
console.log('You must provide the maximum number of pages to visit');
phantom.exit(1);
}
var casper = require('casper').create({
verbose: true
});
......@@ -12,15 +7,15 @@ var upTo = ~~casper.cli.get(0) || 10; // max 10 links
// Fetch all <a> elements from the page and return
// the ones which contains a href starting with 'http://'
function searchLinks() {
var 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'));
var searchLinks = function() {
var filter = Array.prototype.filter;
var 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');
return a.getAttribute("href");
});
}
};
// The base links array
var links = [
......@@ -30,39 +25,42 @@ var links = [
];
// Just opens the page and prints the title
var start = function(self, link) {
self.start(link, function(self) {
self.echo('Page title: ' + self.getTitle());
var start = function(link) {
this.start(link, function() {
this.echo("Page title: " + (this.getTitle()));
});
};
// 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) {
this.then(function(self) {
var found = self.evaluate(searchLinks);
self.echo(found.length + " links found on " + link);
this.then(function() {
var found = this.evaluate(searchLinks);
this.echo("" + found.length + " links found on " + link);
links = links.concat(found);
});
};
casper.start().then(function(self) {
self.echo('Starting');
casper.start();
casper.then(function() {
this.echo('Starting');
});
var currentLink = 0;
// As long as it has a next link, and is under the maximum limit, will keep running
function check(self) {
var check = function() {
if (links[currentLink] && currentLink < upTo) {
self.echo('--- Link ' + currentLink + ' ---');
start(self, links[currentLink]);
addLinks.call(self, links[currentLink]);
this.echo("--- Link " + currentLink + " ---");
start.call(this, links[currentLink]);
addLinks.call(this, links[currentLink]);
currentLink++;
self.run(check);
this.run(check);
} else {
self.echo('All done.').exit();
this.echo('All done.');
this.exit();
}
}
};
casper.run(check);
......
......@@ -19,9 +19,9 @@ casper.fetchScore = ->
terms = casper.cli.args # terms are passed through command-line arguments
if terms.length < 3
if terms.length < 2
casper
.echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...")
.echo("Usage: $ casperjs googlematch.js term1 term2 [term3]...")
.exit(1)
scores = []
......@@ -38,7 +38,7 @@ casper.each terms, (self, term) ->
self.echo "#{term}: #{score}"
casper.run ->
scores.sort -> (a, b) -> b.score - a.score;
scores.sort (a, b) -> b.score - a.score;
winner = scores[0]
@echo "Winner is #{winner.term} with #{winner.score} results"
@echo "Winner is \"#{winner.term}\" with #{winner.score} results"
@exit()
......
......@@ -23,9 +23,9 @@ casper.fetchScore = function() {
var terms = casper.cli.args;
if (terms.length < 3) {
if (terms.length < 2) {
casper
.echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...")
.echo("Usage: $ casperjs googlematch.js term1 term2 [term3]...")
.exit(1)
;
}
......@@ -36,24 +36,28 @@ casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!");
casper.start("http://google.fr/");
casper.each(terms, function(self, term, i) {
self.then(function(self) {
self.fill('form[action="/search"]', { q: term }, true);
}).then(function(self) {
var score = self.fetchScore();
casper.each(terms, function(self, term) {
this.then(function() {
this.fill('form[action="/search"]', {
q: term
}, true);
});
this.then(function() {
var score;
score = this.fetchScore();
scores.push({
term: term,
score: score
});
self.echo(term + ': ' + score);
self.echo("" + term + ": " + score);
});
});
casper.run(function(self) {
casper.run(function() {
scores.sort(function(a, b) {
return b.score - a.score;
});
var winner = scores[0];
self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results');
self.exit();
this.echo("Winner is \"" + winner.term + "\" with " + winner.score + " results");
this.exit();
});
......
###
Capture multiple pages of google search results
usage: casperjs googlepagination.coffee my search terms
Usage: $ casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
###
......@@ -11,7 +11,7 @@ currentPage = 1
if casper.cli.args.length is 0
casper
.echo("Usage: $ casperjs my search terms")
.echo("Usage: $ casperjs googlepagination.coffee my search terms")
.exit(1)
processPage = ->
......
/*
Capture multiple pages of google search results
Usage: casperjs googlepagination.coffee my search terms
Usage: $ casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
*/
......@@ -12,7 +12,7 @@ var currentPage = 1;
if (casper.cli.args.length === 0) {
casper
.echo("Usage: $ casperjs my search terms")
.echo("Usage: $ casperjs googlepagination.coffee my search terms")
.exit(1)
;
}
......
casper = require('casper').create verbose: true
countLinks = -> document.querySelectorAll('a').length
countLinks = ->
document.querySelectorAll('a').length
suites = [
->
......@@ -19,7 +20,8 @@ suites = [
casper.start()
casper.then -> @echo('Starting')
casper.then ->
@echo('Starting')
currentSuite = 0;
......@@ -29,7 +31,7 @@ check = ->
currentSuite++;
casper.run check
else
@echo 'All done.'
@echo "All done."
@exit()
casper.run check
......
......@@ -2,51 +2,55 @@ var casper = require('casper').create({
verbose: true
});
function countLinks() {
var countLinks = function() {
return document.querySelectorAll('a').length;
}
};
var suites = [
function(self) {
self.echo('Suite 1');
self.start('http://google.com/', function(self) {
self.echo('Page title: ' + self.getTitle());
}).then(function(self) {
self.echo(self.evaluate(countLinks) + ' links');
function() {
this.echo('Suite 1');
this.start('http://google.com/', function() {
this.echo("Page title: " + (this.getTitle()));
});
},
function(self) {
self.echo('Suite 2');
self.start('http://yahoo.com/', function(self) {
self.echo('Page title: ' + self.getTitle());
}).then(function(self) {
self.echo(self.evaluate(countLinks) + ' links');
this.then(function() {
this.echo("" + (this.evaluate(countLinks)) + " links");
});
},
function(self) {
self.echo('Suite 3');
self.start('http://bing.com/', function(self) {
self.echo('Page title: ' + self.getTitle());
}).then(function(self) {
self.echo(self.evaluate(countLinks) + ' links');
}, function() {
this.echo('Suite 2');
this.start('http://yahoo.com/', function() {
this.echo("Page title: " + (this.getTitle()));
});
this.then(function() {
this.echo("" + (this.evaluate(countLinks)) + " links");
});
}, function() {
this.echo('Suite 3');
this.start('http://bing.com/', function() {
this.echo("Page title: " + (this.getTitle()));
});
this.then(function() {
this.echo("" + (this.evaluate(countLinks)) + " links");
});
}
];
casper.start().then(function(self) {
self.echo('Starting');
casper.start();
casper.then(function() {
this.echo('Starting');
});
var currentSuite = 0;
function check(self) {
var check = function() {
if (suites[currentSuite]) {
suites[currentSuite](casper);
suites[currentSuite].call(this);
currentSuite++;
casper.run(check);
} else {
self.echo('All done.').exit();
this.echo("All done.");
this.exit();
}
}
};
casper.run(check);
......