Skip to content

Instantly share code, notes, and snippets.

@hashaam
Created January 16, 2019 05:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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")
})
@coeneivan
Copy link

Wow, that was easy!
Thanks!
Any ideas on how I can get the url of this file?

@hashaamchaudhry
Copy link

Thanks @coeneivan for the nice comment. Surely, you can get the Public File URL after line 19, try the following code:

const publicUrl = file.publicUrl() // to get the public url of the file

For more information, refer to the following URL:

https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/file#_google_cloud_storage_File_publicUrl_member_1_

@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