-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
71 lines (62 loc) · 2.43 KB
/
content.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
(async function () {
let data = JSON.parse(localStorage.getItem("senasaData")) || [];
let currentPage = parseInt(localStorage.getItem("senasaCurrentPage")) || 1;
const localStorageKey = "senasaData";
function extractTableData() {
const rows = document.querySelectorAll("#formulario\\:datos1 tbody tr");
return Array.from(rows).map((row) => {
const cells = row.querySelectorAll("td");
return {
certificado: cells[0]?.innerText.trim(),
nombreComercial: cells[1]?.innerText.trim(),
nombreFirma: cells[2]?.innerText.trim(),
propio: cells[3]?.innerText.trim(),
};
});
}
function saveToLocalStorage(data) {
const existingData = JSON.parse(localStorage.getItem(localStorageKey)) || [];
const combinedData = existingData.concat(data);
localStorage.setItem(localStorageKey, JSON.stringify(combinedData));
}
async function goToNextPage() {
const nextPageButton = document.querySelector("#formulario\\:datos1\\:nextPage");
if (nextPageButton) {
localStorage.setItem("senasaCurrentPage", currentPage + 1);
nextPageButton.click();
return true;
}
return false;
}
function downloadCSV() {
const data = JSON.parse(localStorage.getItem(localStorageKey)) || [];
const csvContent = [
["Certificado", "Nombre Comercial", "Nombre Firma", "Propio"].join(","),
...data.map((row) =>
[row.certificado, row.nombreComercial, row.nombreFirma, row.propio].join(",")
),
].join("\n");
const blob = new Blob([csvContent], { type: "text/csv" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "senasa_data.csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Scraping automatizado de todas las páginas
async function scrapePages() {
const pageData = extractTableData();
saveToLocalStorage(pageData);
const hasNextPage = await goToNextPage();
if (!hasNextPage) {
alert("Análisis completado. Descargando CSV...");
downloadCSV();
localStorage.removeItem("senasaData");
localStorage.removeItem("senasaCurrentPage");
}
}
if (window.location.href.includes("buscadorConsultaPublicaInternetProductosCOFIAL")) {
await scrapePages();
}
})();