Created
January 16, 2019 05:52
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
}) |
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
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_