-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (50 loc) · 1.45 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
"use strict";
const chromium = require("chrome-aws-lambda");
exports.getEmailProviderName = async function(event) {
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
const page = await browser.newPage();
await page.goto("https://mxtoolbox.com/MXLookup.aspx");
const inputIdentifier = "input.tools_lookup_textbox.form-control";
const request = JSON.parse(event.body);
await page.waitFor(inputIdentifier);
await page.$eval(
inputIdentifier,
(el, value) => (el.value = value),
request.domain
);
await page.click('input[type="submit"].btn.btn-mx');
await page.waitForSelector(".tool-result-body");
const element = await page.$("pre.alert");
if (!element) {
throw new Error("NO_TEXT_ELEMENT");
}
const text = await page.evaluate(element => element.textContent, element);
await browser.close();
const response = {
statusCode: 200,
body: JSON.stringify({
emailProviderName: text || "UNKNOWN"
})
};
return response;
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({
emailProviderName: "UNKNOWN",
errorMessage: error.message
})
};
} finally {
if (browser !== null) {
await browser.close();
}
}
};