Migrate from jsdom to phantomJS? (basic DOM creation)

M. Bostok noted that jsdom nodejs has incomplete svg support, and, which is important for me, it does not getBBox(). In addition, he advised switching to PhantomJS node with nodejs. I checked, but this approach is new to me.

My nodejs + jsdom script creates a virtual DOM with which my d3js play and follow:

var jsdom = require('jsdom');
jsdom.env(                             // creates virtual page
  "<html><body></body></html>",        // create my DOM hook,
  [ 'http://d3js.org/d3.v3.min.js',    // add my online dependencies ...
  '../js/d3.v3.min.js',                // ... & local ones
  '../js/jquery-2.1.3.min.js'],

  function (err, window) {
           //my normal JS or nodejs code here !
  }
);

How to migrate this nodejs + jsdom to nodejs + phantomjs?

+4
source share
1 answer

Since you want to do this from node.js, you should use a PhantomJS bridge, for example phantomjs-node (phantom npm module).

PhantomJS, : '--local-to-remote-url-access = yes' PhantomJS, . , --web-security=false, --ssl-protocol=any ignore-ssl-errors=true .

DOM, injectJs() includeJs() . , DOM PhantomJS, . (page.evaluate()) , , , .

var phantom = require('phantom');
var async = require('async');

function run(page, ph) {
    page.evaluate(function () {
        // page context: DOM code here
        return document.title;
    }, function (title) {
        // node code here
        console.log('Page title is ' + title);
        ph.exit();
    });
}

var remoteScripts = [ "http://d3js.org/d3.v3.min.js" ];
var localScripts = [ "../js/d3.v3.min.js", "../js/jquery-2.1.3.min.js" ];
phantom.create('--local-to-remote-url-access=yes', '--web-security=false', function (ph) {
    ph.createPage(function (page) {
        async.series(remoteScripts.map(function(url){
            return function(next){
                page.includeJs(url, function(){
                    next();
                });
            };
        }), function(){
            async.series(localScripts.map(function(url){
                return function(next){
                    page.injectJs(url, function(){
                        next();
                    });
                };
            }), function(){
                run(page, ph);
            });
        });
    });
});

async script DOM. series().

+3

Source: https://habr.com/ru/post/1583740/


All Articles