forked from AndrejGajdos/link-preview-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (170 loc) · 5.42 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use strict";
const puppeteer = require("puppeteer-extra");
const pluginStealth = require("puppeteer-extra-plugin-stealth");
const util = require("util");
const request = util.promisify(require("request"));
const getUrls = require("get-urls");
const urlImageIsAccessible = async (url) => {
const correctedUrls = getUrls(url);
if (correctedUrls.size !== 0) {
const urlResponse = await request(correctedUrls.values().next().value);
const contentType = urlResponse.headers["content-type"];
return new RegExp("image/*").test(contentType);
}
};
const getImg = async (page, uri) => {
const img = await page.evaluate(async () => {
const ogImg = document.querySelector('meta[property="og:image"]');
if (
ogImg != null &&
ogImg.content.length > 0 &&
(await urlImageIsAccessible(ogImg.content))
) {
return ogImg.content;
}
const imgRelLink = document.querySelector('link[rel="image_src"]');
if (
imgRelLink != null &&
imgRelLink.href.length > 0 &&
(await urlImageIsAccessible(imgRelLink.href))
) {
return imgRelLink.href;
}
const twitterImg = document.querySelector('meta[name="twitter:image"]');
if (
twitterImg != null &&
twitterImg.content.length > 0 &&
(await urlImageIsAccessible(twitterImg.content))
) {
return twitterImg.content;
}
let imgs = Array.from(document.getElementsByTagName("img"));
if (imgs.length > 0) {
imgs = imgs.filter((img) => {
let addImg = true;
if (img.naturalWidth > img.naturalHeight) {
if (img.naturalWidth / img.naturalHeight > 3) {
addImg = false;
}
} else {
if (img.naturalHeight / img.naturalWidth > 3) {
addImg = false;
}
}
if (img.naturalHeight <= 50 || img.naturalWidth <= 50) {
addImg = false;
}
return addImg;
});
if (imgs.length > 0) {
imgs.forEach((img) =>
img.src.indexOf("//") === -1
? (img.src = `${new URL(uri).origin}/${src}`)
: img.src
);
return imgs[0].src;
}
}
return null;
});
return img;
};
const getTitle = async (page) => {
const title = await page.evaluate(() => {
const ogTitle = document.querySelector('meta[property="og:title"]');
if (ogTitle != null && ogTitle.content.length > 0) {
return ogTitle.content;
}
const twitterTitle = document.querySelector('meta[name="twitter:title"]');
if (twitterTitle != null && twitterTitle.content.length > 0) {
return twitterTitle.content;
}
const docTitle = document.title;
if (docTitle != null && docTitle.length > 0) {
return docTitle;
}
const h1 = document.querySelector("h1").innerHTML;
if (h1 != null && h1.length > 0) {
return h1;
}
const h2 = document.querySelector("h2").innerHTML;
if (h2 != null && h2.length > 0) {
return h2;
}
return null;
});
return title;
};
const getDescription = async (page) => {
const description = await page.evaluate(() => {
const ogDescription = document.querySelector(
'meta[property="og:description"]'
);
if (ogDescription != null && ogDescription.content.length > 0) {
return ogDescription.content;
}
const twitterDescription = document.querySelector(
'meta[name="twitter:description"]'
);
if (twitterDescription != null && twitterDescription.content.length > 0) {
return twitterDescription.content;
}
const metaDescription = document.querySelector('meta[name="description"]');
if (metaDescription != null && metaDescription.content.length > 0) {
return metaDescription.content;
}
paragraphs = document.querySelectorAll("p");
let fstVisibleParagraph = null;
for (let i = 0; i < paragraphs.length; i++) {
if (
// if object is visible in dom
paragraphs[i].offsetParent !== null &&
!paragraphs[i].childElementCount != 0
) {
fstVisibleParagraph = paragraphs[i].textContent;
break;
}
}
return fstVisibleParagraph;
});
return description;
};
const getDomainName = async (page, uri) => {
const domainName = await page.evaluate(() => {
const canonicalLink = document.querySelector("link[rel=canonical]");
if (canonicalLink != null && canonicalLink.href.length > 0) {
return canonicalLink.href;
}
const ogUrlMeta = document.querySelector('meta[property="og:url"]');
if (ogUrlMeta != null && ogUrlMeta.content.length > 0) {
return ogUrlMeta.content;
}
return null;
});
return domainName != null
? new URL(domainName).hostname.replace("www.", "")
: new URL(uri).hostname.replace("www.", "");
};
module.exports = async (
uri,
puppeteerArgs = [],
puppeteerAgent = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
) => {
puppeteer.use(pluginStealth());
const browser = await puppeteer.launch({
headless: true,
args: [...puppeteerArgs],
});
const page = await browser.newPage();
page.setUserAgent(puppeteerAgent);
await page.goto(uri);
await page.exposeFunction("request", request);
await page.exposeFunction("urlImageIsAccessible", urlImageIsAccessible);
const obj = {};
obj.title = await getTitle(page);
obj.description = await getDescription(page);
obj.domain = await getDomainName(page, uri);
obj.img = await getImg(page, uri);
await browser.close();
return obj;
};