-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADP Plus.user.js
More file actions
328 lines (280 loc) · 11.4 KB
/
ADP Plus.user.js
File metadata and controls
328 lines (280 loc) · 11.4 KB
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
// ==UserScript==
// @name ADP Plus
// @description A Chrome Extension that adds AutoFill, AutoSave & other features to ADP TimeClock for CBU Student Workers
// @namespace ADP
// @include https://eetd2.adp.com/*
// @include https://*.mykronos.com/*
// @grant none
// @version 1.3.1
// @author Daniel Crooks
// @icon https://raw.githubusercontent.com/inVariabl/adp-plus/main/extension/icon.png
// @license GPL-3
// @downloadURL https://github.com/inVariabl/adp-plus/raw/main/ADP%20Plus.user.js
// @updateURL https://github.com/inVariabl/adp-plus/raw/main/ADP%20Plus.user.js
// ==/UserScript==
const SAVE_INTERVAL = 10; // AutoSaves every 10 seconds
function getRowInfo(row_number) {
let inPunchTime = null;
let outPunchTime = null;
let hr = "hours";
let punchDate = "";
let calculatedHours;
inPunchTime = document.getElementById("widgetFrame2402").contentWindow.document.querySelector(`#column-inPunch-Row-${row_number} > div`).innerText;
outPunchTime = document.getElementById("widgetFrame2402").contentWindow.document.querySelector(`#column-outPunch-Row-${row_number} > div`).innerText;
punchDate = document.getElementById("widgetFrame2402").contentWindow.document.querySelector(`#column-Date-Row-${row_number} > div`).innerText;
punchDate = simplifyDate(punchDate);
if (!confirm(`Do you need to fix your Time Clock? \n\n${punchDate}; ${calculatedHours} hrs; ${inPunchTime} - ${outPunchTime};\n\n 'Ok' to Fix Clock, 'Cancel' if perfect`)) {
return ``; // blank string so just input comment
}
inPunchTime = getPunchTimeFromUser("In", inPunchTime);
outPunchTime = getPunchTimeFromUser("Out", outPunchTime);
calculatedHours = calculateHours(inPunchTime, outPunchTime);
hr = hourOrhours(calculatedHours);
//inPunchTime = roundToNearestHalfHour(inPunchTime);
//outPunchTime = roundToNearestHalfHour(outPunchTime);
// TODO: Make Picker to Select RD
// Jeremy Duket Format:
// [FIX: Start - End, Total Hours, Description]
// e.g. [FIX: 2:00 - 5:00pm, 3 hrs, staff meeting]
// return `${fix}${inPunchTime} - ${outPunchTime}, ${calculatedHours} hours, `; // Jeremy Duket comment format
// Austin Iannuzzi Format:
// [Date; Total Hours; Time Frame; Description]
// e.g. [8/6; 3 hrs; 2-5pm; staff meeting]
return `${punchDate}; ${calculatedHours} ${hr}; ${inPunchTime} - ${outPunchTime}; `;
}
function hourOrhours(calculatedHours) {
return calculatedHours == 1 ? 'hour' : 'hours';
}
function getPunchTimeFromUser(inOrOut, punchTime) {
return window.prompt(`Punch ${inOrOut}: `, punchTime);
}
function roundToNearestHalfHour(timeStr) {
// Parse the time string
let [time, period] = timeStr.split(/(?=[APM])/);
let [hours, minutes] = time.split(':').map(Number);
// Convert to 24-hour format for easier manipulation
if (period === 'PM' && hours !== 12) {
hours += 12;
} else if (period === 'AM' && hours === 12) {
hours = 0;
}
// Round minutes to the nearest half hour
if (minutes < 15) {
minutes = 0;
} else if (minutes >= 15 && minutes < 45) {
minutes = 30;
} else {
minutes = 0;
hours += 1;
}
// Adjust hours if it overflows
if (hours === 24) {
hours = 0;
}
// Convert back to 12-hour format
period = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
if (hours === 0) {
hours = 12;
}
// Format minutes to always be two digits
minutes = minutes.toString().padStart(2, '0');
// Return the rounded time string
return `${hours}:${minutes}${period}`;
}
function calculateHours(punchIn, punchOut) {
punchIn = convertTo24(punchIn)[0];
punchOut = convertTo24(punchOut);
const start = new Date("2000-01-01 " + punchIn);
let end = new Date("2000-01-01 " + punchOut[0]);
if (punchOut[1] === 1) { end = new Date("2000-01-02 " + punchOut[0]); }
const timeDifference = (end - start);
const hoursWorked = timeDifference / (1000 * 60 * 60);
// return roundUpToNearestQuarter(hoursWorked); // CA Law No Longer Rounds
return roundUpToThreeDecimals(hoursWorked);
}
function roundUpToThreeDecimals(number) {
return Math.ceil(number * 1000) / 1000;
}
function convertTo24(time12) {
const [time, period] = time12.match(/^(\d+:\d+)([APap][Mm])$/).slice(1);
let [hours, minutes] = time.split(":");
let day = 0;
hours = parseInt(hours);
if (period === "PM" && hours !== 12) { hours += 12; }
else if (period === "AM" && hours === 12) {
hours = 0;
}
if (hours < 5) { // before 5:00AM = new day
day = 1;
}
return [`${hours.toString()}:${minutes}`, day];
}
function simplifyDate(dateString) {
const datePart = dateString.split(' ')[1];
const [month, day] = datePart.split('/').map(part => part.replace(/^0+/, ''));
return `${month}/${day}`;
}
function autofillComment(row_number) {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#comments_0_kcomment_0_knote_txtarea_editNote").value = getRowInfo(row_number);
}
function enterKeyPressOKButton() {
document.getElementById("widgetFrame2402").contentWindow.document.addEventListener("keydown", function(event) {
if (event.key === "Return" || event.key === "Enter") {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#addComment > footer > div > a.btn.btn-ok.krn-hard-destroy-monitor-control.ng-binding").click();
}
});
saveTimeCard(); // Save timecard after saving comment
}
function escapeKeyPressCloseButton() {
document.getElementById("widgetFrame2402").contentWindow.document.addEventListener("keydown", function(event) {
if (event.key === "Escape") {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#comment-add-dialog > div.jqx-window-header.jqx-window-header-bluefin.jqx-widget-header.jqx-widget-header-bluefin.jqx-disableselect.jqx-disableselect-bluefin.jqx-rc-t.jqx-rc-t-bluefin > div.jqx-window-close-button-background.jqx-window-close-button-background-bluefin > div").click();
}
});
saveTimeCard(); // Save timecard after saving comment
}
function maximizeTimeCard() {
document.querySelector("body > krn-app > krn-navigator-container > ui-view > krn-workspace-manager-container > krn-workspace > div > krn-layout-manager > div > div.krn-layout-manager__slot.resizable.no-transition.col75.h100.hide-grabber.order1 > krn-widget > krn-toolbar > h5 > div > button:nth-child(1)").click();
}
function getSelectedRowNumber() {
const row_id = document.getElementById("widgetFrame2402").contentWindow.document.querySelector("div.jqx-fill-state-pressed-bluefin").children[0].id;
const regex = /\d+/;
const match = row_id.match(regex);
if (match) {
return parseInt(match[0]);
} else {
console.error("No number found in the string.");
}
}
function fill() {
const row_number = getSelectedRowNumber();
autofillComment(row_number);
//const row = getRowInfo(row_number);
//console.log({row});
}
const fullscreenObserver = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.addedNodes) {
for (const node of mutation.addedNodes) {
if (node instanceof HTMLElement && node.matches("button.krn-toolbar__button")) {
maximizeTimeCard();
observer.disconnect();
return;
}
}
}
}
});
// TODO: These callback functions should be abstracted and standardized to avoid callback hell
function waitForPageLoad(callback) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
try {
const iframeDocument = element.contentWindow.document;
const innerText = iframeDocument.querySelector("#columntablejqxGrid0 > div:nth-child(3) > div > div:nth-child(1) > div").innerText;
if (innerText == "Date") {
clearInterval(intervalId); // Stop checking
callback(); // Execute the provided callback function
}
} catch(e) {
// Page Didn't Load
}
}
}, 5000);
}
function waitForElementText(selector, value, callback) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
try {
const iframeDocument = element.contentWindow.document;
const innerText = iframeDocument.querySelector(selector).innerText == value;
if (innerText) {
clearInterval(intervalId);
callback();
}
} catch(e) {
// Element Text Not Available
}
}
}, 500);
}
function waitForElementToBeVisible(selector, callback, continuous) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
try {
const iframeDocument = element.contentWindow.document;
const visible = iframeDocument.querySelector(selector).checkVisibility();
if (visible) {
if (continuous) {
clearInterval(intervalId); // Stop checking
}
callback(); // Execute the provided callback function
}
} catch(e) {
// Element Not Visible
}
}
}, 500);
}
function waitForElementToBeEnabled(selector, callback, continuous) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
try {
const iframeDocument = element.contentWindow.document;
const disabled = iframeDocument.querySelector(selector).disabled;
if (!disabled) {
if (continuous) {
clearInterval(intervalId); // Stop checking
}
callback(); // Execute the provided callback function
}
} catch(e) {
// Element Disabled
}
}
}, 500);
}
function autoSave(seconds) {
const intervalId = setInterval(() => {
saveTimeCard();
}, (seconds * 1000)); // Check every x seconds
}
function saveTimeCard() {
const element = document.getElementById("widgetFrame2402");
if (element) {
const iframeDocument = element.contentWindow.document;
const saved = iframeDocument.querySelector("#saveButton_btn").classList.contains("disabled");
if (!saved) {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#saveButton_btn > i").click();
console.log("TimeCard Saved.");
}
}
}
function autoClickComment() {
waitForElementToBeVisible("#commentButton\\.workedshift", () => {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#commentButton\\.workedshift").click(); // Comment
waitForElementText("#comments_lbl_commentsSize", 'Comments (0)', () => {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#comments_kcombo_addCommentsList_toggleBtn").click(); // DropDown
waitForElementToBeVisible("#comments_kcombo_addCommentsList_li9", () => {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#comments_kcombo_addCommentsList_li9").click(); // Other
waitForElementToBeEnabled("#comments_0_kcomment_0_knote_txtarea_editNote", () => {
fill();
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#comments_0_kcomment_0_knote_txtarea_editNote").isContentEditable = true;
}, true)
}, false)
});
}, false)
}
fullscreenObserver.observe(document.body, { childList: true, subtree: true });
waitForPageLoad(() => {
console.log("Page Fully Loaded!");
enterKeyPressOKButton();
escapeKeyPressCloseButton();
autoSave(SAVE_INTERVAL);
autoClickComment();
});