Review of “Getting Started with PhantomJS”

Review of Packt Publishing’s “Getting Started with Phantom JS” by Aries Beltran

[iframe src=”http://rcm-na.amazon-adsystem.com/e/cm?t=marcboin-20&o=1&p=8&l=as1&asins=1782164227&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr” style=”width:120px;height:240px;” scrolling=”no” marginwidth=”0″ marginheight=”0″ frameborder=”0″]

I was asked by Packt to review several books, but I chose “Getting Started with PhantomJS” because I was actually interested in it! I’ve used various faceless web browsers before, in particular webkit in GTK applications like wkhtmltopdf or with python or PHP bindings. wkhtmltopdf (and the related wkhtmltoimage) in particular has been suffering from neglect – it has lots of depedencies and it’s difficult and unreliable to build and use (something I’ve written about before). I’ve most often used server-side browsers for tasks like generating page preview images, and seeing that phantomjs will do that, I’ve long thought I’d like to know more about it so that I could get away from custom-building complicated webkit stacks.

As far as I’m concerned, the two key things that phantomjs brings are a straightforward build process (a simple ‘brew install phantomjs’ for me) and a simple way of scripting the virtual browser, with the ability to inject scripts into the page, without having to resort to peculiar tricks, messing with virtual frame buffers or installing odd browser plugins.

I hadn’t realised phantomjs’s own scripting environment was quite so complete, supporting commonJS module integration. The separation between browser and page contexts (using `evaluate`) is clean and easy to get to grips with, and the book presents this well.

The book mentions several extensions to phantomjs that I had not encountered and look useful (particularly casper).

I hadn’t spent much time reading phantomjs’s own docs, but when I looked at them I found that they are very limited. Even though it’s not long, the book goes deeper into examples and explanations than the docs, so there is genuine value in having the book.

All of the example code I tried worked without a hitch. Packt’s web site sets a wrong MIME type on zip downloads, resulting in a page full of rubbish, but it unzips ok when saved manually. There are more example files than are mentioned in the book, which is a welcome bonus.

One small error I spotted suggested using single quotes around JSON values – that’s not valid JSON, though it is valid Javascript. It also mischaracterises JSON slightly – object syntax is part of the Javascript language, so it’s not a separate thing when you’re already in a Javascript context.

One formatting issue costs a little typing – while all the code samples are provided as text and in files, all the displayed command lines (for example when a long URL is passed as a param) are in images, so you can’t copy and paste commands as text. Call me lazy!

The English is generally good, concise and to the point. This is not a long book, but it doesn’t need to be as a “getting started” guide on something that is a pretty confined subject. The editing had a few holes – several typos had sneaked through, things that would have been caught by any spell checker. The code samples had been updated recently, but there were no errata. Oddly, getting to errata is annoying on Packt’s site – when you’re looking at a book’s page there is no link for it. You have to go to the “code and errata page” and select the book from the pop-up menu. This menu is sorted by the exact book title (and contains ALL their book titles!), so I had to look under ‘getting…’ rather than ‘phantomjs’. This could be made much easier.

There are several other books and resources for learning and using phantomjs, and they may be sufficient for some users as it is a fairly small subject to cover. Overall I was impressed with the book. It does exactly what the title says, provides useful links for further reading, and provides effective, useful scripts that cover much of what many will want phantomjs to do in sufficient detail to make it easy to derive your own. Well done Aries!

Setting up a rear derailleur

I needed to set up my rear derailleur from scratch yesterday and thought up a nice simple mechanism for doing it that I’ve not seen described before. This is for a ‘normal’, not reverse-pull derailleur, where increased gear cable tension makes it change down.

  1. Put the bike in middle ring at the front, set the rear shifter to top gear.
  2. Adjust the limit screws.
    I find this is especially easy if you disconnect the gear cable completely – it prevents cable issues interfering and means you don’t need to fiddle with the shifter.

    • Adjust the bottom limit screw so that the bike pedals smoothly in top gear with no clicking or rubbing.
    • Push the derailleur with your fingers so that it shifts into bottom gear and adjust the top limit screw so that the bike pedals smoothly in bottom gear with no clicking or rubbing and without falling into the spokes.
  3. Screw in the barrel adjuster on the derailleur (and the shifter) as far as it will go.
  4. Pull the gear cable tight with your fingers (you don’t need much tension, just take up the slack) and tighten the retaining bolt.
  5. On the shifter, change down ONE gear (e.g. 8th if you have a 9-speed cassette).
  6. While turning the pedals, turn the barrel adjuster until it shifts into the selected gear.
  7. Adjust so it’s not rubbing and that the top jockey wheel aligns nicely.
  8. Check that it changes into all the gears smoothly when changing both up and down.
  9. Job done!

This whole procedure can be done in a couple of minutes. You may need to fine-tune the barrel adjuster slightly in some lower gears, but this procedure will get you to the right ballpark with minimal effort. It’s much easier if you have a workshop stand or similar means of holding the back wheel off the ground.