[英]Getting the Nightmare.JS (v 2.9.1) screenshot buffer
我正在嘗試使用以下代碼對網頁進行截圖。 稍作修改,我就可以將屏幕截圖另存為PNG。 但是,nightmare.JS文檔指示.screenshot()方法將返回“圖像數據的緩沖區”。 如果未提供“路徑”選項。
截圖完成執行后,如何訪問緩沖區?
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');
})
});
}
您幾乎就在那里:在最終的.then()
, e
應該是PNG的完整緩沖區。 要驗證,您可以稍微更改.then()
函數:
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');
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.