npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-lib-phantomjs

v1.1.0

Published

Grunt and PhantomJS, sitting in a tree

Downloads

95,899

Readme

grunt-lib-phantomjs Build Status: Linux Build Status: Windows

Grunt and PhantomJS, sitting in a tree.

Usage

The best way to understand how this lib should be used is by looking at the grunt-contrib-qunit plugin. Mainly, look at how the lib is required, how event handlers are bound and how PhantomJS is actually spawned.

Also, in the case of the grunt-contrib-qunit plugin, it's important to know that the page being loaded into PhantomJS doesn't know it will be loaded into PhantomJS, and as such doesn't have any PhantomJS->Grunt code in it. That communication code, aka. the "bridge", is dynamically injected into the html page.

An inline example

If a Grunt task looked something like this:

grunt.registerTask('mytask', 'Integrate with phantomjs.', function() {
  var phantomjs = require('grunt-lib-phantomjs').init(grunt);
  var errorCount = 0;

  // Handle any number of namespaced events like so.
  phantomjs.on('mytask.ok', function(msg) {
    grunt.log.writeln(msg);
  });

  phantomjs.on('mytask.error', function(msg) {
    errorCount++;
    grunt.log.error(msg);
  });

  // Create some kind of "all done" event.
  phantomjs.on('mytask.done', function() {
    phantomjs.halt();
  });

  // Built-in error handlers.
  phantomjs.on('fail.load', function(url) {
    phantomjs.halt();
    grunt.warn('PhantomJS unable to load URL.');
  });

  phantomjs.on('fail.timeout', function() {
    phantomjs.halt();
    grunt.warn('PhantomJS timed out.');
  });

  // This task is async.
  var done = this.async();

  // Spawn phantomjs
  phantomjs.spawn('test.html', {
    // Additional PhantomJS options.
    options: {},
    // Complete the task when done.
    done: function(err) {
      done(err || errorCount === 0);
    }
  });

});

And test.html looked something like this (note the "bridge" is hard-coded into this page and not injected):

<!doctype html>
<html>
<head>
<script>

// Send messages to the parent PhantomJS process via alert! Good times!!
function sendMessage() {
  var args = [].slice.call(arguments);
  alert(JSON.stringify(args));
}

sendMessage('mytask.ok', 'Something worked.');
sendMessage('mytask.error', 'Something failed.');
sendMessage('mytask.done');

</script>
</head>
<body>
</body>
</html>

Then running Grunt would behave something like this:

$ grunt mytask
Running "mytask" task
Something worked.
>> Something failed.
Warning: Task "mytask" failed. Use --force to continue.

Aborted due to warnings.

API

phantomjs.halt()

Call this when everything has finished successfully, or when something horrible happens, and you need to clean up and abort.

phantomjs.spawn(pageURL, options)

Spawn a PhantomJS process. The method returns a reference to the spawned process.
This method has the following arguments:

pageURL

Type: string
Default: no default value, the user has to set it explicitly.

URL or path to the page .html test file to run.

Options

Type: object

The options object has these possible properties:

done

Type: function
Default: no default value, the user has to set it explicitly.

The callback to call when the task is done.

failCode

Type: number
Default: 0

The error code to exit with when an Error occurs.

killTimeout

Type: number
Default: 1000 ms

The timeout in milliseconds after which the PhantomJS process will be killed.

options (PhantomJS options)

Type: object
Default: {}

Additional options to passe to PhantomJS. This object has the following properties:

timeout

Type: number
Default: undefined

PhantomJS' timeout, in milliseconds.

inject

Type: string|array
Default: undefined

One or multiple (array) JavaScript file names to inject into the page.

page

Type: object
Default: undefined

An object of options for the PhantomJS page object.

screenshot

Type: boolean
Default: undefined

Saves a screenshot on failure

OS Dependencies

PhantomJS requires these dependencies on Ubuntu/Debian:

apt-get install libfontconfig1 fontconfig libfontconfig1-dev libfreetype6-dev