Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/panosoft/phantom-promise
A PhantomJS bridge with a promise based api.
https://github.com/panosoft/phantom-promise
Last synced: about 2 months ago
JSON representation
A PhantomJS bridge with a promise based api.
- Host: GitHub
- URL: https://github.com/panosoft/phantom-promise
- Owner: panosoft
- Created: 2015-08-03T19:11:37.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-05T21:36:07.000Z (almost 9 years ago)
- Last Synced: 2024-09-16T18:39:46.399Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# phantom-promise
A [PhantomJS](http://phantomjs.org/) bridge with a promise based api.
[![npm version](https://img.shields.io/npm/v/phantom-promise.svg)](https://www.npmjs.com/package/phantom-promise)
[![Travis](https://img.shields.io/travis/panosoft/phantom-promise.svg)](https://travis-ci.org/panosoft/phantom-promise)## Installation
```sh
npm install phantom-promise
```## Usage
```js
var Phantom = require('phantom-promise');var phantom = Phantom.create();
phantom.initialize()
.then(() => phantom.createPage())
.then((page) => {
var pageFunction = function () {
var result = 'Hello from Phantom.';
window.callPhantom(result);
};
return page.evaluate(pageFunction);
})
.then((result) => {
console.log(result); //=> Hello from Phantom.
phantom.shutdown();
});
```## API
__Phantom__
- [`create`](#create)
- [`createPage`](#createPage)
- [`initialize`](#initialize)
- [`shutdown`](#shutdown)__Page__
- [`close`](#close)
- [`evaluate`](#evaluate)
- [`get`](#get)
- [`injectJs`](#injectJs)
- [`set`](#set)---
### Phantom
Returns an instance of `Phantom`.
__Example__
```js
var Phantom = require('phantom-promise');var phantom = Phantom.create();
```---
Creates a [Web Page](http://phantomjs.org/api/webpage/) in PhantomJs. Returns a `Promise` that is fulfilled with an instance of `Page`.
__Example__
```js
phantom.createPage()
.then((page) => {
// ...
})
```---
Initializes the `Phantom` instance. Returns a `Promise` that is fulfilled once the initialization is complete.
This must be called before the instance can be used.
__Example__
```js
phantom.initialize()
.then(() => {
// ...
});
```---
Shuts down the phantom instance. Once this has been called, the instance is no longer operable unless it is re-initialized.
__Example__
```js
phantom.shutdown();
```---
### Page
Closes the page. Once this has been called, the page instance can no longer be used.
__Example__
```js
page.close();
```---
Evaluates a function on the page. Returns a `Promise` that is fulfilled with the return value of the function.
__Arguments__
- `fn` - The function to evaluate on the page. This function must call `window.callPhantom(result)` in order to return.
- `arg` - An argument to evaluate `fn` with. This argument must be JSON-serializable (i.e. Closures, functions, DOM nodes, etc. will not work!).__Example__
```js
var pageFunction = function (arg) {
window.callPhantom(arg);
};var arg = 'Hello from Phantom.';
page.evaluate(pageFunction, arg)
.then((result) => {
console.log(result); //=> 'Hello from Phantom.'
});
```---
Returns a Promise that is fulfilled with the requested page property.
__Arguments__
- `property` - A string determining the property to return.
__Example__
```js
page.get('viewportSize')
.then((viewportSize) => {
// ...
});
```---
Injects external scripts into the page. The scripts are loaded in the order they are supplied so that dependencies can be met.
__Arguments__
- `paths` - A path or an array of paths to the script files to be injected.
__Example__
```js
page.injectJs('path/to/external/script.js');
```---
Sets a page property. Returns a promise that is fulfilled with the result.
__Arguments__
- `property` - A string determining the property to set.
- `value` - The value to apply.__Example__
```js
page.set('viewportSize', {height: 768, width: 1024});
```