Commit ab96d89c ab96d89cfeddb9a251ee3b978fce3a85694e2778 by Brikou CARRE

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

1 parent ae6f9edf
1 ### Create a mosaic image from all headline photos on BBC homepage
2 ### 1 ###
2 Create a mosaic image from all headline photos on BBC homepage
3 ###
4
3 casper = require('casper').create() 5 casper = require('casper').create()
4 nbLinks = 0 6 nbLinks = 0
5 currentLink = 1 7 currentLink = 1
6 images = [] 8 images = []
7 9
10 # helper to hide some element from remote DOM
8 casper.hide = (selector) -> 11 casper.hide = (selector) ->
9 @evaluate (selector) -> 12 @evaluate (selector) ->
10 document.querySelector(selector).style.display = "none" 13 document.querySelector(selector).style.display = "none"
...@@ -28,20 +31,6 @@ casper.start 'http://www.bbc.co.uk/', -> ...@@ -28,20 +31,6 @@ casper.start 'http://www.bbc.co.uk/', ->
28 # hide play button 31 # hide play button
29 @hide '.autoplay' 32 @hide '.autoplay'
30 33
31 # Building resulting page and image
32 buildPage = ->
33 @echo 'Build result page'
34 fs = require 'fs'
35 @viewport 624, 400
36 pageHtml = "<html><body style='background:black;margin:0;padding:0'>"
37 for image in images
38 pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>"
39 pageHtml += "</body></html>"
40 fs.write 'result.html', pageHtml, 'w'
41 @thenOpen "file://#{fs.workingDirectory}/result.html", ->
42 @echo 'Resulting image saved to result.png'
43 @capture 'result.png'
44
45 # Capture carrousel area 34 # Capture carrousel area
46 next = -> 35 next = ->
47 image = "bbcshot#{currentLink}.png" 36 image = "bbcshot#{currentLink}.png"
...@@ -56,6 +45,20 @@ next = -> ...@@ -56,6 +45,20 @@ next = ->
56 else 45 else
57 @then buildPage 46 @then buildPage
58 47
48 # Building resulting page and image
49 buildPage = ->
50 @echo 'Build result page'
51 fs = require 'fs'
52 @viewport 624, 400
53 pageHtml = "<html><body style='background:black;margin:0;padding:0'>"
54 for image in images
55 pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>"
56 pageHtml += "</body></html>"
57 fs.write 'result.html', pageHtml, 'w'
58 @thenOpen "file://#{fs.workingDirectory}/result.html", ->
59 @echo 'Resulting image saved to result.png'
60 @capture 'result.png'
61
59 casper.then next 62 casper.then next
60 63
61 casper.run() 64 casper.run()
......
1 /** 1 /*
2 * Create a mosaic image from all headline photos on BBC homepage 2 Create a mosaic image from all headline photos on BBC homepage
3 * 3 */
4 */ 4
5 var casper = require('casper').create(), 5 var casper = require('casper').create();
6 nbLinks = 0, 6 var nbLinks = 0;
7 currentLink = 1, 7 var currentLink = 1;
8 images = []; 8 var images = [];
9 9
10 // helper to hide some element from remote DOM
11 casper.hide = function(selector) { 10 casper.hide = function(selector) {
12 this.evaluate(function(selector) { 11 this.evaluate(function(selector) {
13 document.querySelector(selector).style.display = "none"; 12 document.querySelector(selector).style.display = "none";
14 }, { selector: selector }); 13 }, {
14 selector: selector
15 });
15 }; 16 };
16 17
17 casper.start('http://www.bbc.co.uk/', function() { 18 casper.start('http://www.bbc.co.uk/', function() {
18 nbLinks = this.evaluate(function() { 19 nbLinks = this.evaluate(function() {
19 return __utils__.findAll('#promo2_carousel_items_items li').length; 20 return __utils__.findAll('#promo2_carousel_items_items li').length;
20 }); 21 });
21 this.echo(nbLinks + ' items founds'); 22 this.echo("" + nbLinks + " items founds");
22 // hide navigation arrows 23 // hide navigation arrows
23 this.hide('.nav_left'); 24 this.hide('.nav_left');
24 this.hide('.nav_right'); 25 this.hide('.nav_right');
...@@ -36,14 +37,13 @@ casper.start('http://www.bbc.co.uk/', function() { ...@@ -36,14 +37,13 @@ casper.start('http://www.bbc.co.uk/', function() {
36 }); 37 });
37 }); 38 });
38 39
39 // Capture carrousel area
40 var next = function() { 40 var next = function() {
41 var image = 'bbcshot' + currentLink + '.png'; 41 var image = "bbcshot" + currentLink + ".png";
42 images.push(image); 42 images.push(image);
43 this.echo('Processing image ' + currentLink); 43 this.echo("Processing image " + currentLink);
44 this.captureSelector(image, '.carousel_viewport'); 44 this.captureSelector(image, '.carousel_viewport');
45 if (currentLink < nbLinks) { 45 if (currentLink < nbLinks) {
46 this.click('.carousel_itemList_li[rel="' + currentLink + '"]'); 46 this.click(".carousel_itemList_li[rel='" + currentLink + "']");
47 this.wait(1000, function() { 47 this.wait(1000, function() {
48 this.then(next); 48 this.then(next);
49 currentLink++; 49 currentLink++;
...@@ -53,18 +53,17 @@ var next = function() { ...@@ -53,18 +53,17 @@ var next = function() {
53 } 53 }
54 }; 54 };
55 55
56 // Building resulting page and image
57 var buildPage = function() { 56 var buildPage = function() {
58 this.echo('Build result page'); 57 this.echo('Build result page');
59 var fs = require('fs'); 58 var fs = require('fs');
60 this.viewport(624, 400); 59 this.viewport(624, 400);
61 var pageHtml = "<html><body style='background:black;margin:0;padding:0'>"; 60 var pageHtml = "<html><body style='background:black;margin:0;padding:0'>";
62 images.forEach(function(image) { 61 images.forEach(function(image) {
63 pageHtml += '<img src="file://' + fs.workingDirectory + '/' + image + '"><br>'; 62 pageHtml += "<img src='file://" + fs.workingDirectory + "/" + image + "'><br>";
64 }); 63 });
65 pageHtml += "</body></html>"; 64 pageHtml += "</body></html>";
66 fs.write('result.html', pageHtml, 'w'); 65 fs.write('result.html', pageHtml, 'w');
67 this.thenOpen('file://' + fs.workingDirectory + '/result.html', function() { 66 this.thenOpen("file://" + fs.workingDirectory + "/result.html", function() {
68 this.echo('Resulting image saved to result.png'); 67 this.echo('Resulting image saved to result.png');
69 this.capture('result.png'); 68 this.capture('result.png');
70 }); 69 });
......
...@@ -7,11 +7,11 @@ upTo = ~~casper.cli.get(0) || 10 # max 10 links ...@@ -7,11 +7,11 @@ upTo = ~~casper.cli.get(0) || 10 # max 10 links
7 # the ones which contains a href starting with 'http://' 7 # the ones which contains a href starting with 'http://'
8 searchLinks = -> 8 searchLinks = ->
9 filter = Array::filter 9 filter = Array::filter
10 map = Array::map 10 map = Array::map
11 links = document.querySelectorAll 'a' 11 map.call filter.call(document.querySelectorAll("a"), (a) ->
12 absolutes = filter.call links, (a) -> 12 (/^http:\/\/.*/i).test a.getAttribute("href")
13 /^http:\/\/.*/i.test a.getAttribute 'href' 13 ), (a) ->
14 return map.call absolutes, (a) -> a.getAttribute 'href' 14 a.getAttribute "href"
15 15
16 # The base links array 16 # The base links array
17 links = [ 17 links = [
...@@ -21,7 +21,9 @@ links = [ ...@@ -21,7 +21,9 @@ links = [
21 ] 21 ]
22 22
23 # Just opens the page and prints the title 23 # Just opens the page and prints the title
24 start = (link) -> @start link, -> @echo "Page title: #{ @getTitle() }" 24 start = (link) ->
25 @start link, ->
26 @echo "Page title: #{ @getTitle() }"
25 27
26 # Get the links, and add them to the links array 28 # Get the links, and add them to the links array
27 # (It could be done all in one step, but it is intentionally splitted) 29 # (It could be done all in one step, but it is intentionally splitted)
......
1 if (phantom.casperArgs.args.length !== 1) {
2 console.log('You must provide the maximum number of pages to visit');
3 phantom.exit(1);
4 }
5
6 var casper = require('casper').create({ 1 var casper = require('casper').create({
7 verbose: true 2 verbose: true
8 }); 3 });
...@@ -12,15 +7,15 @@ var upTo = ~~casper.cli.get(0) || 10; // max 10 links ...@@ -12,15 +7,15 @@ var upTo = ~~casper.cli.get(0) || 10; // max 10 links
12 7
13 // Fetch all <a> elements from the page and return 8 // Fetch all <a> elements from the page and return
14 // the ones which contains a href starting with 'http://' 9 // the ones which contains a href starting with 'http://'
15 function searchLinks() { 10 var searchLinks = function() {
16 var filter = Array.prototype.filter, 11 var filter = Array.prototype.filter;
17 map = Array.prototype.map; 12 var map = Array.prototype.map;
18 return map.call(filter.call(document.querySelectorAll('a'), function(a) { 13 return map.call(filter.call(document.querySelectorAll("a"), function(a) {
19 return (/^http:\/\/.*/i).test(a.getAttribute('href')); 14 return /^http:\/\/.*/i.test(a.getAttribute("href"));
20 }), function(a) { 15 }), function(a) {
21 return a.getAttribute('href'); 16 return a.getAttribute("href");
22 }); 17 });
23 } 18 };
24 19
25 // The base links array 20 // The base links array
26 var links = [ 21 var links = [
...@@ -30,39 +25,42 @@ var links = [ ...@@ -30,39 +25,42 @@ var links = [
30 ]; 25 ];
31 26
32 // Just opens the page and prints the title 27 // Just opens the page and prints the title
33 var start = function(self, link) { 28 var start = function(link) {
34 self.start(link, function(self) { 29 this.start(link, function() {
35 self.echo('Page title: ' + self.getTitle()); 30 this.echo("Page title: " + (this.getTitle()));
36 }); 31 });
37 }; 32 };
38 33
39 // Get the links, and add them to the links array 34 // Get the links, and add them to the links array
40 // (It could be done all in one step, but it is intentionally splitted) 35 // (It could be done all in one step, but it is intentionally splitted)
41 var addLinks = function(link) { 36 var addLinks = function(link) {
42 this.then(function(self) { 37 this.then(function() {
43 var found = self.evaluate(searchLinks); 38 var found = this.evaluate(searchLinks);
44 self.echo(found.length + " links found on " + link); 39 this.echo("" + found.length + " links found on " + link);
45 links = links.concat(found); 40 links = links.concat(found);
46 }); 41 });
47 }; 42 };
48 43
49 casper.start().then(function(self) { 44 casper.start();
50 self.echo('Starting'); 45
46 casper.then(function() {
47 this.echo('Starting');
51 }); 48 });
52 49
53 var currentLink = 0; 50 var currentLink = 0;
54 51
55 // As long as it has a next link, and is under the maximum limit, will keep running 52 // As long as it has a next link, and is under the maximum limit, will keep running
56 function check(self) { 53 var check = function() {
57 if (links[currentLink] && currentLink < upTo) { 54 if (links[currentLink] && currentLink < upTo) {
58 self.echo('--- Link ' + currentLink + ' ---'); 55 this.echo("--- Link " + currentLink + " ---");
59 start(self, links[currentLink]); 56 start.call(this, links[currentLink]);
60 addLinks.call(self, links[currentLink]); 57 addLinks.call(this, links[currentLink]);
61 currentLink++; 58 currentLink++;
62 self.run(check); 59 this.run(check);
63 } else { 60 } else {
64 self.echo('All done.').exit(); 61 this.echo('All done.');
62 this.exit();
65 } 63 }
66 } 64 };
67 65
68 casper.run(check); 66 casper.run(check);
......
...@@ -19,9 +19,9 @@ casper.fetchScore = -> ...@@ -19,9 +19,9 @@ casper.fetchScore = ->
19 19
20 terms = casper.cli.args # terms are passed through command-line arguments 20 terms = casper.cli.args # terms are passed through command-line arguments
21 21
22 if terms.length < 3 22 if terms.length < 2
23 casper 23 casper
24 .echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...") 24 .echo("Usage: $ casperjs googlematch.js term1 term2 [term3]...")
25 .exit(1) 25 .exit(1)
26 26
27 scores = [] 27 scores = []
...@@ -38,7 +38,7 @@ casper.each terms, (self, term) -> ...@@ -38,7 +38,7 @@ casper.each terms, (self, term) ->
38 self.echo "#{term}: #{score}" 38 self.echo "#{term}: #{score}"
39 39
40 casper.run -> 40 casper.run ->
41 scores.sort -> (a, b) -> b.score - a.score; 41 scores.sort (a, b) -> b.score - a.score;
42 winner = scores[0] 42 winner = scores[0]
43 @echo "Winner is #{winner.term} with #{winner.score} results" 43 @echo "Winner is \"#{winner.term}\" with #{winner.score} results"
44 @exit() 44 @exit()
......
...@@ -23,9 +23,9 @@ casper.fetchScore = function() { ...@@ -23,9 +23,9 @@ casper.fetchScore = function() {
23 23
24 var terms = casper.cli.args; 24 var terms = casper.cli.args;
25 25
26 if (terms.length < 3) { 26 if (terms.length < 2) {
27 casper 27 casper
28 .echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...") 28 .echo("Usage: $ casperjs googlematch.js term1 term2 [term3]...")
29 .exit(1) 29 .exit(1)
30 ; 30 ;
31 } 31 }
...@@ -36,24 +36,28 @@ casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!"); ...@@ -36,24 +36,28 @@ casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!");
36 36
37 casper.start("http://google.fr/"); 37 casper.start("http://google.fr/");
38 38
39 casper.each(terms, function(self, term, i) { 39 casper.each(terms, function(self, term) {
40 self.then(function(self) { 40 this.then(function() {
41 self.fill('form[action="/search"]', { q: term }, true); 41 this.fill('form[action="/search"]', {
42 }).then(function(self) { 42 q: term
43 var score = self.fetchScore(); 43 }, true);
44 });
45 this.then(function() {
46 var score;
47 score = this.fetchScore();
44 scores.push({ 48 scores.push({
45 term: term, 49 term: term,
46 score: score 50 score: score
47 }); 51 });
48 self.echo(term + ': ' + score); 52 self.echo("" + term + ": " + score);
49 }); 53 });
50 }); 54 });
51 55
52 casper.run(function(self) { 56 casper.run(function() {
53 scores.sort(function(a, b) { 57 scores.sort(function(a, b) {
54 return b.score - a.score; 58 return b.score - a.score;
55 }); 59 });
56 var winner = scores[0]; 60 var winner = scores[0];
57 self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results'); 61 this.echo("Winner is \"" + winner.term + "\" with " + winner.score + " results");
58 self.exit(); 62 this.exit();
59 }); 63 });
......
1 ### 1 ###
2 Capture multiple pages of google search results 2 Capture multiple pages of google search results
3 3
4 usage: casperjs googlepagination.coffee my search terms 4 Usage: $ casperjs googlepagination.coffee my search terms
5 5
6 (all arguments will be used as the query) 6 (all arguments will be used as the query)
7 ### 7 ###
...@@ -10,30 +10,30 @@ casper = require('casper').create() ...@@ -10,30 +10,30 @@ casper = require('casper').create()
10 currentPage = 1 10 currentPage = 1
11 11
12 if casper.cli.args.length is 0 12 if casper.cli.args.length is 0
13 casper 13 casper
14 .echo("Usage: $ casperjs my search terms") 14 .echo("Usage: $ casperjs googlepagination.coffee my search terms")
15 .exit(1) 15 .exit(1)
16 16
17 processPage = -> 17 processPage = ->
18 @echo "capturing page #{currentPage}" 18 @echo "capturing page #{currentPage}"
19 @capture "google-results-p#{currentPage}.png" 19 @capture "google-results-p#{currentPage}.png"
20 20
21 # don't go too far down the rabbit hole 21 # don't go too far down the rabbit hole
22 return if currentPage >= 5 22 return if currentPage >= 5
23 23
24 if @exists "#pnnext" 24 if @exists "#pnnext"
25 currentPage++ 25 currentPage++
26 @echo "requesting next page: #{currentPage}" 26 @echo "requesting next page: #{currentPage}"
27 #@thenClick("#pnnext").then(processPage) 27 #@thenClick("#pnnext").then(processPage)
28 url = @getCurrentUrl() 28 url = @getCurrentUrl()
29 @thenClick("#pnnext").then -> 29 @thenClick("#pnnext").then ->
30 check = -> url != @getCurrentUrl() 30 check = -> url != @getCurrentUrl()
31 @waitFor check, processPage 31 @waitFor check, processPage
32 else 32 else
33 @echo "that's all, folks." 33 @echo "that's all, folks."
34 34
35 casper.start 'http://google.fr/', -> 35 casper.start 'http://google.fr/', ->
36 @fill 'form[action="/search"]', q: casper.cli.args.join(' '), true 36 @fill 'form[action="/search"]', q: casper.cli.args.join(' '), true
37 37
38 casper.then processPage 38 casper.then processPage
39 39
......
1 /* 1 /*
2 Capture multiple pages of google search results 2 Capture multiple pages of google search results
3 3
4 Usage: casperjs googlepagination.coffee my search terms 4 Usage: $ casperjs googlepagination.coffee my search terms
5 5
6 (all arguments will be used as the query) 6 (all arguments will be used as the query)
7 */ 7 */
...@@ -12,7 +12,7 @@ var currentPage = 1; ...@@ -12,7 +12,7 @@ var currentPage = 1;
12 12
13 if (casper.cli.args.length === 0) { 13 if (casper.cli.args.length === 0) {
14 casper 14 casper
15 .echo("Usage: $ casperjs my search terms") 15 .echo("Usage: $ casperjs googlepagination.coffee my search terms")
16 .exit(1) 16 .exit(1)
17 ; 17 ;
18 } 18 }
......
1 casper = require('casper').create 1 casper = require('casper').create
2 verbose: true 2 verbose: true
3 logLevel: 'debug' 3 logLevel: 'debug'
4 4
5 casper.log 'this is a debug message', 'debug' 5 casper.log 'this is a debug message', 'debug'
6 casper.log 'and an informative one', 'info' 6 casper.log 'and an informative one', 'info'
......
1 casper = require('casper').create verbose: true 1 casper = require('casper').create verbose: true
2 2
3 countLinks = -> document.querySelectorAll('a').length 3 countLinks = ->
4 document.querySelectorAll('a').length
4 5
5 suites = [ 6 suites = [
6 -> 7 ->
...@@ -19,7 +20,8 @@ suites = [ ...@@ -19,7 +20,8 @@ suites = [
19 20
20 casper.start() 21 casper.start()
21 22
22 casper.then -> @echo('Starting') 23 casper.then ->
24 @echo('Starting')
23 25
24 currentSuite = 0; 26 currentSuite = 0;
25 27
...@@ -29,7 +31,7 @@ check = -> ...@@ -29,7 +31,7 @@ check = ->
29 currentSuite++; 31 currentSuite++;
30 casper.run check 32 casper.run check
31 else 33 else
32 @echo 'All done.' 34 @echo "All done."
33 @exit() 35 @exit()
34 36
35 casper.run check 37 casper.run check
......
...@@ -2,51 +2,55 @@ var casper = require('casper').create({ ...@@ -2,51 +2,55 @@ var casper = require('casper').create({
2 verbose: true 2 verbose: true
3 }); 3 });
4 4
5 function countLinks() { 5 var countLinks = function() {
6 return document.querySelectorAll('a').length; 6 return document.querySelectorAll('a').length;
7 } 7 };
8 8
9 var suites = [ 9 var suites = [
10 function(self) { 10 function() {
11 self.echo('Suite 1'); 11 this.echo('Suite 1');
12 self.start('http://google.com/', function(self) { 12 this.start('http://google.com/', function() {
13 self.echo('Page title: ' + self.getTitle()); 13 this.echo("Page title: " + (this.getTitle()));
14 }).then(function(self) {
15 self.echo(self.evaluate(countLinks) + ' links');
16 }); 14 });
17 }, 15 this.then(function() {
18 function(self) { 16 this.echo("" + (this.evaluate(countLinks)) + " links");
19 self.echo('Suite 2');
20 self.start('http://yahoo.com/', function(self) {
21 self.echo('Page title: ' + self.getTitle());
22 }).then(function(self) {
23 self.echo(self.evaluate(countLinks) + ' links');
24 }); 17 });
25 }, 18 }, function() {
26 function(self) { 19 this.echo('Suite 2');
27 self.echo('Suite 3'); 20 this.start('http://yahoo.com/', function() {
28 self.start('http://bing.com/', function(self) { 21 this.echo("Page title: " + (this.getTitle()));
29 self.echo('Page title: ' + self.getTitle()); 22 });
30 }).then(function(self) { 23 this.then(function() {
31 self.echo(self.evaluate(countLinks) + ' links'); 24 this.echo("" + (this.evaluate(countLinks)) + " links");
25 });
26 }, function() {
27 this.echo('Suite 3');
28 this.start('http://bing.com/', function() {
29 this.echo("Page title: " + (this.getTitle()));
30 });
31 this.then(function() {
32 this.echo("" + (this.evaluate(countLinks)) + " links");
32 }); 33 });
33 } 34 }
34 ]; 35 ];
35 36
36 casper.start().then(function(self) { 37 casper.start();
37 self.echo('Starting'); 38
39 casper.then(function() {
40 this.echo('Starting');
38 }); 41 });
39 42
40 var currentSuite = 0; 43 var currentSuite = 0;
41 44
42 function check(self) { 45 var check = function() {
43 if (suites[currentSuite]) { 46 if (suites[currentSuite]) {
44 suites[currentSuite](casper); 47 suites[currentSuite].call(this);
45 currentSuite++; 48 currentSuite++;
46 casper.run(check); 49 casper.run(check);
47 } else { 50 } else {
48 self.echo('All done.').exit(); 51 this.echo("All done.");
52 this.exit();
49 } 53 }
50 } 54 };
51 55
52 casper.run(check); 56 casper.run(check);
......