-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.js
More file actions
118 lines (104 loc) · 3.32 KB
/
Copy pathextractor.js
File metadata and controls
118 lines (104 loc) · 3.32 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
// Extractor v13.0 - Soporte para Bajas e Incidencias
console.log("Urgencias Pro: Extractor v13.0 activado");
async function extraerDatos() {
console.log("Iniciando extracción...");
const listaFinal = new Map();
const scrollContainer = document.scrollingElement || document.documentElement;
let ultimaAltura = 0;
let intentosSinNuevos = 0;
function limpiarNombre(n) {
if (!n) return "";
let partes = n
.split("\n")
.map((p) => p.trim())
.filter((p) => p.length >= 2);
let limpio = partes[partes.length - 1] || "";
let palabras = limpio.split(" ");
if (
palabras.length > 2 &&
palabras[0].length <= 2 &&
/^[A-Z0-9]+$/.test(palabras[0])
) {
palabras.shift();
limpio = palabras.join(" ");
}
return limpio;
}
function esNombreValido(n) {
if (!n || n.length < 5) return false;
if (/\d{1,2}:\d{2}/.test(n)) return false;
const descartar = ["TOTAL", "HORAS", "TURNO", "LEYENDA", "DÍAS"];
if (descartar.some((d) => n.toUpperCase().includes(d))) return false;
return /[a-zA-Z]/.test(n);
}
function capturarVisibles() {
const filas = Array.from(document.querySelectorAll("tr"));
let encontrados = 0;
filas.forEach((fila) => {
const celdas = Array.from(fila.querySelectorAll("td, th"));
if (celdas.length < 15) return;
let nombreRaw = "";
let colNombre = 0;
for (let i = 0; i < 3; i++) {
if (celdas[i] && esNombreValido(celdas[i].innerText.trim())) {
nombreRaw = celdas[i].innerText.trim();
colNombre = i;
break;
}
}
if (!nombreRaw) return;
let nombreLimpio = limpiarNombre(nombreRaw);
if (!listaFinal.has(nombreLimpio)) {
// IMPORTANTE: No quitamos los paréntesis aquí para que popup.js detecte las bajas
const turnos = celdas.slice(colNombre + 1, colNombre + 32).map((c) => {
return c.innerText.trim().split("\n")[0];
});
listaFinal.set(nombreLimpio, {
nombre: nombreLimpio,
turnos: turnos,
});
encontrados++;
}
});
return encontrados;
}
while (intentosSinNuevos < 4) {
const nuevos = capturarVisibles();
window.scrollBy(0, 800);
await new Promise((r) => setTimeout(r, 800));
let alturaActual = scrollContainer.scrollTop;
if (alturaActual === ultimaAltura) {
intentosSinNuevos++;
} else {
intentosSinNuevos = 0;
ultimaAltura = alturaActual;
}
if (listaFinal.size > 150) break;
}
window.scrollTo(0, 0);
// Extraer mes del th con colspan="31" y año de data-date
let mesTexto = "";
const thMes = document.querySelector('th[colspan="31"]');
if (thMes) {
const nombreMes = thMes.innerText.trim();
const dateEl = document.querySelector("[data-date]");
const anio = dateEl
? dateEl.dataset.date.substring(0, 4)
: new Date().getFullYear();
mesTexto = `${nombreMes.toUpperCase()} ${anio}`;
}
if (!mesTexto) mesTexto = document.title;
return {
exito: true,
personal: Array.from(listaFinal.values()),
mes: mesTexto,
};
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "get_data") {
extraerDatos()
.then(sendResponse)
.catch((err) => sendResponse({ error: err.message }));
}
return true;
});