【问题标题】:Reuse electron session with nightmare在噩梦中重用电子会话
【发布时间】:2017-10-08 21:55:56
【问题描述】:

我正在使用 Nightmare.js 打印 pdf。 我向节点服务器发送请求并构建页面,使用 Nightmare 确保页面已加载,然后打印 pdf。 但是对于每个请求,我都会创建一个新的电子窗口,我如何重用同一个窗口或最大 X 个电子窗口池来处理我的 pdf 打印?

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

我知道在之前的打印完成之前可能再次使用相同的电子窗口可能会出现所有并发问题,所以我希望答案能说明如何避免这种情况。

【问题讨论】:

    标签: javascript performance pdf-generation nightmare


    【解决方案1】:

    你需要创建一次噩梦实例,而不是循环。

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

    这会在您每次创建页面时创建新的噩梦实例。 您可以创建一次实例

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

    然后每次使用 browser.goto(url)。您可以使用以下地址给出的答案链接您的 goto 语句:https://github.com/segmentio/nightmare/issues/708

    摘自其中一个答案:

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

    您还可以创建多个浏览器,也可以用于池化。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2020-03-14
      • 2010-11-13
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多