javascript - can phantomjs work with node.js? -
i use phantomjs in node.js script. there phantomjs-node library.. unfortunately author used weird coffee script code explain he's doing:
phantom = require 'phantom' phantom.create (ph) -> ph.createpage (page) -> page.open "http://www.google.com", (status) -> console.log "opened google? ", status page.evaluate (-> document.title), (result) -> console.log 'page title ' + result ph.exit()
now if use phantomjs directly javascript, this:
var page = require('webpage').create(); page.open(url, function (status) { var title = page.evaluate(function () { return document.title; }); console.log('page title ' + title); });
so i'm trying write equivalent of first snippet of code above in normal javascript (by reading coffee script documentation.. did:
// file name: phantomtest.js var phantom = require('phantom'); phantom.create(function(ph) { ph.createpage(function(page) { page.open('http://www.google.com', function(status) { console.log('opened google?', status); var title = page.evaluate(function() { return document.title; }); console.log('page title ' + title); }); }); ph.exit(); });
unfortunately it's not working! if run
node phantomtest.js
on shell, nothing happens.. nothing returns , process doesn't stop.. ideas?
update:
i read in phantomjs faq:
q: why phantomjs not written node.js module?
a: short answer: "no 1 can serve 2 masters."
a longer explanation follows.
as of now, technically challenging so.
every node.js module "a slave" core of node.js, i.e. "the master". in current state, phantomjs (and included webkit) needs have full control (in synchronous matter) on everything: event loop, network stack, , javascript execution.
if intention using phantomjs right script running within node.js, such "loose binding" can achieved launching phantomjs process , interact it.
mmm.. have it? whole library wouldn't make sense!
update 2:
i found code in web same thing:
var phantom = require('phantom'); phantom.create(function(ph) { return ph.createpage(function(page) { return page.open("http://www.google.com", function(status) { console.log("opened google? ", status); return page.evaluate((function() { return document.title; }), function(result) { console.log('page title ' + result); return ph.exit(); }); }); }); });
unfortunately that's not working either.. same result!
phantomjs-node isn't official supported npm package phantomjs. instead, implements "nauseously clever bridge" between node , phantom creating web server uses websockets serve ipc channel between node , phantom. i'm not making up:
so communicate phantomjs spinning instance of expressjs, opening phantom in subprocess, , pointing @ special webpage turns socket.io messages alert() calls. alert() calls picked phantom , there go!
so wouldn't surprised if phantomjs-node works, doesn't work, fails silently, or fails spectacularly. nor expect other author of phantomjs-node able troubleshoot phantomjs-node.
the answer original question answer phantomjs faq: no. phantom , node have irreconcilable differences. both expect have complete control on fundamental low-level functionality event loop, network stack, , js execution can't cooperate within same process.
Comments
Post a Comment