-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-grades-json.user.js
397 lines (367 loc) · 13.2 KB
/
extract-grades-json.user.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// ==UserScript==
// @name Extract Grades With JSON format [ECE Paris]
// @namespace ECE Paris Script
// @version 0.1
// @description Allows you to calculate course averages automatically and add the average box to the grades table.
// @author BragdonD
// @match https://campusonline.inseec.net/note/*
// @require https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @homepageURL https://github.com/bragdond/ECE-Scripts
// @supportURL https://github.com/bragdond/ECE-Scripts/issues
// @downloadURL https://raw.githubusercontent.com/bragdond/ECE-Scripts/main/extract-grades-json.user.js
// @updateURL https://raw.githubusercontent.com/bragdond/ECE-Scripts/main/extract-grades-json.user.js
// @grant none
// ==/UserScript==
/**
* @typedef {Object} Grade
* @property {number} value - The value of the grade.
* @property {number} weight - The weight of the grade.
*/
/**
* @typedef {Object} CourseGradePart
* @property {string} name - The name of the course grade part.
* @property {number} weight - The weight of the course grade part.r}
* @property {Array.<Grade>} grades - The grades of the course grade part.
* @property {number} average - The average grade of the course grade part.
*/
/**
* @typedef {CourseGrade}
* @property {string} name - The name of the course.
* @property {Array.<CourseGradePart>} courseParts - The course grade parts.
* @property {number} coefficient - The coefficient of the course.
* @property {number} average
*/
/**
* @typedef {Object} Module
* @property {string} name - The name of the module.
* @property {Array.<Course>} courses - The courses of the module.
* @property {number} average - The average grade of the module.
*/
/**
* @typedef {Object} Semester
* @property {string} name - The name of the semester.
* @property {Array.<Module>} modules - The modules of the semester.
*/
/**
* @typedef {Object} Year
* @property {string} name - The name of the year.
* @property {Array.<Semester>} semesters - The semesters of the year.
*/
(function () {
"use strict";
const resultatsContainerId = "resultat-note";
const resultatsTableId = "table_note";
const yearRowClass = "master";
const semesterAndModuleRowClass = "item-ens";
const semesterStr = ["Semestre"];
const semesterMistakeStr = "Semestre Académique";
const courseRowClass = "item-fpc";
const coursePartClass = "item-ev1";
const nameColumnClass = "libelle";
const resitColumnClass = "rattrapage";
const gradeColumnClass = "note";
const courseCoefficientClass = "ponderation";
const weightColumnClass = "coefficient"; // they inverse it ...
const semesterNumber = 2;
const resultsContainer = document.querySelector(
"#".concat(resultatsContainerId)
);
if (!resultsContainer) {
return;
}
/**
* Extract the number of years in the table
* @param {HTMLTableElement} table
* @returns {number} the number of years in the table
*/
const extractYearsCount = (table) => {
const years = table.querySelectorAll(".".concat(yearRowClass));
return years.length;
};
/**
* Extract the grades from a string
* @param {string} str
* @returns {Array.<Grade>} the extracted grades from str
*/
const extractGrades = (str) => {
let grades = str.split(" - ");
grades = grades.map((grade) => {
const gradeCoefficient = grade.split(" ");
let gradeValue = parseFloat(gradeCoefficient[0]?.replace(",", "."));
let gradeWeight = gradeCoefficient[1];
if (gradeWeight === undefined || gradeWeight === null || gradeWeight === "") {
gradeWeight = 100.0;
} else {
gradeWeight = gradeWeight.replace("(", "");
gradeWeight = gradeWeight.replace(")", "");
gradeWeight = parseFloat(gradeWeight.replace(",", "."));
}
if (isNaN(gradeValue)) {
gradeValue = undefined;
}
return {
value: gradeValue,
weight: gradeWeight,
};
});
return grades;
};
/**
* Extracts a course part from a table row.
*
* @param {HTMLElement} coursePartRow - The table row containing the course part.
* @returns {Object} - The extracted course part object.
*/
const extractCoursePartFromTable = (coursePartRow) => {
let coursePart = {
name: coursePartRow.querySelector(".".concat(nameColumnClass)).innerText,
weight: parseFloat(
coursePartRow
.querySelector(".".concat(weightColumnClass))
.innerText.replace(",", ".")
),
grades: extractGrades(
coursePartRow.querySelector(".".concat(gradeColumnClass)).innerText
),
average: 0
};
return coursePart;
};
/**
* Extracts course information from a course row element.
* @param {HTMLElement} courseRow - The course row element.
* @returns {Object} - The extracted course information.
* @property {string} name - The name of the course.
* @property {number} coefficient - The coefficient of the course.
* @property {number} resit - The resit value of the course.
* @property {Array} courseParts - The array of course parts.
*/
const extractCourseInformation = (courseRow) => {
return {
name: courseRow.querySelector(".".concat(nameColumnClass)).innerText,
coefficient: parseFloat(
courseRow
.querySelector(".".concat(courseCoefficientClass))
.innerText.replace(",", ".")
),
resit: parseFloat(
courseRow.querySelector(".".concat(resitColumnClass)).innerText
),
courseParts: []
};
};
/**
* Extracts the number of courses from the given module rows.
*
* @param {NodeList} moduleRows - The module rows to extract the course number from.
* @returns {number} The number of courses extracted from the module rows.
*/
const extractCourseNumberFromTable = (moduleRows) => {
const rows = Array.from(moduleRows);
let coursesRows = rows.filter((row) =>
row.querySelector(".".concat(courseRowClass))
);
return coursesRows.length;
};
/**
* Extract the course from the module rows
* @param {Array.<HTMLTableRowElement>} moduleRows rows of the table corresponding to the module
* @param {number} i module's index
* @returns {Course} the course extracted from the table
*/
const extractCourseFromTable = (moduleRows, i) => {
const rows = Array.from(moduleRows);
let coursesRows = rows.filter((row) =>
row.querySelector(".".concat(courseRowClass))
);
const courseRowIndex = rows.indexOf(coursesRows[i]);
let nextCourseRowIndex = rows.indexOf(coursesRows[i + 1]);
if (nextCourseRowIndex === -1) {
nextCourseRowIndex = rows.length;
}
const courseRows = rows.slice(courseRowIndex, nextCourseRowIndex);
/**
* @type {Course}
*/
let course = {}
if (courseRows.length >= 1) {
course = extractCourseInformation(courseRows[0]);
}
for (let i = 1; i < courseRows.length; i++) {
let coursePart = extractCoursePartFromTable(courseRows[i]);
course.courseParts.push(coursePart);
}
course.average = 0;
return course;
};
const extractModuleInformation = (moduleRow) => {
return {
name: moduleRow.querySelector(".".concat(nameColumnClass)).innerText,
};
};
const extractModuleNumberFromTable = (semesterRows) => {
const rows = Array.from(semesterRows);
let modulesRows = rows.filter((row) =>
row.querySelector(".".concat(semesterAndModuleRowClass))
);
modulesRows = modulesRows.filter((row) => {
for (let i = 0; i < semesterStr.length; i++) {
if (!row.innerText.includes(semesterStr[i])) {
return true;
}
}
return false;
});
return modulesRows.length;
};
/**
* Extracts module information from a module row.
*
* @param {HTMLElement} moduleRow - The module row element.
* @returns {Object} - The extracted module information.
* @property {string} name - The name of the module. * @property {number} average - The average grade of the module.
*/
const extractModuleFromTable = (semesterRows, i) => {
const rows = Array.from(semesterRows);
let modulesRows = rows.filter((row) =>
row.querySelector(".".concat(semesterAndModuleRowClass))
);
modulesRows = modulesRows.filter((row) => {
for (let i = 0; i < semesterStr.length; i++) {
if (!row.innerText.includes(semesterStr[i])) {
return true;
}
}
return false;
});
const moduleRowIndex = rows.indexOf(modulesRows[i]);
let nextModuleRowIndex = rows.indexOf(modulesRows[i + 1]);
if (nextModuleRowIndex === -1) {
nextModuleRowIndex = rows.length;
}
const moduleRows = rows.slice(moduleRowIndex, nextModuleRowIndex);
const courses = [];
for (let i = 0; i < extractCourseNumberFromTable(moduleRows); i++) {
courses.push(extractCourseFromTable(moduleRows, i));
}
return {
...extractModuleInformation(moduleRows[0]),
courses: courses,
};
};
/**
* Extracts semester information from a given semester row.
*
* @param {HTMLElement} semesterRow - The HTML element representing the semester row.
* @returns {Object} - An object containing the extracted semester information.
* @property {string} name - The name of the semester.
*/
const extractSemesterInformation = (semesterRow) => {
return {
name: semesterRow.querySelector(".".concat(nameColumnClass)).innerText,
};
}
/**
* Extracts the semester information from a table of year rows.
*
* @param {NodeList} yearRows - The list of year rows in the table.
* @param {number} i - The index of the semester row to extract.
* @returns {Object} - The extracted semester information, including modules.
*/
const extractSemesterFromTable = (yearRows, i) => {
const rows = Array.from(yearRows);
const semestersAndModuleRows = rows.filter((row) =>
row.querySelector(".".concat(semesterAndModuleRowClass))
);
let semestersRows = semestersAndModuleRows.filter((row) =>
row.innerText.includes(semesterStr)
);
semestersRows = semestersRows.filter(
(row) => !row.innerText.includes(semesterMistakeStr)
);
const semesterRowIndex = rows.indexOf(semestersRows[i]);
let nextSemesterRowIndex = rows.indexOf(semestersRows[i + 1]);
if (nextSemesterRowIndex === -1) {
nextSemesterRowIndex = rows.length;
}
const semesterRows = rows.slice(semesterRowIndex, nextSemesterRowIndex);
const modules = [];
for (let i = 0; i < extractModuleNumberFromTable(semesterRows); i++) {
const module = extractModuleFromTable(semesterRows, i);
modules.push(module);
}
return {
...extractSemesterInformation(semesterRows[0]),
modules: modules,
}
};
/**
* Extracts year information from a given year row.
* @param {HTMLElement} yearRow - The year row element.
* @returns {Object} - The extracted year information.
*/
const extractYearInformation = (yearRow) => {
return {
name: yearRow.querySelector(".".concat(nameColumnClass)).innerText,
};
}
/**
* Extract all the data for a year
* @param {HTMLAllCollection} tableRows the rows of the table
* @param {number} i the index of the year
*/
const extractYearFromTable = (tableRows, i) => {
const rows = Array.from(tableRows);
const yearsRows = rows.filter((row) =>
row.classList.contains(yearRowClass)
);
const yearRowIndex = rows.indexOf(yearsRows[i]);
const nextYearRowIndex = rows.indexOf(yearsRows[i + 1]);
const yearRows = rows.slice(yearRowIndex, nextYearRowIndex);
const semesters = [];
for (let i = 0; i < semesterNumber; i++) {
semesters.push(extractSemesterFromTable(yearRows, i));
}
return {
...extractYearInformation(yearRows[0]),
semesters: semesters,
};
};
/**
* This functions remove all the part that are stucks between two
* .item-ens that are not a module or doesn't have a name...
* @param {HTMLAllCollection} tableRows the rows of the table
*/
const removeUselessPartsFromTable = (tableRows) => {
const rows = Array.from(tableRows);
const semestersAndModuleRows = rows.filter((row) =>
row.querySelector(".".concat(semesterAndModuleRowClass))
);
for (let i = 0; i < semestersAndModuleRows.length; i++) {
if (semestersAndModuleRows[i].innerText === "") {
// no name module that are a mistake
const rowIndex = rows.indexOf(semestersAndModuleRows[i]);
const nextRowIndex = rows.indexOf(semestersAndModuleRows[i + 1]);
for (let j = rowIndex; j < nextRowIndex; j++) {
tableRows.item(j).remove();
}
}
}
};
resultsContainer.arrive("#".concat(resultatsTableId), (table) => {
console.log("STARTING TO EXTRACT GRADES")
const yearsCount = extractYearsCount(table);
const years = [];
let tableRows = table.querySelectorAll("tr");
removeUselessPartsFromTable(tableRows); // clean the table
tableRows = table.querySelectorAll("tr"); // update the tableRows
for (let i = 0; i < yearsCount; i++) {
const year = extractYearFromTable(tableRows, i);
years.push(year);
}
const json = JSON.stringify(years);
window.localStorage.setItem("grades", json);
console.log("DONE WITH EXTRACTION")
window.dispatchEvent(new Event("extract-grades"));
});
})();