-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
365 lines (336 loc) · 10.2 KB
/
script.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
const asciiArtLogo = "\n" +
" __ __ ______ _ _ _ ______ _ \n" +
" | \\/ | | ____| (_| | | | | ____| | | \n" +
" | \\ / |_ _| |__ _ __ _| |_ ___ ___| |__ | |__ __ _| |_ \n" +
" | |\\/| | | | | __| | '_ \\| | __/ _ \\/ __| '_ \\| __| \\ \\/ | __|\n" +
" | | | | |_| | |____| |_) | | || __| (__| | | | |____ > <| |_ \n" +
" |_| |_|\\__, |______| .__/|_|\\__\\___|\\___|_| |_|______/_/\\_\\\\__|\n" +
" __/ | | | \n" +
" |___/ |_| \n\n"
const baseUrl = "https://api.epitest.eu/me/";
const projectName = "MyEpitechExt";
const types = {
YEAR: "year",
MODULE: "module",
PROJECT: "project",
DETAILS: "details",
UNKNOWN: "unknown"
}
let type = null;
let hasBeenFixed = false; //used for blocking multiple override
let elements = []; //all elements added
/**
* Function to print on the console
* @param msg the message
* @param type the type of log
*/
const epiLog = (msg, type = "debug") => {
switch (type) {
case "debug":
console.debug("[%s] %s", projectName, msg);
break;
case "info":
console.info("[%s] %s", projectName, msg);
break;
case "error":
console.error("[%s] %s", projectName, msg);
break;
case "warn":
console.warn("[%s] %s", projectName, msg);
break;
case "json":
console.debug("[%s] Json Debug", projectName, msg);
break;
case "timeStart":
console.time(`[${projectName}] ${msg}`);
break;
case "timeEnd":
console.timeEnd(`[${projectName}] ${msg}`);
break;
}
}
/**
* Display MyEpitechExt logo
* @returns {void}
*/
const printLogo = () => {
console.log(asciiArtLogo);
}
/**
* Retrieve json from url
* @param url the url
* @returns {Promise<any>} the json or an error
*/
let fetchData = async (url) => {
let data;
try {
epiLog("Fetching data from " + url);
const result = fetch(url, {
method: "GET",
headers: {
'Content-Type': 'applications/json',
'Authorization': 'Bearer ' + localStorage.getItem("argos-api.oidc-token").replace(/"/g, "")
}
});
data = (await result).json();
} catch (e) {
epiLog(e, "error");
throw "Request error " + url;
}
return data;
}
/**
* Get the type (Year, Module, Project, Details)
* @returns {Promise<string>}
*/
const getType = () => {
let windowUrl = window.location.href;
if (windowUrl.includes("#y"))
return types.YEAR;
else if (windowUrl.includes("#m"))
return types.MODULE;
else if (windowUrl.includes("#p"))
return types.PROJECT;
else if (windowUrl.includes("#d"))
return types.DETAILS;
return types.UNKNOWN;
}
/**
* Get the type schema
* @returns {string}
*/
const getTypeSchema = () => {
let windowUrl = window.location.href;
switch (type) {
case "year":
return windowUrl.split("#y/")[1];
case "module":
return windowUrl.split("#m/")[1].slice(0, 4);
case "project":
return windowUrl.split("#p/")[1];
case "details":
let splitted = windowUrl.split("/");
return "details/" + splitted[splitted.length - 1];
}
}
/**
* Retrieve the json from the api (filtered or not)
* @returns {Promise<*>}
*/
const retrieveData = async () => {
let typeSchema = getTypeSchema();
let projects = await fetchData(baseUrl + typeSchema);
let windowUrl = window.location.href;
if (type === types.MODULE) {
let splitted = windowUrl.split("/");
let module = splitted[splitted.length - 1];
projects = await projects.filter(project => project.project.module.code === module);
}
return projects;
}
/**
* Get the percentage value
* @param project the project
* @returns {string} the percentage
*/
const getPercentage = (project) => {
let total_test = 0;
let passed_test = 0;
let skills = type === types.DETAILS ? project.skills : project.results.skills;
if (type !== types.DETAILS) {
for (let task in skills) {
if (skills.hasOwnProperty(task)) {
total_test += skills[task].count;
passed_test += skills[task].passed;
}
}
} else {
for (let skill of skills) {
if (skill.BreakdownSkillReport === undefined) {
total_test += skill.FullSkillReport.tests.length;
for (let test of skill.FullSkillReport.tests)
passed_test += test.passed;
} else {
total_test += skill.BreakdownSkillReport.breakdown.count;
passed_test += skill.BreakdownSkillReport.breakdown.passed;
}
}
}
let percentage = (passed_test / total_test * 100).toFixed(1);
let splitted = percentage.split(".");
if (percentage.includes(".") && splitted[1] === "0")
return splitted[0];
return percentage;
}
/**
* Check if the mdl-grid exist
* @returns {boolean}
*/
const checkGrid = () => {
return document.querySelectorAll("main > div.mdl-grid").length === 1;
}
/**
* Check if the main layout content equals to <strong>No results</strong>
* @returns {boolean}
*/
const checkNoResult = () => {
let mainGrid = document.querySelector("#elm-mdl-layout-main > div:nth-child(2) > div");
if (mainGrid === undefined || mainGrid === null)
return false;
return mainGrid?.innerHTML === "No results";
}
/**
* A simple sleep function
* @param ms time to sleep
* @returns {Promise<void>}
*/
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
/**
* Get the color from percentage
* @param percentage the percentage
* @returns {string} the color in string
*/
const getColor = (percentage) => {
if (percentage < 25)
return "red";
else if (percentage < 75)
return "orange"
else
return "limegreen";
}
/**
* Return a progress bar
* @param name name of the project for debug
* @param percentage the percentage
* @returns {HTMLDivElement} simple progress bar taken from mdl :)
*/
const getProgressBar = (name, percentage) => {
epiLog(name + " " + percentage + "%");
let div = document.createElement("div");
div.className = "mdl-progress";
div.style.height = "20px";
div.style.margin = "5px auto";
let text = document.createElement("div");
text.className = "bar";
text.innerHTML = percentage + "%";
text.style.color = "black";
text.style.zIndex = "2";
text.style.width = "100%";
text.style.fontSize = "smaller";
text.style.margin = "2px";
let fgDiv = document.createElement("div");
fgDiv.className = "bar";
fgDiv.style.width = percentage + "%";
fgDiv.style.backgroundColor = getColor(percentage);
fgDiv.style.zIndex = "1";
fgDiv.style.borderRadius = "10px";
let bgDiv = document.createElement("div");
bgDiv.className = "bar";
bgDiv.style.backgroundColor = getColor(percentage);
bgDiv.style.zIndex = "0";
bgDiv.style.borderRadius = "10px";
bgDiv.style.opacity = "30%";
bgDiv.style.width = "100%";
div.appendChild(text);
div.appendChild(fgDiv);
div.appendChild(bgDiv);
return div;
}
/**
* Fix the year, module and project page
* @param projects all the projects
* @returns {void}
*/
const fixPercentage = (projects) => {
let mdlGridSelector = document.querySelectorAll("main > div.mdl-grid");
if (mdlGridSelector.length === 0) {
epiLog("Mdl grid is empty !", "warn");
return;
}
let mdlGrid = mdlGridSelector[0];
if (mdlGrid.childNodes.length !== projects.length) {
epiLog("Card project are empty !", "warn");
return;
}
for (let i = 0; i < mdlGrid.childNodes.length; i++) {
let percentage = getPercentage(projects[i]);
let div = getProgressBar(projects[i].project.name, percentage);
let textTitle = mdlGrid.childNodes[i]?.childNodes[0]?.childNodes[1]?.childNodes[0]?.childNodes[0];
textTitle.appendChild(div);
elements.push(div);
}
}
/**
* Fix the details page
* @param project the project
* @returns {void}
*/
const fixDetails = (project) => {
let mdlGridSelector = document.querySelectorAll("main > div.mdl-grid");
if (mdlGridSelector.length === 0) {
epiLog("Mdl grid is empty !", "warn");
return;
}
let mdlGrid = mdlGridSelector[0];
let percentage = getPercentage(project);
let div = getProgressBar(project.instance.projectName, percentage);
let textTitle = mdlGrid.childNodes[0]?.childNodes[1]?.childNodes[0];
textTitle.appendChild(div);
elements.push(div);
}
/**
* Fix my epitech
* @returns {Promise<void>}
*/
const fixMyEpitech = async () => {
epiLog("Finish patching my.epitech.eu", "timeStart");
while (!checkGrid()) {
if (checkNoResult()) {
epiLog("No results found !");
return;
}
await sleep(150);
}
hasBeenFixed = true;
type = getType();
epiLog("Fetching data of projects [Type=" + type + "]");
try {
let projects = await retrieveData();
projects = type !== types.DETAILS ? projects.reverse() : projects;
epiLog(projects, "json");
if (type !== types.DETAILS)
fixPercentage(projects);
else
fixDetails(projects);
} catch (e) {
epiLog(e, "error");
}
hasBeenFixed = false;
epiLog("Finish patching my.epitech.eu", "timeEnd");
}
/**
* The main
* @returns {Promise<void>}
*/
const main = async () => {
if (window.location.href.includes("#")) {
epiLog("Loading MyEpitech extension...");
printLogo();
await fixMyEpitech();
}
}
/**
* Update page on url change
*/
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.message === "refresh" && !hasBeenFixed) {
elements.forEach(elem => elem.remove()); //clear elements added, can cause bug if they are not remove
elements = [];
await fixMyEpitech();
}
})
window.onload = main;