0
我正在寻找一种方法来使用类似于chromeless所做的headless chrome,但不是作为nodejs端点实现,而是允许将html内容作为有效负载的平稳请求。RESTful Chromless implementation
我想通过API网关触发aws lambda运行此服务。有没有人有这个用例的经验?
我正在寻找一种方法来使用类似于chromeless所做的headless chrome,但不是作为nodejs端点实现,而是允许将html内容作为有效负载的平稳请求。RESTful Chromless implementation
我想通过API网关触发aws lambda运行此服务。有没有人有这个用例的经验?
在你的使用案例中,没有什么能够阻止你使用Chromeless。可以在AWS Lambda函数中使用Chromeless。您可以接收来自AWS API Gateway的(RESTful)请求,然后使用它和Chromeless进行操作。您可以将@serverless-chrome/lambda包与Chromeless结合使用,以便在Lambda中运行无头Chrome浏览器,以便Chrome可用于Chromeless。 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)
})
}
你会发现无边框问题跟踪here类似的线程。
披露:我是这些软件包的合作者/作者。
欢迎来到Stack Overflow。你会发现“有人有......吗?”在这里通常不被认为是一个[好问题](https://stackoverflow.com/help/how-to-ask)。在这里,一个很好的问题是试图理解和解决特定的编程问题。例如,在这里,我建议你需要尝试你想完成的任务,并且当你手头有代码时提出问题,并且提供一个最小化,完整,可验证的例子(MCVE),这个例子不能像预期的那样工作。 –
Chromeless看起来应该很容易适应您的使用情况,方法是从请求主体中提取HTML,将其保存到文件或S3存储桶,然后以编程方式导航到它。 –