Node.js page.set(';content';)不';不要在phantomjs中处理动态内容

Node.js page.set(';content';)不';不要在phantomjs中处理动态内容,node.js,phantomjs,Node.js,Phantomjs,我试图用phantomjs用bridge截屏我的页面。以下是我正在尝试的: var phantom = require('node-phantom'); phantom.create(function (err, ph) { return ph.createPage(function (err, page) { return page.set('content', '<html><head></head>

我试图用phantomjs用bridge截屏我的页面。以下是我正在尝试的:

 var phantom = require('node-phantom');

 phantom.create(function (err, ph) {
            return ph.createPage(function (err, page) {
              return page.set('content', '<html><head></head><body><p>Hello</p></body></html>', function (err, status) {
                  return page.render('./content.png', function (err) {
                    ph.exit();
                  });
                });
            });
          });
var phantom=require('node-phantom');
幻影创建(功能(错误、ph){
返回ph.createPage(函数(错误,第页){
返回页面.set('content','Hello

',函数(err,status){ 返回page.render('./content.png',函数(err){ 博士退出(); }); }); }); });
这很好,但如果我试图设置包含javascript的内容,那就行不通了。请帮帮我,为什么它不起作用

编辑:这不起作用:

var phantom = require('node-phantom');

phantom.create(function (err, ph) {
   return ph.createPage(function (err, page) {
      page.open("about:blank", function(err,status) {
         page.evaluate(function() {        
            document.write('<html><head></head><body><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script><script>$(function(){document.write("Hello from jQuery")})</script></body>');
         });

         setTimeout(function () {
            return page.render('./content.png', function (err) {
                ph.exit();
             }); 
         }, 5000);   
    });         
  });
var phantom=require('node-phantom');
幻影创建(功能(错误、ph){
返回ph.createPage(函数(错误,第页){
页面打开(“关于:空白”,功能(错误,状态){
page.evaluate(函数(){
document.write('$(function(){document.write(“来自jQuery的你好”)}));
});
setTimeout(函数(){
返回page.render('./content.png',函数(err){
博士退出();
}); 
}, 5000);   
});         
});

我不知道为什么设置内容不起作用,这似乎是phantomjs api的一个限制。您可以使用document.write

var phantom = require('node-phantom');

phantom.create(function (err, ph) {
  return ph.createPage(function (err, page) {
    page.open("about:blank", function(err,status) {
      page.evaluate(function() {        
        document.write('<html><body><script>document.write("<h1>Hello From JS</h1>");</script><p>Hello from html</p></body></html>');
      });
      return page.render('./content.png', function (err) {
        ph.exit();
      });
    });
  });         
});
var phantom=require('node-phantom');
幻影创建(功能(错误、ph){
返回ph.createPage(函数(错误,第页){
页面打开(“关于:空白”,功能(错误,状态){
page.evaluate(函数(){
document.write('document.write(“Hello From JS”);Hello From html

); }); 返回page.render('./content.png',函数(err){ 博士退出(); }); }); }); });
JavaScript代码需要一些时间来执行。请尝试在设置页面内容和调用
render

之间有一个延迟,正如ariya提到的,需要时间。此库可能有一个“onLoadFinished”事件(我使用的节点库有)。您可以通过查看github问题底部的示例来处理此问题,而无需任意等待时间:


}

不幸的是,它不起作用。page.evaluate无法执行。
Document.prototype.captureScreenshot = function(next) {
console.log(">> Rendering screencap for " + this.id)
var self = this;
phantom.create(function(ph) {
    ph.createPage(function(page) {
        page.setContent(self.html);
        page.set("viewportSize", {
            width: 1920,
            height: 1080
        });
        page.set('onLoadFinished', function(success) {
            var outputFile = './screenshots/screenshot-' + self.id + '.png';
            page.render(outputFile);
            ph.exit();
            console.log(">> Render complete for " + self.id)
            if (next)
                next(outputFile);
        })
    });
});