-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinject.js
311 lines (242 loc) · 10.5 KB
/
inject.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
let gradesData = {};
function consoleLog(text) {
console.log(`%c [Epitech GPA Extension] ${text}`, "color: cyan");
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getIntranetCookie() {
const repsonse = await chrome.runtime.sendMessage({ command: "getIntranetCookie" });
return repsonse.cookie ?? null;
}
function getIntranetLogin() {
const uri = window.location.href;
if (!uri.includes("/user")) {
return null;
}
const login = document.evaluate(
'//*[@id="profil"]/div[3]/div/div[3]/div[1]/span',
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
return login ? login.textContent.trim() : null;
}
async function injectTableColumn() {
await sleep(500);
const table = document.querySelector("#user-module > div:nth-child(1) > table");
const tableRows = table.querySelectorAll("tr");
if (tableRows[0].children[3].textContent.trim() === "Projection Grade") {
consoleLog("Intranet is so bad that it already has the column... skipping...")
return;
}
const newColumnHeader = document.createElement("th");
newColumnHeader.style.width = "75px";
const divOuter = document.createElement('div');
const divInner = document.createElement('div');
const spanResizer = document.createElement('span');
const spanUpDown = document.createElement('span');
const label = document.createElement('label');
divOuter.appendChild(divInner);
divInner.appendChild(spanResizer);
divInner.appendChild(spanUpDown);
divInner.appendChild(label);
spanResizer.classList.add('resizer');
spanUpDown.classList.add('updown');
label.textContent = 'Projection Grade';
label.style.color = 'green';
newColumnHeader.appendChild(divOuter);
tableRows[0].insertBefore(newColumnHeader, tableRows[0].children[3]);
const tableWhat = document.querySelector("#user-module > div.overflow > div > table");
const tableWhatRows = tableWhat.querySelectorAll("tr");
const newColumnHeaderWhat = document.createElement("th");
newColumnHeaderWhat.style.width = "75px";
tableWhatRows[0].insertBefore(newColumnHeaderWhat, tableWhatRows[0].children[3]);
}
async function injectTableData() {
await sleep(500);
const table = document.querySelector("#user-module > div.overflow > div > table");
const tableRows = table.querySelectorAll("tr");
for (let i = 2; i < tableRows.length; i++) {
const row = tableRows[i];
const newColumn = document.createElement("td");
newColumn.className = "grade projection";
newColumn.setAttribute("tabindex", "0");
const select = document.createElement('select');
select.classList.add('select');
select.setAttribute('credit', row.children[3].textContent.trim());
select.setAttribute('module', row.children[5].textContent.trim());
select.setAttribute('instance', row.children[0].children[0].getAttribute("href").split("/")[4]);
select.setAttribute('year', row.children[4].textContent.trim());
const optionDefault = document.createElement('option');
optionDefault.value = '-';
optionDefault.textContent = '-';
select.appendChild(optionDefault);
const optionAcquis = document.createElement('option');
optionAcquis.value = 'Acquis';
optionAcquis.textContent = 'Acquis';
select.appendChild(optionAcquis);
if (row.children[5].textContent.trim() !== "G-EPI-030") {
const optionA = document.createElement('option');
optionA.value = 'A';
optionA.textContent = 'A';
select.appendChild(optionA);
const optionB = document.createElement('option');
optionB.value = 'B';
optionB.textContent = 'B';
select.appendChild(optionB);
const optionC = document.createElement('option');
optionC.value = 'C';
optionC.textContent = 'C';
select.appendChild(optionC);
const optionD = document.createElement('option');
optionD.value = 'D';
optionD.textContent = 'D';
select.appendChild(optionD);
const optionEchec = document.createElement('option');
optionEchec.value = 'Echec';
optionEchec.textContent = 'Echec';
select.appendChild(optionEchec);
}
newColumn.appendChild(select);
select.addEventListener("mouseup", event => event.stopPropagation(), true);
select.addEventListener("mousedown", event => event.stopPropagation(), true);
row.insertBefore(newColumn, row.children[3]);
// We need to compare the year AND the instance because the same module can be taken multiple times (e.g. JAM / English)
// If we don't compare the instance, we might end up changing the grade of all the instances of the JAM / English module
const currentGrade = gradesData.modules.find(grade => grade.codemodule === row.children[6].textContent.trim() && grade.scolaryear == row.children[5].textContent.trim() && grade.codeinstance === row.children[0].children[0].getAttribute("href").split("/")[4]).grade;
const selectSelector = newColumn.querySelector("select");
const options = selectSelector.querySelectorAll("option");
let found = false;
for (let j = 0; j < options.length; j++) {
if (options[j].value === currentGrade) {
options[j].setAttribute("selected", "selected");
found = true;
}
}
if (!found) {
options[5].setAttribute("selected", "selected");
consoleLog("Could not find grade in select options");
}
selectSelector.addEventListener("change", onChange);
}
}
function onChange() {
const table = document.querySelector("#user-module > div.overflow > div > table");
const tableRows = table.querySelectorAll("tr");
for (let i = 2; i < tableRows.length; i++) {
const row = tableRows[i];
const select = row.children[3].querySelector("select");
const newGrade = select.options[select.selectedIndex].value;
const module = select.getAttribute("module");
gradesData.modules = gradesData.modules.map(grade => {
// We need to compare the year AND the instance because the same module can be taken multiple times (e.g. JAM / English)
// If we don't compare the instance, we might end up changing the grade of all the instances of the JAM / English module
if (grade.codemodule === module && grade.codeinstance === select.getAttribute("instance") && grade.scolaryear == select.getAttribute("year")) {
grade.grade = newGrade;
}
return grade;
});
}
const computedGPA = computeGPA(gradesData);
const computedCredits = computeCredits(gradesData);
editData(computedGPA, computedCredits);
}
async function injectData(gpa = "-", credits = 0) {
const noteZone = document.querySelector("#profil > div.bloc.top > div.rzone > span");
const dataLabel = document.createElement("label");
dataLabel.textContent = "G.P.A. Projection";
dataLabel.style.color = "green";
noteZone.appendChild(dataLabel);
const dataValue = document.createElement("span");
dataValue.className = "value";
dataValue.style.color = "green";
const textNode = document.createTextNode(`${gpa} (${credits} credits)`);
while (dataValue.firstChild) {
dataValue.removeChild(dataValue.firstChild);
}
dataValue.appendChild(textNode);
noteZone.appendChild(dataValue);
const studentGrades = await getStudentGrades();
const computedGPA = computeGPA(studentGrades);
const computedCredits = computeCredits(studentGrades);
editData(computedGPA, computedCredits);
}
function editData(gpa, credits) {
const gpaValue = document.querySelector("#profil > div.bloc.top > div.rzone > span > span:nth-child(6)");
if (!gpaValue) {
consoleLog("No custom GPA value found");
return;
}
const textNode = document.createTextNode(`${gpa} (${credits} credits)`);
while (gpaValue.firstChild) {
gpaValue.removeChild(gpaValue.firstChild);
}
gpaValue.appendChild(textNode);
}
function computeGPA(grades) {
const gradeMappping = {
"A": 4,
"B": 3,
"C": 2,
"D": 1,
};
const gradesToBypass = ["-", "Acquis"];
const totalAcquiredCredits = grades.modules.reduce((acc, grade) => {
return acc + parseFloat(gradesToBypass.includes(grade.grade) ? 0 : grade.credits);
}, 0);
const gpa = grades.modules.reduce((acc, grade) => {
if (gradesToBypass.includes(grade.grade)) {
return acc;
}
const gradeValue = parseFloat(grade.credits);
const gradeWeight = parseFloat(gradeMappping[grade.grade] ?? 0);
return acc + (gradeValue * gradeWeight);
}, 0);
return (gpa / totalAcquiredCredits).toFixed(2);
}
async function getStudentGrades() {
const cookie = await getIntranetCookie();
const login = getIntranetLogin();
if (!cookie || !login) {
consoleLog("No cookie or login found");
return;
}
const response = await fetch(`https://intra.epitech.eu/user/${login}/notes?format=json`, {
headers: {
"Cookie": `user=${cookie},${document.cookie}`
}
});
const data = await response.json();
gradesData = data;
return data;
}
function computeCredits(grades) {
const gradesToBypass = ["-", "Echec"];
// Retrieve and reduce all the credits obtained since first year
// The Final jury credits already removes the supplements all by itself
return grades.modules.reduce((acc, grade) => {
return acc + parseInt(gradesToBypass.includes(grade.grade) ? 0 : grade.credits);
}, 0);
}
window.navigation.addEventListener("navigate", (event) => {
const uri = event.destination.url;
if (uri.includes("/user/#!/notes")) {
consoleLog("Injecting table column and data...");
injectTableColumn();
injectTableData();
}
});
(() => {
if (window.location.href.includes("/user/#!/notes")) {
consoleLog("Injecting table column and data...");
injectTableColumn();
injectTableData();
}
if (window.location.href.includes("/user")) {
consoleLog(`- v${chrome.runtime.getManifest().version}`);
consoleLog("Injecting GPA Projection script...")
injectData();
}
})();