RESTful 无色实施

RESTful Chromless implementation

我正在寻找一种使用无头 chrome 的方法,类似于 chromeless 所做的,但不是作为 nodejs 端点实现,允许 restful 请求 [=16] =] 内容作为有效负载。

我想 运行 通过 API 网关触发 aws lambda 上的此服务。有没有人有这个用例的经验?

没有什么能阻止您在用例中使用 Chromeless。 Chromeless 可以在 AWS Lambda 函数中使用。您可以接收来自 AWS API 网关的 (RESTful) 请求,然后用它和 Chromeless 做一些事情。您可以以类似的方式组合 @serverless-chrome/lambda package with Chromeless to get headless Chrome running within Lambda so that Chrome is available to Chromeless. The Chromeless Proxy 作品。例如,您的 Lambda 函数的代码可能如下所示(这是我刚刚拼凑的未经测试的代码,但应该传达了这个想法):

const launchChrome = require('@serverless-chrome/lambda')
const Chromeless = require('chromeless').Chromeless

module.exports.handler = function handler (event, context, callback) {
  const body = JSON.parse(event.body) // event.body coming from API Gateway
  const url = body.url
  const evaluateJs = body.evaluateJs

  launchChrome({
    flags: ['--window-size=1280x1696', '--hide-scrollbars'],
  })
    .then((chrome) => {
      // Chrome is now running on localhost:9222

      const chromeless = new Chromeless({
        launchChrome: false,
      })

      chromeless
        .goto(url)
        .wait('body')
        .evaluate(() => `
          // this will be executed in headless chrome
          ${evaluateJs}
        `)
        .then((result) => {
          chromeless
            .end()
            .then(chrome.kill) // https://github.com/adieuadieu/serverless-chrome/issues/41#issuecomment-317989508
            .then(() => {
              callback(null, {
                statusCode: 200,
                body: JSON.stringify({ result })
              })
            })
        })
        .catch(callback)
    })
    .catch((error) => {
      // Chrome didn't launch correctly
      callback(error)
    })
}

您会在 Chromeless 问题跟踪器 here 上找到类似的帖子。

披露:我是这些软件包的 collaborator/author。