Created
November 13, 2011 12:12
-
-
Save n1k0/1362045 to your computer and use it in GitHub Desktop.
CasperJS documentation examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phantom.injectJs "casper.js" | |
getLinks = -> | |
links = document.querySelectorAll "h3.r a" | |
Array::map.call links, (e) -> e.getAttribute "href" | |
links = [] | |
casper = new phantom.Casper() | |
casper.start "http://google.fr/", (self) -> | |
"search for 'casperjs' from google form" | |
self.fill "form[name=f]", q: "casperjs", true | |
casper.then (self) -> | |
"aggregate results for the 'casperjs' search" | |
links = self.evaluate(getLinks) | |
"search for 'phantomjs' from google form" | |
self.fill "form[name=f]", q: "phantomjs", true | |
casper.then (self) -> | |
"concat results for the 'phantomjs' search" | |
links = links.concat(self.evaluate(getLinks)) | |
casper.run (self) -> | |
"display results" | |
self.echo links.length + " links found:" | |
self.echo(" - " + links.join("\n - ")).exit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phantom.injectJs('casper.js'); | |
var links = []; | |
var casper = new phantom.Casper(); | |
function getLinks() { | |
var links = document.querySelectorAll('h3.r a'); | |
return Array.prototype.map.call(links, function(e) { | |
return e.getAttribute('href') | |
}); | |
} | |
casper.start('http://google.fr/', function(self) { | |
// search for 'casperjs' from google form | |
self.fill('form[name=f]', { q: 'casperjs' }, true); | |
}); | |
casper.then(function(self) { | |
// aggregate results for the 'casperjs' search | |
links = self.evaluate(getLinks); | |
// now search for 'phantomjs' by fillin the form again | |
self.fill('form[name=f]', { q: 'phantomjs' }, true); | |
}); | |
casper.then(function(self) { | |
// aggregate results for the 'phantomjs' search | |
links = links.concat(self.evaluate(getLinks)); | |
}); | |
casper.run(function(self) { | |
// echo results in some pretty fashion | |
self.echo(links.length + ' links found:'); | |
self.echo(' - ' + links.join('\n - ')).exit(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ phantomjs samples/googlelinks.js | |
20 links found: | |
- https://github.com/n1k0/casperjs | |
- https://github.com/n1k0/casperjs/issues/2 | |
- https://github.com/n1k0/casperjs/tree/master/samples | |
- https://github.com/n1k0/casperjs/commits/master/ | |
- http://www.facebook.com/people/Casper-Js/100000337260665 | |
- http://www.facebook.com/public/Casper-Js | |
- http://hashtags.org/tag/CasperJS/ | |
- http://www.zerotohundred.com/newforums/members/casper-js.html | |
- http://www.yellowpages.com/casper-wy/j-s-enterprises | |
- http://local.trib.com/casper+wy/j+s+chinese+restaurant.zq.html | |
- http://www.phantomjs.org/ | |
- http://code.google.com/p/phantomjs/ | |
- http://code.google.com/p/phantomjs/wiki/QuickStart | |
- http://svay.com/blog/index/post/2011/08/31/Paris-JS-10-%3A-Introduction-%C3%A0-PhantomJS,-un-navigateur-webkit-headless | |
- https://github.com/ariya/phantomjs | |
- http://dailyjs.com/2011/01/28/phantoms/ | |
- http://css.dzone.com/articles/phantom-js-alternative | |
- http://pilvee.com/blog/tag/phantom-js/ | |
- http://ariya.blogspot.com/2011/01/phantomjs-minimalistic-headless-webkit.html | |
- http://www.readwriteweb.com/hack/2011/03/phantomjs-the-power-of-webkit.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment