首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以使用chromeless API访问document.body.scrollHeight?

是否可以使用chromeless API访问document.body.scrollHeight?
EN

Stack Overflow用户
提问于 2017-10-20 08:55:27
回答 1查看 194关注 0票数 0

我想使用chromless api捕获一个完整的页面截图。(整页显示为折叠下的所有内容,而不仅仅是当前视口。

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

这是我目前所拥有的,我正在他们的demo site上测试

代码语言:javascript
复制
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的一个限制?可以从无头web浏览器访问document对象吗?

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

EN

回答 1

Stack Overflow用户

发布于 2017-10-21 18:22:37

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

代码语言:javascript
复制
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,然后我们可以使用该变量来设置视口的高度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46840929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档