-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
383 lines (306 loc) · 10.5 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
(function (win, doc) {
// Redirect from anywhere (the bookmarklet is also a regular bookmark)
if (win.location.hostname !== 'checkin.timewatch.co.il') {
win.location.href = 'https://checkin.timewatch.co.il';
return;
}
// Homepage, after login
if (window.location.pathname === '/punch/punch2_e.php') {
// Goto "Update punch data" page
var links = Array.from(document.querySelectorAll('a'))
var updatePunchDataLink = links.find((l) => l.getAttribute('href').startsWith('/punch/editwh.php?'));
if (updatePunchDataLink) return updatePunchDataLink.click();
}
var HALF = 'חצי';
var REST_DAY = 'מנוחה';
var DAY = 'day';
var MISSING = 'Missing';
var SICKNESS = 'מחלה';
var DAY_OFF = 'חופש';
var customStdHours = localStorage.getItem('customStdHours');
var today = new Date();
var popup;
var tableElm = doc.querySelectorAll('table table')[4];
var allTableRows = tableElm.querySelectorAll('tr'); // includes table headers
// Headers (first three rows)
var firstHeader = allTableRows[0];
var thirdHeader = allTableRows[2];
// Data Rows
var dataRows = Array.from(allTableRows).slice(3);
/* Note the column offset:
The 'Total Hours' column is 7th in the table's header but 12th on a day row (when 'Punch Data' has 3 in/out parts).
The 'Punch Data' column size is dynamic as employers could change the number of ins and outs.
┌────────────────────┐
│P u n c h D a t a │ <<------- firstHeader 1 cell
│ -1- │ -2- │ -3- │
│in|out│in|out│in|out│ <<------- thirdHeader 6 cells
*/
var punchOffset = thirdHeader.children.length - 1;
var isFirstRun = !win.isNice;
if (isFirstRun) {
appendColumnTitle();
// General styles
setStyle(doc.body, {
fontFamily: 'arial',
margin: '0 auto',
maxWidth: '1100px',
});
tableElm.style.borderSpacing = '0';
// Align titles
Array.from(firstHeader.children).concat(Array.from(thirdHeader.children)).forEach(function (title){
title.setAttribute('align', 'left');
});
// Set `isLoaded` flag
win.isNice = true;
}
var totalDiff = 0;
dataRows
.map(collectRawData)
.map(parseRawData)
.forEach(changeDOM)
;
showPopup();
// --------------------------------------------------------------
// Functions
// --------------------------------------------------------------
function createElm (tag) {
tag = tag || 'div';
return doc.createElement(tag);
}
function appendColumnTitle () {
var title = createNewTitle('Time Diff');
setStyle(title, {
color: 'white',
fontSize: '13px',
});
firstHeader.appendChild(title);
}
function appendDiffCell (day) {
// Data Cell Reference:
// <td bgcolor="#e6e6e6"><font size="2" face="Arial"> 9:28</font></td>
var cell = createElm('td');
// "Inherit" background color (copy from first sibling)
cell.setAttribute('bgcolor', day.rowCells[0].getAttribute('bgColor'));
setStyle(cell, {
textAlign: 'right',
fontSize: '15px',
fontWeight: 'bold',
});
day.rowElm.appendChild(cell);
day.rowCells.push(cell);
}
/*
A day should be ignored when:
1. Sickness/Vacation (only if a whole day)
2. Rest day (e.g. Fri, Sat)
3. Future date
4. No actual working hours
*/
function shouldBeIgnored (day) {
return (
(day.hasSickness || day.hasDayOff) && !day.hasHalfDay
|| day.isRestDay
|| day.isFutureDate
|| !day.actualWorkMinutes
);
}
function padWithZero (num) {
if (num < 10)
return '0' + num;
return String(num);
}
function createNewTitle (titleText) {
// Title Cell Reference:
// <td align="center" valign="middle" bgcolor="#7ba849" rowspan="3"><font size="2" face="Arial" color="white">Total Hours</font></td>
var title = createElm('td');
title.setAttribute('align', 'center');
title.setAttribute('valign', 'middle');
title.setAttribute('bgcolor', '#7ba849');
title.setAttribute('rowspan', '3');
title.innerHTML = titleText;
return title;
}
function setDiffCellValue (rowCells, value) {
// last cell
var cell = rowCells[rowCells.length - 1];
cell.style.color = value > 0 ? '#00bd00' : value < 0 ? '#ff4545' : 'black';
cell.innerHTML = value + ' ';
}
function setStyle (elm, style) {
for (var key in style) {
elm.style[key] = style[key];
}
}
function getTotalMinutes (timeString) {
if (!timeString) return 0;
var split = timeString.split(':').map(function (str) {
return parseInt(str, 10)
});
var hours = split[0];
var minutes = split[1];
var totalMinutes = (hours * 60) + minutes;
return totalMinutes;
}
function getContent (cellElm) {
return cellElm.textContent.trim();
}
function parseDate (rawDate) {
// e.g. '31-10-2018 Wed'
var split = rawDate.split(' ');
var date = split[0]; // 31-10-2018
var dayName = split[1]; // Wed
var dateSplit = date.split('-');
var dateObj = {
dayNumber: parseInt(dateSplit[0], 10),
month: parseInt(dateSplit[1], 10),
year: parseInt(dateSplit[2], 10),
};
return [dayName, dateObj];
}
function isFutureDate (dateObj) {
var date = new Date();
date.setFullYear(dateObj.year);
date.setMonth(dateObj.month - 1);
date.setDate(dateObj.dayNumber - 1);
return date.getTime() > today.getTime();
}
function getExpectedMinutes (stdHours, isHalfDayOff) {
var expectedMinutes = getTotalMinutes(stdHours);
if (isHalfDayOff) return expectedMinutes / 2;
return expectedMinutes;
}
function colorizeRow (day, bgColor, fgColor) {
day.rowCells.forEach(function (cell) {
cell.style.backgroundColor = bgColor;
if (fgColor) cell.style.color = fgColor;
});
}
function collectRawData (rowElm) {
var rowCells = Array.from(rowElm.children);
var cellsData = rowCells.map(function (cell) {return getContent(cell)})
return {
rowElm: rowElm,
rowCells: rowCells,
cellsData: cellsData,
column: {
'Date': cellsData[0],
'DayType': cellsData[1],
'DayName': cellsData[2],
'StdHoursCell': cellsData[3],
'StdHours': customStdHours || cellsData[3],
'Absence': cellsData[5 + punchOffset],
'Remark': cellsData[6 + punchOffset],
'TotalHours': cellsData[7 + punchOffset],
}
};
}
function parseRawData (rawRowObj) {
var day = rawRowObj;
var rawColumn = day.column;
var dayNameAndDate = parseDate(rawColumn.Date);
day.name = dayNameAndDate[0];
day.date = dayNameAndDate[1];
day.isFutureDate = isFutureDate(day.date);
/* Normalize columns:
The column titles are misleading:
Under 'Day Name' you'll see: 'יום עבודה', 'יום מנוחה', 'יום א' and more.
Under 'Day Type' you'll see: Sun, Mon, Tue... but also 'Holiday', 'Holiday Eve' and more.
It makes more sense to:
*/
day.title = rawColumn.DayType;
day.type = rawColumn.DayName;
var absence = rawColumn.Absence;
var remark = rawColumn.Remark;
var hasHalfDay = absence.includes(HALF) || remark.includes(HALF);
var hasSickness = absence.includes(SICKNESS) || remark.includes(SICKNESS);
var hasDayOff = absence.includes(DAY_OFF) || remark.includes(DAY_OFF);
day.hasHalfDay = hasHalfDay;
day.hasSickness = hasSickness;
day.hasDayOff = hasDayOff;
var stdHoursCell = rawColumn.StdHoursCell;
var stdHours = rawColumn.StdHours;
var totalHours = rawColumn.TotalHours;
day.isRestDay = day.type.includes(REST_DAY) && !stdHoursCell;
day.isMissing = totalHours.toLowerCase().includes(MISSING);
day.isXday = day.type.toLowerCase().includes(DAY) || day.title.toLowerCase().includes(DAY);
day.isHalfSick = hasHalfDay && hasSickness;
day.isHalfDayOff = hasHalfDay && hasDayOff;
day.expectedMinutes = getExpectedMinutes(stdHours, hasHalfDay);
day.actualWorkMinutes = getTotalMinutes(totalHours);
return day;
}
function changeDOM (day) {
if (isFirstRun) {
appendDiffCell(day);
if (day.isRestDay) {
colorizeRow(day, '#cecece', '#544343');
}
else if (day.isFutureDate) {
colorizeRow(day, '#bdbdbd', '#796363');
}
}
if (shouldBeIgnored(day)) return;
// Calculate time diff
var currentDiff = day.actualWorkMinutes - day.expectedMinutes;
setDiffCellValue(day.rowCells, currentDiff);
// Sum up
totalDiff += currentDiff;
}
function showPopup () {
var titleText = totalDiff < 0 ? 'Missing Time' : totalDiff > 0 ? 'Extra Time': 'No Time Diff :)';
var sign = totalDiff < 0 ? '-' : '+';
var absDiff = Math.abs(totalDiff);
var hoursDiff = padWithZero(Math.floor(absDiff / 60));
var minsDiff = padWithZero(absDiff % 60);
var link = 'https://github.com/taitulism/TimeWatch-Bookmarklet';
var repoLink = '<a href="'+link+'" style="color:white;">'+link+'</a>';
// Create elements
popup = createElm();
var header = createElm();
var body = createElm();
var footer = createElm();
header.innerHTML = titleText;
body.innerHTML = sign + hoursDiff + ':' + minsDiff;
footer.innerHTML = repoLink;
// Style
setStyle(header, {
backgroundColor: (sign === '+') ? '#5b921d' : 'red',
color: 'white',
textAlign: 'center',
padding: '1em',
});
setStyle(body, {
textAlign: 'center',
fontSize: '140%',
});
setStyle(footer, {
fontSize: '85%',
color: 'white',
letterSpacing: '0.5px',
});
setStyle(popup, {
width: '400px',
height: '200px',
position: 'fixed',
top: '150px',
left: '38%',
backgroundColor: '#403434',
color: '#e4d9d9',
padding: '1em 2em',
borderRadius: '10px',
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-around',
cursor: 'pointer',
});
popup.appendChild(header);
popup.appendChild(body);
popup.appendChild(footer);
doc.body.appendChild(popup);
function dismissPopup() {
popup.removeEventListener('click', dismissPopup);
popup.parentNode.removeChild(popup);
}
popup.addEventListener('click', dismissPopup);
}
})(window, document);