Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/netlify-labs/netlify-functions-headless-chrome
Run headless chrome in Netlify functions
https://github.com/netlify-labs/netlify-functions-headless-chrome
Last synced: 2 months ago
JSON representation
Run headless chrome in Netlify functions
- Host: GitHub
- URL: https://github.com/netlify-labs/netlify-functions-headless-chrome
- Owner: netlify-labs
- Archived: true
- Created: 2018-12-12T05:17:12.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-08T02:11:25.000Z (over 4 years ago)
- Last Synced: 2024-08-03T11:09:32.060Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 522 KB
- Stars: 51
- Watchers: 8
- Forks: 33
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Netlify functions + Headless Chrome
> Run headless chrome in netlify lambda functions
## How
Add a [Netlify function](https://www.netlify.com/products/functions/) with the following code!
```js
const chromium = require('chrome-aws-lambda')
const puppeteer = require('puppeteer-core')exports.handler = async (event, context, callback) => {
let theTitle = null
let browser = null
console.log('spawning chrome headless')
try {
const executablePath = await chromium.executablePath// setup
browser = await puppeteer.launch({
args: chromium.args,
executablePath: executablePath,
headless: chromium.headless,
})// Do stuff with headless chrome
const page = await browser.newPage()
const targetUrl = 'https://davidwells.io'// Goto page and then do stuff
await page.goto(targetUrl, {
waitUntil: ["domcontentloaded", "networkidle0"]
})await page.waitForSelector('#phenomic')
theTitle = await page.title();
console.log('done on page', theTitle)
} catch (error) {
console.log('error', error)
return callback(null, {
statusCode: 500,
body: JSON.stringify({
error: error
})
})
} finally {
// close browser
if (browser !== null) {
await browser.close()
}
}return callback(null, {
statusCode: 200,
body: JSON.stringify({
title: theTitle,
})
})
}
```