-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.js
248 lines (228 loc) · 7.17 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"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 isBase64 = require("is-base64");
const urlImageIsAccessible = async (url) => {
const correctedUrls = getUrls(url);
if (isBase64(url, { allowMime: true })) {
return true;
}
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}/${img.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 h1El = document.querySelector("h1");
const h1 = h1El ? h1El.innerHTML : null;
if (h1 != null && h1.length > 0) {
return h1;
}
const h2El = document.querySelector("h2");
const h2 = h2El ? h2El.innerHTML : null;
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;
}
let 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.", "");
};
const getFavicon = async (page, uri) => {
const noLinkIcon = `${new URL(uri).origin}/favicon.ico`;
if (await urlImageIsAccessible(noLinkIcon)) {
return noLinkIcon;
}
const favicon = await page.evaluate(async () => {
const icon16Sizes = document.querySelector('link[rel=icon][sizes="16x16"]');
if (
icon16Sizes &&
icon16Sizes.href.length > 0 &&
(await urlImageIsAccessible(icon16Sizes.href))
) {
return icon16Sizes.href;
}
const shortcutIcon = document.querySelector('link[rel="shortcut icon"]');
if (
shortcutIcon &&
shortcutIcon.href.length > 0 &&
(await urlImageIsAccessible(shortcutIcon.href))
) {
return shortcutIcon.href;
}
const icons = document.querySelectorAll("link[rel=icon]");
for (let i = 0; i < icons.length; i++) {
if (
icons[i] &&
icons[i].href.length > 0 &&
(await urlImageIsAccessible(icons[i].href))
) {
return icons[i].href;
}
}
const appleTouchIcons = document.querySelectorAll('link[rel="apple-touch-icon"],link[rel="apple-touch-icon-precomposed"]');
for (let i = 0; i < appleTouchIcons.length; i ++) {
if (
appleTouchIcons[i] &&
appleTouchIcons[i].href.length > 0 &&
(await urlImageIsAccessible(appleTouchIcons[i].href))
) {
return appleTouchIcons[i].href;
}
}
return null;
})
return favicon;
}
module.exports = async (
uri,
puppeteerArgs = [],
puppeteerAgent = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
executablePath
) => {
puppeteer.use(pluginStealth());
const params = {
headless: true,
args: [...puppeteerArgs],
};
if (executablePath) {
params["executablePath"] = executablePath;
}
const browser = await puppeteer.launch(params);
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);
obj.favicon = await getFavicon(page, uri);
await browser.close();
return obj;
};