javascript – 可以phantomjs使用node.js?

我想在我的node.js脚本中使用phantomjs。有一个phantomjs-node图书馆..但不幸的是,作者使用这个奇怪的咖啡脚本代码来解释他在做什么:

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 is ' + result
        ph.exit()

现在如果我直接使用phantomjs与javascript,它会看起来像this

var page = require('webpage').create();
page.open(url, function (status) {
    var title = page.evaluate(function () {
        return document.title;
    });
    console.log('Page title is ' + title);
});

所以基本上我试图写上等同的代码上面的第一段代码在正常的javascript(通过阅读咖啡脚本documentation ..这是我做的:

// 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 is ' + title);              
        });
    });
    ph.exit();
});

不幸的是它不工作!如果我跑

node phantomTest.js

在shell上,没有发生什么返回和进程不停止..任何想法?

更新:

我只是在phantomjs faq读这个:

Q: Why is PhantomJS not written as Node.js module?

A: The short answer: “No one can serve two masters.”

A longer explanation is as follows.

As of now, it is technically very challenging to do so.

Every Node.js module is essentially “a slave” to the core of Node.js,
i.e. “the master”. In its current state, PhantomJS (and its included
WebKit) needs to have the full control (in a synchronous matter) over
everything: event loop, network stack, and JavaScript execution.

If the intention is just about using PhantomJS right from a script
running within Node.js, such a “loose binding” can be achieved by
launching a PhantomJS process and interact with it.

这可能与它有关系吗?但那么整个图书馆就没有意义!

更新2:

我发现这个代码在web做同样的事情:

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 is ' + result);
        return ph.exit();
      });
    });
  });
});

不幸的是,这不是工作任何..相同的结果!

phantomjs-node不是phantomjs的官方支持的npm包。相反,它通过创建一个使用websockets作为节点和幻像之间的IPC通道的Web服务器,在节点和幻象之间实现一个“恶心的聪明的桥”。 I’m not making this up

So we communicate with PhantomJS by spinning up an instance of ExpressJS, opening Phantom in a subprocess, and pointing it at a special webpage that turns socket.io messages into alert() calls. Those alert() calls are picked up by Phantom and there you go!

所以我不会感到惊讶,如果phantomjs-node工作,不工作,失败沉默,或失败壮观。也不会期望除了phantomjs-node的作者之外的任何人都能够对phantomjs-node进行故障排除。

你原来问题的答案是从phantomjs faq的答案:No.幻影和节点有不可调和的差异。两者都期望对诸如事件循环,网络栈和JS执行的基本低级功能具有完全控制,因此它们不能在同一进程内协作。

http://stackoverflow.com/questions/15745394/can-phantomjs-work-with-node-js

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:javascript – 可以phantomjs使用node.js?