Reuse electron session with nightmare

守給你的承諾、 提交于 2019-12-25 11:53:11

问题


I am using Nightmare.js to print pdf's. I send a request to the node server and build the page, using Nightmare to ensure the page has loaded, then print a pdf. But for each request I create a new electron window, how do I reuse the same window, or a pool of max X electron windows, to handle my pdf printing?

var nightmare = require('nightmare'),
    http = require('http');

function createPage(o, final) {

    var page = nightmare()
    .goto('file:\\\\' + __dirname + '\\index.html');
    .wait(function () {
        return !!(window.App && App.app); //Check Javascript has loaded
    })

    page.evaluate(function (template, form, lists, printOptions) {
        App.pdf.Builder.create({
            //args for building pages
        });
    }, o.template, o.form, o.lists, o.printOptions);

    page.wait(function () {
        return App.pdf.Builder.ready;
    })
    .pdf(form.filename, { "pageSize": "A4", "marginsType": 1 })
    .end()
    .then(function () {
        console.log('Pdf printed');
        final(true);
    })
    .catch(function (err) {
        console.log('Print Error: ' + err.message);
    });
}

http.createServer(function (request, response) {
    var body = [];
    request.on('data', function (chunk) {
        body.push(chunk);
    }).on('end', function () {
        body = Buffer.concat(body).toString();
        var json = JSON.parse(body);
        createPage(json, function (status) {
            if (status === true) {
                response.writeHead(200, { 'Content-Length': 0 });
            } else {
                response.writeHead(500, { 'Content-Type': 'text/html' });
                response.write(' ' + status);
                console.log('status error: ' + status);
            }
            response.end('End of Request \n'); //return status msg, if any
        });
    });
}).listen(8007);

I am aware of all the concurrency issues that might arise by potentially using the same electron window again before a previous print has finished, so I would like the answer to make it clear how that is avoided.


回答1:


You need to create the nightmare instance once and not in a loop.

var page = nightmare()
    .goto('file:\\\\' + __dirname + '\\index.html');

This creates new nightmare instance each time you create a page. You can create instance once

const Nightmare = require('nightmare');
const browser = Nightmare();

Then use it each time with browser.goto(url). You can chain your goto statment using answers given at: https://github.com/segmentio/nightmare/issues/708

Extract from one of the answers:

function run() {
  var nightmare = Nightmare();
  yield nightmare
    .goto('https://www.example.com/signin')
    .type('#login', 'username')
    .type('#password', 'password')
    .click('#btn')

  for (var i = 0; i < 4; i++) {
    yield nightmare
      .goto('https://www.example.com/page'+i)
      .wait(1000)
      .evaluate(function(){
        return $('#result > h3').text()
      })
  }

  yield nightmare.end()
}

You can also create multiple browsers, for pooling as well.

var browser1 = Nightmare();
var browser2 = Nightmare();


来源:https://stackoverflow.com/questions/43889284/reuse-electron-session-with-nightmare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!