-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
48 lines (40 loc) · 965 Bytes
/
utils.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
const fs = require('fs');
/**
* Read file content
*/
const readFile = (filename, encoding) => {
try {
return fs.readFileSync(filename, encoding);
} catch (e) {
return null;
}
};
/**
* Write a given object in a file
*/
const writeInFile = (filename, object) => {
fs.writeFileSync(filename, JSON.stringify(object, null, 2));
};
/**
* Login to LinkedIn
*/
const loginToLinkedIn = async (browser, sessionCookie) => {
console.log('Login to LinkedIn');
const page = await browser.newPage();
await page.setCookie({
name: 'li_at',
value: sessionCookie,
domain: 'www.linkedin.com',
});
await page.goto('https://www.linkedin.com/feed/');
// If we're redirect to the login page, session cookie is not valid
if (/www.linkedin.com\/m\/login/.test(page.url())) {
throw new Error("Can't sign in with this session cookie");
}
await page.close();
};
module.exports = {
readFile,
writeInFile,
loginToLinkedIn,
};