lunes, 14 de julio de 2014

Getting Started with PhantomJS - Book Review

A couple of days ago I start reading Getting Started with PhantomJS from Packt Publishing. I had heard about PhantomJS in the past but never really use it...so I was really excited about reading the book...


Like all the Getting Started books, this one is kind of short...with 140 pages...but let me tell you...that's more than enough to keep your attention and make a PhatomJS advocate -;)

The book starts with a little introduction and then jumps straight into the code examples...which is something that I always appreciate -:P

The first important example is based on Pinterest but I don't use/like Pinterest...so I change the example a little bit to use Twitter instead -;)

Here's the source code in case you're interested...

var system = require('system');
var userid = system.args[1];
var page = require('webpage').create();

var profileUrl = "http://www.twitter.com/" + userid;
page.open(profileUrl, function(status) {
 if ( status === "success" ) {
  var twitter = page.evaluate(function (uid) {
  var username = document.querySelector('[href="/' + uid + '"]').innerText.trim();
  var numTweets = document.querySelector('[data-nav="tweets"]');
  numTweets = numTweets.attributes[1].value;
  var numFollowing = document.querySelector('[href="/' + uid + '/following"]');
  numFollowing = numFollowing.querySelector('[class="ProfileNav-value"]').innerText;  
  var numFollowers = document.querySelector('[href="/' + uid + '/followers"]');
  numFollowers = numFollowers.querySelector('[class="ProfileNav-value"]').innerText;    
  return {
   name: username,
   tweets: numTweets,
   following: numFollowing,
   followers: numFollowers
         };
}, userid);

console.log(twitter.name + ' (' + userid + ')' + ' has wrote ' + twitter.tweets + ' and has ' +
      twitter.followers + ' followers and is following ' +
      twitter.following + ' accounts ');
}

phantom.exit(0);
});


And here's the output...



Cool, huh? For a very first example...I think it's impressive -;)

The book also comes with example on taking webpages screenshots, loading performance, modification of the DOM, working with files and more...

It includes even a small introduction to CasperJS, the perfect companion to PhantomJS.

I would say...just go ahead and buy this book...I totally love it and will read it again just to discover and learn more....PhantomJS is just amazing!

Greetings,

Blag.
Development Culture.

No hay comentarios: