-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
131 lines (97 loc) · 3.16 KB
/
main.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
const CronJob = require('cron').CronJob;
const puppeteer = require('puppeteer');
const dotenv = require('dotenv');
var browser; //hold puppeteer session
start();
function start() {
dotenv.config({ path: '.env' });
let daysOfWeek = process.env.WEEK_DAYS.split(' ');
for (let day of daysOfWeek) {
for (let i = 0; i < 4; i++) {
let hoursMinutes = process.env[`HOUR${i + 1}`].split(':');
console.log(`${hoursMinutes[1]} ${hoursMinutes[0]} * * ${day}`);
let cron = new CronJob(
`${hoursMinutes[1]} ${hoursMinutes[0]} * * ${day}`,
() => { (i + 1) % 2 == 0 ? exit() : entry() },
null,
false,
process.env.TIMEZONE
);
cron.start();
}
}
}
async function entry() {
console.log("[INFO] starting clocking in");
let page = await newPage();
await login(page);
const elementHandle = await page.$(
'iframe[name="Main"]',
);
const frame = await elementHandle.contentFrame();
await frame.waitForSelector('[id$=_label9]');
console.log("[INFO] clocking done ", new Date());
await frame.tap('[id$=_label9]');
await page.close();
await closeBrowser();
}
async function exit() {
console.log("[INFO] starting clocking out");
let page = await newPage();
await login(page);
const elementHandle = await page.$(
'iframe[name="Main"]',
);
const frame = await elementHandle.contentFrame();
await frame.waitForSelector('[id$=_label10]');
console.log("[INFO] clocking done ", new Date());
await frame.tap('[id$=_label10]');
await page.close();
await closeBrowser();
}
async function login(page) {
console.log('[INFO] logging in ...');
await page.goto(process.env.LOGIN_URL, { waitUntil: 'networkidle0' });
await page.waitForSelector('[id$=_m_cUserName]');
await page.waitForSelector('[id$=_m_cPassword]');
await page.type('[id$=_m_cUserName]', process.env.USERNAME);
await page.type('[id$=_m_cPassword]', process.env.PASSWORD);
await page.waitForSelector('[id$=_Accedi]');
await page.tap('[id$=_Accedi]');
await page.waitForNavigation();
console.log('[INFO] login done!');
}
async function newPage() {
if (!browser) await openPuppeteer();
return await browser.newPage();
}
async function closeBrowser() {
if (!browser) return;
await browser.close();
}
async function openPuppeteer() {
if (browser) { return; }
var pathPi = '/usr/bin/chromium-browser';
var normalAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
//1366 768
var options =
{
headless: "new",
args: [
`--user-agent=${normalAgent}`,
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
`--window-size=1366,768`,
"--disable-notifications"
],
defaultViewport: { width: 1366, height: 768 },
executablePath: '/usr/bin/chromium'
};
//if (process.platform === 'linux' && process.arch === "arm") { options.executablePath = pathPi; } // Raspberry Pi
browser = await puppeteer.launch(options);
}