0

我想使用 chromless api 捕获整页屏幕截图。(整页与首屏下方的所有内容一样,而不仅仅是当前视口。

为了做到这一点,我认为可以计算网页的高度(使用document.body.scrollHeight),并将视口高度设置为该值。

这是我目前拥有的,我正在他们的演示网站上进行测试:

const chromeless = new Chromeless({ remote: true })

const screenshot = await chromeless
  .goto('https://www.graph.cool')
  .setViewport({width: 1024, height: 800, scale: 1})
  .evaluate(() => {
    const height = document.body.scrollHeight;
    return height
    })
  .setViewport({width: 1024, height: height, scale: 1})
  .screenshot()

console.log(screenshot)

await chromeless.end()

我认为(希望)我的 javascript 没问题,也许这是 API 的限制?document是否可以从无头 Web 浏览器访问该对象?

以下是评估文档以供参考: https ://github.com/graphcool/chromeless/blob/master/docs/api.md#api-evaluate

4

1 回答 1

1

可以通过像这样抓取文档的高度来截取整个页面(演示):

const chromeless = new Chromeless({ remote: true })

const scrollHeight = await chromeless
  .goto('https://www.graph.cool')
  .evaluate(() => document.body.scrollHeight)

console.log(`The document's height is ${scrollHeight}px`)

const screenshot = await chromeless
  .setViewport({width: 1024, height: scrollHeight, scale: 1})
  .screenshot()

console.log(screenshot)

await chromeless.end()

需要注意的主要一点是,我们必须从evaluate()调用中返回高度并将其分配给一个变量scrollHeight,然后我们可以使用该变量来设置视口的高度。

于 2017-10-21T10:22:36.643 回答