-
Notifications
You must be signed in to change notification settings - Fork 9
/
meta.js
41 lines (35 loc) · 1.34 KB
/
meta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const puppeteer = require("puppeteer-core");
const chrome = require("chrome-aws-lambda");
module.exports = async (req, res) => {
try {
const url = req.query.url;
const browser = await puppeteer.launch({
args: [...chrome.args, "--hide-scrollbars", "--disable-web-security"],
defaultViewport: chrome.defaultViewport,
executablePath: await chrome.executablePath,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
await page.goto(url);
const metaData = await page.evaluate(() => {
const data = {};
const metaTags = document.querySelectorAll("meta");
metaTags.forEach((tag, i) => {
const key = tag.getAttribute('name') ? tag.getAttribute('name') : (tag.getAttribute('property') ? tag.getAttribute('property') : (tag.getAttribute('http-equiv') ? tag.getAttribute('http-equiv') : tag.getAttribute('itemprop')));
tag.getAttribute('charset') ? data["charset"] = tag.getAttribute('charset') : data[key == null ? i : key] = tag.getAttribute('content');
});
return data;
});
await browser.close();
res.statusCode = 200;
res.setHeader("Content-Type", `application/json`);
res.end(JSON.stringify(metaData));
} catch (err) {
console.log(err);
res.statusCode = 500;
res.json({
error: err.toString(),
});
res.end();
}
};