ariya.io About Talks Articles

water lily

3 min read

Nymphaea japan

By Kazuyanagae (Own work), CC-BY-SA-3.0, via Wikimedia Commons

Last Friday was an equinox. It also happened to be the scheduled release date for the latest version 1.3 of PhantomJS, the scriptable headless WebKit. Its code repository has been tagged so that you can build it from source. Alternatively, just use the source packages. Expect binary packages for major operating systems to catch up and become available in few days or weeks.

For a complete and detailed info of the new features and other changes in this version, I refer you to the official release notes. Most of you are probably excited with the filesystem module, originally championed by James and Ivan. This module has a lot of functions to deal with files and directories, modelled after CommonJS Filesystem proposal. Included in the examples folder is a short script demonstrating the API:

// List all the files in a directory
 
if (phantom.args.length !== 1) {
    console.log("Usage: phantomjs scandir.js DIRECTORY_TO_SCAN");
    phantom.exit();
}
 
var scanDirectory = function (path) {
    var fs = require('fs');
    if (fs.exists(path) && fs.isFile(path)) {
        console.log(path);
    } else if (fs.isDirectory(path)) {
        fs.list(path).forEach(function (e) {
            if ( e !== "." && e !== ".." ) {
                scanDirectory(path + '/' + e);
            }
        });
    }
};
scanDirectory(phantom.args[]);
phantom.exit();

My other favorite new weird feature is the callback for page initialization. It is triggered after the web page is created but right before any client code in the loaded URL is executed. This has a nice side effect: you can hijack and tweak the global objects that the web page will see. For example, this short code fragment effectively modifies the behavior of Math.random() to do something completely different:

page.onInitialized = function() {
    Math.random = function() {
        return 0.42; // one-hundredth of "The Answer"
    };
};

I’m confident there are other useful tricks you can achieve by employing this initialization callback.

There are also other nice new goodies as well as minor bug fixes which may be of interest to you. Again, details can be read in the release notes.

Since the previous release, there have been more coverage of PhantomJS in various blogs. If you are curious to see the different use cases, have a look at the compiled list of those external articles. The number of related software projects and organizations which is known to use PhantomJS is also increasing rapidly.

This third minor update Water Lily follows the path of the previous two, Birds of Paradise and Cherry Blossom (there are of course the stories why the names are chosen that way). As for the future, the plan should reveal itself in the coming weeks. Expect a delightful beauty to pave the way for PhantomJS 1.4!

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook