(async function() {
if (!window.location.href.includes(“/FattureWeb/fatture/elenco”)) {
window.location.href = “https://serviziae.agenziaentrate.gov.it/FattureWeb/fatture/elenco”;
return;
}
let debugReport = [];
function creaPannelloControllo() {
const pannello = document.createElement("div");
pannello.id = "pannelloMassDownload";
pannello.style = `
position: fixed; top: 20px; right: 20px;
background: #fff; border: 2px solid #007bff; border-radius: 8px;
padding: 15px; box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
z-index: 9999; font-family: sans-serif;
`;
pannello.innerHTML = `
<h4 style="margin:0 0 10px;color:#007bff;">Download Massivo</h4>
<label><input type="checkbox" id="selezionaTutto"> Seleziona tutte</label><br><br>
<label>Secondi tra download: <input id="inputDelay" type="number" value="1" style="width: 50px;"></label><br><br>
<label><input type="checkbox" id="scaricaMetadati"> Scarica anche Metadati</label><br><br>
<progress id="progressBar" value="0" max="100" style="width: 100%;"></progress><br><br>
<button id="btnMassDownload" style="padding:8px 16px; background:#007bff; color:white; border:none; border-radius:5px; cursor:pointer;">
Avvia Download
</button>
<div id="logMassDownload" style="margin-top:10px; max-height:150px; overflow:auto; font-size:12px;"></div>
`;
document.body.appendChild(pannello);
document.getElementById("selezionaTutto").addEventListener("change", toggleSelezioneTutte);
document.getElementById("btnMassDownload").addEventListener("click", avviaDownloadMassivo);
}
function aggiungiCheckboxAlleFatture() {
const righe = document.querySelectorAll('table tbody tr');
righe.forEach(riga => {
if (!riga.querySelector(".checkboxFattura")) {
const cella = document.createElement("td");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "checkboxFattura";
cella.appendChild(checkbox);
riga.insertBefore(cella, riga.firstChild);
}
});
const intestazione = document.querySelector('table thead tr');
if (intestazione && !document.getElementById("checkboxIntestazione")) {
const cellaIntestazione = document.createElement("th");
cellaIntestazione.innerHTML = "✔️";
cellaIntestazione.id = "checkboxIntestazione";
intestazione.insertBefore(cellaIntestazione, intestazione.firstChild);
}
}
function toggleSelezioneTutte() {
const checked = document.getElementById("selezionaTutto").checked;
document.querySelectorAll(".checkboxFattura").forEach(checkbox => {
checkbox.checked = checked;
});
}
function log(messaggio, colore = "black") {
const logDiv = document.getElementById("logMassDownload");
const p = document.createElement("div");
p.textContent = `[${new Date().toLocaleTimeString()}] ${messaggio}`;
p.style.color = colore;
logDiv.appendChild(p);
logDiv.scrollTop = logDiv.scrollHeight;
}
async function avviaDownloadMassivo() {
const delaySec = parseInt(document.getElementById("inputDelay").value) || 1;
const scaricaMetadati = document.getElementById("scaricaMetadati").checked;
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const links = Array.from(document.querySelectorAll('a[href^="../fatture/dettaglio/"]'));
const selezionati = Array.from(document.querySelectorAll(".checkboxFattura"))
.map((cb, idx) => cb.checked ? idx : -1)
.filter(idx => idx !== -1);
if (selezionati.length === 0) {
log("Nessuna fattura selezionata.", "red");
return;
}
log(`Inizio download di ${selezionati.length} fatture...`, "green");
const progressBar = document.getElementById("progressBar");
for (let i = 0; i < selezionati.length; i++) {
const idx = selezionati[i];
const link = links[idx];
if (!link) continue;
const href = link.getAttribute("href");
const fatturaId = href.split("/").pop();
const dettaglioUrl = `https://serviziae.agenziaentrate.gov.it/FattureWeb/fatture/dettaglio/${fatturaId}`;
log(`Apro iframe per: ${fatturaId}`, "black");
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = dettaglioUrl;
document.body.appendChild(iframe);
await new Promise(resolve => { iframe.onload = resolve; });
try {
await delay(1000);
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
const downloadBtn = iframeDoc.querySelector('button[title^="download file fattura"]');
if (downloadBtn) {
downloadBtn.click();
log(`Download avviato: Fattura ${fatturaId}`, "green");
} else {
log(`⚠️ Nessun bottone download trovato: ${fatturaId}`, "red");
}
if (scaricaMetadati) {
const downloadMetadatiBtn = iframeDoc.querySelector('button[title^="download meta-dati"]');
if (downloadMetadatiBtn) {
downloadMetadatiBtn.click();
log(`Download meta-dati avviato: ${fatturaId}_metadato.xml`, "blue");
} else {
log(`⚠️ Nessun bottone download meta-dati trovato: ${fatturaId}`, "red");
}
}
} catch (e) {
console.error("Errore iframe:", e);
log(`❌ Errore su: ${fatturaId}`, "red");
}
progressBar.value = ((i + 1) / selezionati.length) * 100;
await delay(delaySec * 1000);
iframe.remove();
}
log("✅ Download massivo completato.", "green");
console.table(debugReport);
}
creaPannelloControllo();
aggiungiCheckboxAlleFatture();
})();