-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
136 lines (98 loc) · 3.71 KB
/
background.js
File metadata and controls
136 lines (98 loc) · 3.71 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
// Seleccionamos la tabla
const tabla = document.getElementsByClassName("upv_listacolumnas")[0];
const thead = tabla.getElementsByTagName("thead")[0];
const tbody = tabla.getElementsByTagName("tbody")[0];
var filas = tbody.rows;
var estadisticas = { media: 0, aprobados: 0, suspendidos: 0 };
var listaSeguimiento = [""];
// Cargamos la lista de seguimiento
chrome.storage.sync.get(['listaSeguimiento'], function (result) {
listaSeguimiento = result.listaSeguimiento;
colorearListaSeguimiento();
});
// Creamos un listener para actualizar los cambios
chrome.storage.onChanged.addListener((changes, namespace) => {
console.log("coloreando");
listaSeguimiento = changes["listaSeguimiento"]["newValue"];
colorearListaSeguimiento();
});
// Ordenamos la tabla
thead.rows[0].insertCell(0).outerHTML = "<th></th>";
thead.rows[0].getElementsByTagName("th")[1].onclick = function () { ordenarTabla(1); };
thead.rows[0].getElementsByTagName("th")[2].onclick = function () { ordenarTabla(2); };
// Por defecto la tabla permanecerá ordenada por nota
ordenarTabla (2)
// Calculamos las estadísticas
let acumulador = 0;
for (i = 0; i < (filas.length); i++) {
let nota = Number(filas[i].getElementsByTagName("TD")[2].innerHTML);
acumulador += nota;
if (nota >= 5) { estadisticas['aprobados']++ } else { estadisticas['suspendidos']++ } // Actualizamos las estadisticas
}
estadisticas['media'] = acumulador / filas.length;
// Mostramos las estadísticas
const container = document.getElementsByClassName('upv_containerwrap')[0];
var texto = 'Media: ' + estadisticas['media'].toFixed(3).toString() + '<br>Aprobados: ' + estadisticas['aprobados'].toString() + '<br>Suspensos: ' + estadisticas['suspendidos'].toString();
var parrafo = document.createElement("p");
parrafo.innerHTML = texto;
container.prepend(parrafo);
function ordenarTabla (n) {
// Ordenamos la tabla
let direccion = "desc";
let ordenada = true;
if (filas[0].getElementsByTagName("td").length == 2){
for (let i = 0; i < filas.length; i++) {
filas[i].insertCell(0).innerHTML = i+1;
let columnas = filas[i].getElementsByTagName("td");
columnas[2].innerHTML = Number(columnas[2].innerHTML.replace(',', '.'));
}
}
for (let i = 0; i < filas.length; i++) {
for (let j = 0; j < filas.length - i - 1; j++) {
let x = filas[j].getElementsByTagName("TD")[n];
let y = filas[j + 1].getElementsByTagName("TD")[n];
//Ordenamos por nombre
if (n == 1) {
a = x.innerHTML.toLowerCase();
b = y.innerHTML.toLowerCase();
}
// Ordenamos por nota
else if (n == 2) {
a = Number(x.innerHTML);
b = Number(y.innerHTML);
}
if (direccion == "asc" && a > b) {
filas[j].parentNode.insertBefore(filas[j + 1], filas[j]);
ordenada = false;
} else if (direccion == "desc" && a < b) {
filas[j].parentNode.insertBefore(filas[j + 1], filas[j]);
ordenada = false;
} else if (ordenada && direccion == "desc" && i == 1 ){
direccion = "asc";
i = 0;
ordenada = true;
}
}
}
// Añadimos la posicion y coloreamos corectamente
for (i = 0; i < (filas.length); i++) {
filas[i].getElementsByTagName("TD")[0].innerHTML = i+1;
filas[i].style.backgroundColor = (i % 2) ? 'white' : '#F0F0F0';
}
// Coloreamos usuario en la lista de seguimiento
colorearListaSeguimiento();
}
function colorearListaSeguimiento () {
for (i = 1; i < (filas.length); i++){
filas[i].style.backgroundColor = (i % 2) ? 'white' : '#F0F0F0';
filas[i].style.color = '#818181';
// Vemos si el nombre esta en la lista de seguimiento
for (elemento of listaSeguimiento){
if (filas[i].getElementsByTagName("TD")[1].innerHTML == elemento.nombre){
filas[i].style.backgroundColor = elemento.color;
filas[i].style.color = 'black';
break;
}
}
}
}