node-webkit-screenshot icon indicating copy to clipboard operation
node-webkit-screenshot copied to clipboard

Application hangs after multiple screenshot calls

Open MonsieurBlutbad opened this issue 9 years ago • 1 comments

I want to screenshot multiple urls in sequence which I provide as an array, but after a couple of successful iterations the application hangs. The amount of iterations before this happens depends on the page that is being opened. Also some pages (like google.com) don't seem to cause this problem at all. Typically it is between 3 and 20 iterations. From what I can say gui.window.open is getting called but it doesn't load the url for some reason (I don't get any error messages or feedback at all) and so the application seems to be hung waiting for the load event which doesn't get fired.

'use strict';

var urls = [];

for(var i = 0; i < 100; i++) {
    urls.push({
        source: 'https://github.com/FWeinb/node-webkit-screenshot',
        target: 'filename' + i
    });
}

var fs = require('fs');
var path = require('path');
var screenshot = require('node-webkit-screenshot');

var getUserHome = function() {
    return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
};

var targetDirectory =  getUserHome() + path.sep + 'test' + path.sep;

var sequence = Promise.resolve();

urls.forEach(
    function(url) {
        sequence = sequence
            .then(
                function() {
                    console.log('screenshot ' + url.source);
                    return screenshot({
                        url: url.source,
                        width: 1366,
                        height: 768,
                        delay: 1
                    })
                }
            )
            .then(
                function (buffer) {
                    console.log('save as ' + targetDirectory  + url.target + '.png');
                    fs.writeFile(
                        targetDirectory  + url.target + '.png',
                        buffer
                    );
                }
            )
            .catch( function(error) { console.log(error) });
    }
);

MonsieurBlutbad avatar Feb 24 '16 13:02 MonsieurBlutbad

Add screenshot.close(); after your fs.writeFile() call. That fixed it for me.

KerryRitter avatar Sep 29 '16 01:09 KerryRitter