-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (91 loc) · 3.02 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
const chromium = require("chrome-aws-lambda");
const url = "https://sledilnik-social.netlify.app/";
const socialButton = "#social-changer + span";
const possibleSocial = ["TW", "FB"];
const possiblePosts = ["lab", "hos", "epi", "lab_w", "epi_w"];
const navButtons = ".tablinks";
const PostLinks = {
lab: 0,
hos: 1,
epi: 2,
lab_w: 3,
epi_w: 4,
};
const removeConsecutiveNewLines = (text) => {
const step1 = text.replace(/(\r\n|\r|\n){2,}/g, "\n");
return step1.slice(-1) === "\n" ? step1.slice(0, -1) : step1;
};
exports.handler = async (event, context, callback) => {
if (!event.queryStringParameters) {
return callback(undefined, "No target");
}
const { post = "", social = "" } = event.queryStringParameters;
if (!post) {
return callback(undefined, "No post!");
}
const _post = post.toLowerCase();
const _social = social ? social?.toUpperCase() : "FB";
if (!possiblePosts.includes(_post)) {
return callback(undefined, "Wrong post! ['lab' || 'hos' || 'epi']");
}
if (!possibleSocial.includes(_social)) {
return callback(undefined, "Wrong social! ['TW' || 'FB']");
}
let result;
let browser;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: true,
ignoreHTTPSErrors: true,
});
console.log("Made browser");
const page = await browser.newPage();
console.log("Made Page");
await page.setViewport({ width: 1366, height: 768 });
console.log("Set viewport");
await page.goto(url, { waitUntil: "networkidle0" });
console.log("Went to ", url);
if (_social === "FB") {
await page.waitForSelector(socialButton);
const socialElement = await page.$(socialButton);
await socialElement.click();
console.log("Change to FB!");
}
const tabLinks = await page.$$(navButtons);
const tabLink = tabLinks[PostLinks[_post]];
await tabLink.click();
console.log("Go to post: ", _post);
const postSelector = `#post-${_post}`;
await page.waitForSelector(postSelector);
const wantedPost = await page.$(postSelector);
console.log(`Grabbed post with selector: ${postSelector}`);
const innerText = await wantedPost.getInnerText();
console.log("Got innerText");
const text = removeConsecutiveNewLines(innerText);
console.log("Removed consecutive new lines");
result = text;
} catch (error) {
return callback(error);
} finally {
if (browser !== null) {
let pages = await browser.pages();
await Promise.all(pages.map((page) => page.close()));
console.log("All pages closed");
await browser.close();
console.log("Browser closed");
}
}
return callback(null, result);
};
if (require.main === module) {
console.log(
"this module was run directly from the command line as in node xxx.js"
);
} else {
console.log(
"this module was not run directly from the command line and probably loaded by something else"
);
}