我正在尝试抓取JavaScript日历,并在Google Cloud Functions(Blaze计划)中返回其事件的JSON数组。以下功能有效,但仅在通过Firebase模拟器在本地运行时有效。它部署成功,但是每个调用都会导致超时。没有在日志或任何东西中引发任何错误。在Node.js 10上运行的本地功能和已部署功能。( EDIT :我发现this文章提到xvfb是使用不显示的噩梦所必需的,但是我不确定如何会将其添加到Firebase,甚至安装它)
const functions = require('firebase-functions');
const Nightmare = require('nightmare'); //latest version
const retrieveEventsOpts = { memory: "2GB", timeoutSeconds: 60 };
exports.retrieveEventsArray = functions.runWith(retrieveEventsOpts).https.onRequest(async (request, response) => {
nightmare = Nightmare({show: false})
try {
await nightmare
.goto('https://www.csbcsaints.org/calendar')
.evaluate(() => document.querySelector('body').innerHTML )
.then(firstResponse => {
let responseJSON = parseHTMLForEvents(firstResponse) //Just a function that synchronously parses the HTML string to a JSON array
return response.status(200).json(responseJSON)
}).catch(error => {
return response.status(500).json(error)
})
} catch(error) {
return response.status(500).json(error)
}
})