Simple and reliable bridge between Node.js and PhantomJS / SlimerJS
This module is API-compatible with
node-phantom but doesn't rely on
socket.io. In essence the communication between Node and
Phantom / Slimer has been simplified significantly. It has the following advantages
cluster(node-phantom does not, due to how it works)
server.listen(0)works in cluster.
Your software should work without changes, but can show deprecation warning about outdated signatures. You need to update:
callbackto last position of arguments list.
npm install node-phantom-simple# Also need phantomjs OR slimerjs:npm install phantomjs# ORnpm install slimerjs
Note. SlimerJS is not headless and requires a windowing environment.
Under Linux/FreeBSD/OSX xvfb can be used to run headlessly.. For example, if you wish
to run SlimerJS on Travis-CI, add those lines to your
before_script:- export DISPLAY=:99.0- "sh -e /etc/init.d/xvfb start"
You can use it exactly like node-phantom, and the entire API of PhantomJS should work, with the exception that every method call takes a callback (always as the last parameter), instead of returning values.
For example, this is an adaptation of a web scraping example:
var driver = require'node-phantom-simple';drivercreate path: require'phantomjs'pathreturn browsercreatePagereturn pageopen""console.log"opened site? " status;pageincludeJs''// jQuery Loaded.// Wait for a bit for AJAX content to load on the page. Here, we are waiting 5 seconds.setTimeoutreturn pageevaluate//Get what you want from the page using jQuery. A good way is to populate an object with all the jQuery commands that you need and then return the object.var h2Arr =pArr = ;$'h2'each h2Arrpush$thishtml; ;$'p'each pArrpush$thishtml; ;returnh2: h2Arrp: pArr;console.logresult;browserexit;;5000;;;;;
options (not mandatory):
/CoreText/to suppress some common annoying font-related warnings.
drivercreate parameters: 'ignore-ssl-errors': 'yes' callback
will start phantom as:
You can rely on globally installed engines, but we recommend to pass path explicit:
drivercreate path: require'phantomjs'path callback// or for slimerdrivercreate path: require'slimerjs'path callback
You can also have a look at the test directory to see some examples of using the API, however the de-facto reference is the PhantomJS documentation. Just mentally substitute all return values for callbacks.
All of the
WebPage callbacks have been implemented including
and are set the same way as with the core phantomjs library:
console.log'Response (#' + responseid + ', stage "' + responsestage + '"): ' + JSONstringifyresponse;;
This includes the
onPageCreated callback which receives a new
pageget'content'console.log"Page HTML is: " + html;;pageset'zoomfactor' 0.25pagerender'capture.png';;// You can get/set nested values easy!pageset'settings.userAgent' 'PhAnToSlImEr' callback;
Engines are buggy. Here are some cases you should know.
.evaluatecan return corrupted result:
Made by Matt Sergeant for Hubdoc Inc.