可以在 Google Cloud 功能中运行 Headless Chrome/Chromium 吗? [英] Possible to run Headless Chrome/Chromium in a Google Cloud Function?

查看:19
本文介绍了可以在 Google Cloud 功能中运行 Headless Chrome/Chromium 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以运行 Headless Chrome/Chromium在谷歌云功能中?我知道我可以在 GCF 中包含和运行静态编译的二进制文件.我能否获得适用于此的 Chrome 的静态编译版本?

Is there any way to run Headless Chrome/Chromium in a Google Cloud Function? I understand I can include and run statically compiled binaries in GCF. Can I get a statically compiled version of Chrome that would work for this?

推荐答案

Google Cloud Functions 的 Node.js 8 运行时现在包含运行 Headless Chrome 所需的所有操作系统包.

The Node.js 8 runtime for Google Cloud Functions now includes all the necessary OS packages to run Headless Chrome.

这是一个返回屏幕截图的 HTTP 函数的代码示例:

Here is a code sample of an HTTP function that returns screenshots:

index.js文件:

const puppeteer = require('puppeteer');

exports.screenshot = async (req, res) => {
  const url = req.query.url;

  if (!url) {
    return res.send('Please provide URL as GET parameter, for example: <a href="?url=https://example.com">?url=https://example.com</a>');
  }

  const browser = await puppeteer.launch({
    args: ['--no-sandbox']
  });
  const page = await browser.newPage();
  await page.goto(url);
  const imageBuffer = await page.screenshot();
  await browser.close();

  res.set('Content-Type', 'image/png');
  res.send(imageBuffer);
}

package.json

{
  "name": "screenshot",
  "version": "0.0.1",
  "dependencies": {
    "puppeteer": "^1.6.2"
  }
}

这篇关于可以在 Google Cloud 功能中运行 Headless Chrome/Chromium 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆