-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (38 loc) · 1.35 KB
/
index.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
const axios = require('axios');
const cheerio = require('cheerio');
async function fetchHeadTags(url) {
try {
const { data } = await axios.get(url);
const $ = cheerio.load(data);
const metaTags = {};
const linkTags = {};
// Extract meta tags
$('meta').each((i, element) => {
const name = $(element).attr('name') || $(element).attr('property');
if (name) {
metaTags[name] = $(element).attr('content');
}
});
// Extract link tags
$('link').each((j, elements) => {
const rel = $(elements).attr('rel');
const href = $(elements).attr('href');
if (rel) {
linkTags[rel] = href || '';
}
// Check for favicon in other attributes
if (rel && rel.toLowerCase() === 'icon' && !linkTags.icon && href) {
linkTags.icon = href;
} else if (!linkTags.icon && href && href.toLowerCase().includes('favicon')) {
linkTags.icon = href;
}
});
return { metaTags, linkTags };
} catch (error) {
throw new Error(error.message);
}
}
// Export the fetchHeadTags function CommonJS
module.exports = fetchHeadTags;
// Export the fetchHeadTags function ES6 module
module.exports.default = fetchHeadTags;