-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADP.user.js
More file actions
202 lines (176 loc) · 7.33 KB
/
ADP.user.js
File metadata and controls
202 lines (176 loc) · 7.33 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
// ==UserScript==
// @name ADP Plus
// @description Adds Autofill Time Clock to ADP Comments for CBU Student Workers
// @namespace Violentmonkey Scripts
// @include https://eetd2.adp.com/*
// @grant none
// @version 1.0
// @author Daniel Crooks
// ==/UserScript==
function getRowInfo(row_number) {
const inPunchTime = document.getElementById("widgetFrame2402").contentWindow.document.querySelector(`#column-inPunch-Row-${row_number} > div`).innerText;
const outPunchTime = document.getElementById("widgetFrame2402").contentWindow.document.querySelector(`#column-outPunch-Row-${row_number} > div`).innerText;
const calculatedHours = calculateHours(inPunchTime, outPunchTime);
return `${inPunchTime} - ${outPunchTime}, ${calculatedHours} hours, `; // comment format
}
function calculateHours(punchIn, punchOut) {
// TODO: 15 minute intervals (7 = down, 8min = up)
// nearest :00 :15 :30 :45
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 hoursWorked.toFixed(1);
}
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 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();
}
});
}
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();
const row = getRowInfo(row_number);
autofillComment(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;
}
}
}
}
});
function waitForPageLoad(callback) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
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
}
}
}, 5000);
}
function waitForElementText(selector, value, callback) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
const iframeDocument = element.contentWindow.document;
const innerText = iframeDocument.querySelector(selector).innerText == value;
try {
if (innerText) { // FIXME: catch errros
clearInterval(intervalId); // Stop checking
callback(); // Execute the provided callback function
}
} catch(e) {}
}
}, 500);
}
function waitForElementToBeVisible(selector, callback, continuous) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
const iframeDocument = element.contentWindow.document;
const visible = iframeDocument.querySelector(selector).checkVisibility();
if (visible) {
if (continuous) { // FIXME: catch errors
clearInterval(intervalId); // Stop checking
}
callback(); // Execute the provided callback function
}
}
}, 500);
}
function waitForElementToBeEnabled(selector, callback, continuous) {
const intervalId = setInterval(() => {
const element = document.getElementById("widgetFrame2402");
if (element) {
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
}
}
}, 500);
}
function autoSave(seconds) {
const intervalId = setInterval(() => {
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("Saved!");
}
}
}, (seconds * 1000)); // Check every x seconds
}
waitForPageLoad(() => {
console.log("Page Fully Loaded!");
enterKeyPressOKButton();
autoSave(10);
});
function autoClickComment() {
waitForElementToBeVisible("#commentButton\\.workedshift", () => {
document.getElementById("widgetFrame2402").contentWindow.document.querySelector("#commentButton\\.workedshift").click(); // Comment
waitForElementText("#comments_lbl_commentsSize", 'Comments (0)', () => { // FIXME: Change 1 -> 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)
}
autoClickComment();
fullscreenObserver.observe(document.body, { childList: true, subtree: true });