# phantomstream

> Streaming interface to the PhantomJS headless browser

Latest version **0.0.1** (published 2012-09-10) · 0 weekly downloads

## Install

```sh
npm install phantomstream
pnpm add phantomstream
yarn add phantomstream
bun add phantomstream
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2012-09-10 |
| First published | 2012-09-10 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matt Pizzimenti |
| Maintainers | mjpizz |

## Links

- npm: https://www.npmjs.com/package/phantomstream
- npm.io page: https://npm.io/package/phantomstream

## Dependencies (2)

- [temp](https://npm.io/package/temp.md) *
- [coffee-script](https://npm.io/package/coffee-script.md) *

## Recent versions

- 0.0.1 (latest) — 2012-09-10

## README

# phantomstream

Write and automate [PhantomJS](http://phantomjs.org) scripts inside node, using
a standard [Stream](http://nodejs.org/api/stream.html) interface.

# Getting Started

First off, install the `phantomstream` module.

    npm install phantomstream

Next, create a simple script named "myscript.js".

```javascript
phantomstream = require("../phantomstream")

// Open up a PhantomJS stream that listens for commands that look like
// "TITLE <url>". Browse to the URL and write the title back via the stream.
var conf = {logger: console}
var ps = phantomstream.open(conf, function(nodestream, phantom, require, global) {

  // This callback executes inside of PhantomJS, not node.  To see the API
  // available to you in PhantomJS, check out their documentation:
  // http://code.google.com/p/phantomjs/wiki/Interface
  var page = require("webpage").create()

  // Listen for the "TITLE <url>" commands via the streaming interface.
  nodestream.on("data", function(data) {
    var matches = /TITLE\s*(.+)/.exec(data)
    if (matches) {
      var url = matches[1]

      // Got a TITLE command, use the PhantomJS page object to browse
      // to that page, get the document title, and write it back to the stream.
      console.info("opening", url)
      page.open(url, function() {
        var title = page.evaluate(function() {
          return document.title
        })
        console.info("sending title =", title)
        nodestream.write(title)
      })
    }
  })

})

// Send an TITLE command to our PhantomJS process and echo the response.
ps.write("TITLE http://www.google.com")
ps.on("data", function(data) {
  console.info("node received:", data.toString())
  process.exit()
})
```

Run your script with node.

    node myscript.js

...you should see output like this:

    [phantom-stdout] opening http://www.google.com
    [phantom-stdout] sending title = Google
    node received: Google

Enjoy! For more ideas on what is possible inside the callback for
`phantomstream.open()`, read the
[PhantomJS documentation](http://code.google.com/p/phantomjs/wiki/Interface).

Don't forget try out some demos from the `examples/` directory :)

# Alternatives

If you are looking for higher-level implementations that wrap the PhantomJS API,
check these out:

* [phantomjs-node](https://github.com/sgentle/phantomjs-node)
* [node-phantomjs-sync](https://github.com/sebv/node-phantomjs-sync)

---
_Source: https://npm.io/package/phantomstream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
