Skip to content

Instantly share code, notes, and snippets.

@hashaam
Created January 16, 2019 05:52
Show Gist options
  • Save hashaam/357ad0460dee25b3e729e3aa0f622ed8 to your computer and use it in GitHub Desktop.
Save hashaam/357ad0460dee25b3e729e3aa0f622ed8 to your computer and use it in GitHub Desktop.
This firebase cloud function takes screenshot of google home page and saves in firebase storage bucket under screenshots/google.png, every time it is run.
import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";
import * as puppeteer from "puppeteer";
admin.initializeApp()
export const takeGoogleScreenshot = functions
.runWith({ memory: "1GB" })
.https.onRequest(async (request, response) => {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
})
const page = await browser.newPage()
await page.goto("https://www.google.com", {waitUntil: 'networkidle2'})
const bucket = admin.storage().bucket("screenshots")
const file = bucket.file("google.png")
const screenshotBuffer = await page.screenshot({ fullPage: true })
await file.save(screenshotBuffer)
await browser.close()
response.send("done")
})
@greggyc
Copy link

greggyc commented Oct 18, 2022

This is just what I needed for my app thank you. I have modified it to run on a firestore cloud function trigger but I am hitting an error in the logs when the cloud function executes:

"Exception from a finished function: Error: Could not find expected browser (chrome) locally. Run npm install to download the correct Chromium revision (1045629).

Adding chromuim in the dependencies doesn't fix it unfortunately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment