2

node-phantomブリッジで自分のページを画面キャプチャするために phantomjs を使用しようとしました。ここに私がしようとしているものがあります:

 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();
                  });
                });
            });
          });

それは問題なく動作しますが、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);   
    });         
  });
4

3 に答える 3

0

有谷さんがおっしゃったように、時間が必要です。このライブラリには「onLoadFinished」イベントが発生する可能性があります(私が使用するノードライブラリにはあります)。この github の問題の下部にある私の例を見ることで、任意の待ち時間なしでこれを処理できます: https://github.com/amir20/phantomjs-node/issues/68

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);
        })
    });
});

}

于 2016-02-26T18:52:32.560 に答える