sudo apt-get install phantomjs
brew install phantomjs
page.indectJs
) scripts into a page only along the full path on the file system.page.includeJs
) scripts per page for the full URL, but in the module the page API of the internal API page.includeJs is corrupted due to implementation features..
.
// ( package.json) var phantom = require("node-phantom") // , vow = require("vow") // - , cfg = require("../config") // , fs = require('fs') // , pi; // exports.init = function () { if (pi) { pi.exit(); } phantom.create(function (err, instance) { pi = instance; }); } // - exports.render = function (dataset, opts) { var promise = vow.promise(); // pi.createPage(function (err, page) { // , page.set("viewportSize", opts.viewport); // d3 (. " ") var d3Path = __dirname + "/../client/scripts/vendor/d3.v3.js"; // , d3 // type - (line, bar, pie) // chart.xxx.js var chartJs = __dirname + "/../client/scripts/chart." + opts.type + ".js"; // var chartCss = __dirname + "/../client/styles/charts.css"; var innerStyle = ""; // // ? ? injectLib_(page, d3Path)() .then(injectLib_(page, chartJs)) .then(readCssStyles_(chartCss)) .then(drawChart_(page, {dataset: dataset, innerCss: innerStyle}, opts)) .then(function (res) { // , promise.fulfill({filename: res.filename}); }) .fail(function (err) { promise.reject(err) } ) }); return promise; } // () // - " " function readCssStyles_(chartCss) { return function(){ var prom = vow.promise(); fs.readFile(chartCss, 'utf8', function (err,innerCss) { if (err) { console.log(chartCss + ": read failed, err: " + err); prom.reject(chartCss + ": read failed, err: " + err); } else { console.log(chartCss + " read"); prom.fulfill(innerCss); } }); return prom; } } function injectLib_(page, path) { return function () { var prom = vow.promise(); // , page.evaluate page.injectJs(path, function (err) { if (err) { console.log(path + " injection failed") prom.reject(path + " injection failed"); } else { console.log(path + " injected") prom.fulfill(); } }); return prom; } } function drawChart_(page, data, opts) { return function (innerCss) { data.innerCss = innerCss; var prom = vow.promise(); // // - "". // , , page.evaluate(function (data) { // // data = JSON.parse(data); // // charts.xxx.js charts.line("body",data.dataset); // , var style = document.createElement("style"); style.innerHTML = data.innerCss; document.getElementsByTagName("head")[0].appendChild(style); } , function (err, result) { if (err) { prom.reject("phantomjs evaluation failed : " + err) } // // png, pdf, gif jpeg var filename = cfg.server.chartsPath + '/' + opts.type + "_" + Date.now() + ".png"; var savingPath = "client" + filename; // page.render(savingPath, function (err, res) { console.log("Saving image: " + filename); page.close(); prom.fulfill({filename: filename}); }); }, JSON.stringify(data)); return prom; } }
Source: https://habr.com/ru/post/180927/
All Articles