-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
173 lines (154 loc) · 4.76 KB
/
userscript.js
File metadata and controls
173 lines (154 loc) · 4.76 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
// ==UserScript==
// @name CRM Dummy
// @version 2025-01-05
// @description Userscript for filling weekly timesheet at my dayjob
// @author Anhsirk0
// @match https://crm.softsensor.ai/account/timelogs/weekly-timesheets
// @icon https://www.google.com/s2/favicons?sz=64&domain=softsensor.ai
// @grant none
// ==/UserScript==
(function () {
"use strict";
/**
* @enum {string}
*/
const Task = Object.freeze({
Organisation: "Organisation-wide meeting",
StandUp: "Scheduler standup call",
WrapUp: "Scheduler wrapup call",
Main: "Working on Scheduler",
Bench: "Task not assigned",
Testing: "QA Testing meeting",
Timesheet: "Timesheet record, Administrative activity",
});
/**
* Click on an element by selector (inside base)
* @param {string} - Selector
* @param {Element} [base=document] - Base Element (used as base to select)
*/
async function click(selector, base = document) {
const btn = base.querySelector(selector);
if (btn) {
btn.click();
await sleep(1);
} else console.log(`Could not click '${selector}'`);
}
/**
* Get a random item from an array
* @param {Array<T>} - The array
* @returns {T} The random item
*/
function randomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
}
/**
* Sleep function to wait while the page/actions loads
* @param {number} - time to wait in seconds
*/
function sleep(s) {
return new Promise((resolve) => setTimeout(resolve, s * 1000));
}
/**
* Clicks on the add more task button
*/
async function clickAddMore() {
await click("#add-more-task");
}
/**
* Set the value of the task using the task dropdown
* @param {Task} - The task
* @param {number} - The index/order of the task
* @param {boolean} [addMore='true'] - Whether to click on add more button
*/
async function setTask(task, idx, addMore = true) {
await sleep(1);
if (addMore) await clickAddMore();
let ariaId = `bs-select-${idx}`;
let optionId = `${ariaId}-38`;
if (task === Task.StandUp) {
optionId = `${ariaId}-4`;
} else if (task === Task.Main) {
optionId = `${ariaId}-34`;
} else if (task === Task.Bench) {
optionId = `${ariaId}-56`;
} else if (task === Task.WrapUp) {
optionId = `${ariaId}-5`;
} else if (task === Task.Testing) {
optionId = `${ariaId}-39`;
} else if (task === Task.Timesheet) {
optionId = `${ariaId}-1`;
}
await click(`button[aria-owns=${ariaId}]`);
await click(`#${optionId}`);
}
/**
* Fill all 5 input hours (Monday-Friday)
* @param {Task} - The task
* @param {number} - The index/order of the task
*/
async function fillInputs(task, idx, days = 5) {
let hrs = 0.5; // Number or Array<Number>
if (task === Task.StandUp) {
hrs = 0.25;
} else if (task === Task.Main) {
hrs = 5.5;
} else if (task === Task.WrapUp) {
hrs = 0.25;
} else if (task === Task.Bench) {
hrs = [2, 1.5];
} else if (task === Task.Testing) {
hrs = [0, 0.25, 0.5];
}
for (let i = 0; i < days; i++) {
let text = Array.isArray(hrs) ? randomChoice(hrs) : hrs;
if (task === Task.Timesheet) {
text = i === days - 1 ? 0.25 : 0;
}
const sel = `td[data-index='${i}']>input[name='hours[${idx - 1}][]']`;
const input = document.querySelector(sel);
if (input) input.value = text;
}
}
/**
* Set the value of the task using the task dropdown, and fill inputs
* @param {Task} - The task
* @param {number} - The index/order of the task
* @param {boolean} [addMore='true'] - Whether to click on add more button
*/
async function fillTask(task, order, addMore = true) {
await setTask(task, order, addMore);
await fillInputs(task, order, 5);
}
/**
* Fill task for each task
*/
async function fillAll() {
let order = 1;
await fillTask(Task.Organisation, order++, false);
await fillTask(Task.StandUp, order++);
await fillTask(Task.Main, order++);
await fillTask(Task.Bench, order++);
await fillTask(Task.Testing, order++);
await fillTask(Task.WrapUp, order++);
await fillTask(Task.Timesheet, order++);
}
/**
* Add a button to fill all tasks
*/
function addFillButton() {
const table = document.querySelector("div.table-responsive");
if (!table) return;
const div = table.querySelector("div.mt-3");
if (!div) return;
const btn = document.createElement("button");
btn.classList.add("btn-info", "f-14", "p-2", "rounded");
btn.innerHTML = "Fill Timesheet";
btn.type = "button";
btn.onclick = () => fillAll();
div.appendChild(btn);
}
function main() {
setTimeout(addFillButton, 200);
}
setTimeout(main, 1200);
})();