Skip to content

Commit a0d7034

Browse files
install playwright
1 parent db7c59e commit a0d7034

File tree

3 files changed

+455
-3
lines changed

3 files changed

+455
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1-
exports.handler = async function(event, context) {
1+
const playwright = require("playwright-aws-lambda");
2+
3+
exports.handler = async function(event, ctx) {
4+
const browser = await playwright.launchChromium();
5+
const context = await browser._defaultContext;
6+
const page = await context.newPage();
7+
await page.setContent(`<!DOCTYPE html>
8+
<html>
9+
<head>
10+
<meta charset="utf-8" />
11+
</head>
12+
13+
<body>
14+
<div id="corgi">hello</div>
15+
</body>
16+
</html>
17+
`);
18+
const boundingRect = await page.evaluate(() => {
19+
const corgi = document.getElementById("corgi");
20+
const { x, y, width, height } = corgi.children[0].getBoundingClientRect();
21+
return { x, y, width, height };
22+
});
23+
24+
const screenshotBuffer = await page.screenshot({ clip: boundingRect });
25+
await browser.close();
226
return {
27+
isBase64Encoded: true,
328
statusCode: 200,
4-
body: "gen function works"
29+
headers: {
30+
"Content-Type": "image/png",
31+
"Content-Length": screenshotBuffer.length.toString()
32+
},
33+
body: screenshotBuffer.toString("base64")
534
};
635
};

functions/gen-opengraph-image/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"author": "christopherbiscardi <[email protected]> (@chrisbiscardi)",
6-
"license": "MIT"
6+
"license": "MIT",
7+
"dependencies": {
8+
"playwright-aws-lambda": "^0.4.0",
9+
"playwright-core": "^0.11.1"
10+
}
711
}

0 commit comments

Comments
 (0)