https://github.com/cap32/phantom-page-promise
A phantomjs-node extension that make phantom page support promise.
https://github.com/cap32/phantom-page-promise
Last synced: 2 months ago
JSON representation
A phantomjs-node extension that make phantom page support promise.
- Host: GitHub
- URL: https://github.com/cap32/phantom-page-promise
- Owner: Cap32
- License: mit
- Created: 2016-11-24T03:42:29.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-13T17:10:57.000Z (about 9 years ago)
- Last Synced: 2025-03-22T14:26:08.036Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# phantom-page-promise
A phantomjs-node extension that make phantom page support promise.
## Usage
```js
import 'phantom-page-promise';
import phantom from 'phantom';
const init = async () => {
const ph = await phantom.create();
const page = await ph.createPage();
const status = await page.open('http://facebook.com');
if (status.toLowerCase() !== 'success') { throw new Error(status); }
const result = await page.evaluatePromise(
`function () {
return new Promise(function (resolve) {
setTimeout(function () {
resolve('it works');
}, 2000);
})
}`
);
console.log(result); // 'it works'
ph.exit();
page.close();
};
init();
```
## API
##### page#evaluatePromise(codeOrFunction[, options])
Just like `page.evaluateJavaScript`, but could evaluate and return a promise.
###### Arguments
1. `codeOrFunction` (String|Function): Evaluate a function or function contained in a string.
2. `options` (Object): See below for detail.
###### Available options:
- `timeout` (Number): Specify timeout of execution. If timeout before the promise return, it would throw a `TIMEOUT` error. Defaults to 10000(ms).
- `delay` (Number): Specify delay of execution. Defaults to 0(ms).
- `args` ([Any]): Specify arguments to the function. Only work if the type of `codeOrFunction` is `Function`.
###### Return
(Promise): The result.
## Installing
Using npm:
```
npm install --save phantom-page-promise
```
Using yarn:
```
yarn add phantom-page-promise
```
## License
MIT