-
Notifications
You must be signed in to change notification settings - Fork 14
/
todoMvcMetaData.js
50 lines (43 loc) · 1.35 KB
/
todoMvcMetaData.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
42
43
44
45
46
47
48
49
50
const { chromium } = require('playwright');
const scrapeMetatags = async () => {
const defaultUrl = 'http://todomvc.com';
const [, , url = defaultUrl] = process.argv;
const isValidUrl = /^(ftp|http|https):\/\/[^ "]+$/.test(url);
let seoObj;
if (!isValidUrl) {
console.error('please pass valid URL as argument');
return;
}
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
page.on('pageerror', console.log);
await page.goto(url, {
waitUntil: 'networkidle',
});
try {
seoObj = await page.evaluate(() => {
const getMetatag = (name) => {
const $metaName =
document.querySelector(`meta[name=${name}]`) ||
document.querySelector(`meta[name="og:${name}"]`) ||
document.querySelector(`meta[name="twitter:${name}"]`);
return $metaName.getAttribute('content');
};
return {
title: document.querySelector('title').innerText,
favicon: document
.querySelector('link[rel="shortcut icon"]')
.getAttribute('href'),
description: getMetatag('description'),
// image: getMetatag('image'),
// author: getMetatag('author'),
};
});
} catch (error) {
console.log(error);
}
console.log(seoObj);
await browser.close();
};
scrapeMetatags();