I'm trying to take a screenshot of a webpage with the code below. With some slight modification, I can save the screenshot as a PNG. However, nightmare.JS docs indicate the .screenshot() method will return "a Buffer of the image data." if a "path" option is not provided.
How can I get access to the buffer once the screenshot has completed execution?
const getScreenshot = (url) => {
nightmare
.goto(url)
.wait()
.evaluate(() => {
const body = document.querySelector('body');
return {
height: body.scrollHeight,
width: body.scrollWidth
};
})
.then(function(dimensions) {
return nightmare
.viewport(dimensions.width, dimensions.height)
.wait(1000)
// .screenshot(require('path').join(__dirname, 'screenshot.png'))
.screenshot()
})
.then(function(e) {
console.log('nightmare', nightmare)
nightmare.end()
.then(function(result) {
console.log('completed screenshot', result)
console.log('done');
})
});
}
You are almost there: in your final .then()
, e
should be the completed buffer for the PNG. To verify, you can change the .then()
function slightly:
function(e) {
//this should print "e is a buffer: true"
console.log(`e is a buffer: ${Buffer.isBuffer(e)}`);
return nightmare.end()
.then(function(result) {
console.log('completed screenshot', result)
console.log('done');
});
}