Files
script/Automazione DATI COC.user.js
2026-05-12 21:05:35 +02:00

189 lines
7.5 KiB
JavaScript

// ==UserScript==
// @name Automazione DATI COC
// @namespace http://tampermonkey.net/
// @version 7.0
// @description Polling 2.5s, Nessun refresh dopo upload, Dati persistenti in memoria
// @match *://www.ilportaledellautomobilista.it/immatricolazioni/*
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @connect localhost
// ==/UserScript==
(function() {
'use strict';
// --- CONFIGURAZIONE ---
const SEARCH_URL = "https://www.ilportaledellautomobilista.it/immatricolazioni/certificatoConformitaDatiTecniciVE/Read_initAction.action?pageStatus=SEARCH";
const LOCAL_GET_URL = "https://localhost/hook/dati4.php";
const LOCAL_POST_URL = "https://localhost/hook/ans4.php";
const POLLING_MS = 2500;
const clean = (val) => (val === "." || val === undefined || val === null) ? "" : val;
// --- STILE UI (Allineato a Destra) ---
GM_addStyle(`
#m-container { position: fixed; right: 3cm; top: 5cm; z-index: 9999; display: flex; flex-direction: column; gap: 10px; }
.m-btn { padding: 12px 18px; border: none; border-radius: 5px; color: white; font-weight: bold; cursor: pointer; box-shadow: 2px 2px 5px rgba(0,0,0,0.3); font-family: sans-serif; min-width: 220px; }
#btn-compila { background-color: #ffc107; color: #000; }
#btn-upload { background-color: #dc3545; cursor: not-allowed; opacity: 0.6; }
#btn-upload.active { background-color: #28a745; cursor: pointer; opacity: 1; }
#status-info { background: white; padding: 5px; font-size: 10px; border: 1px solid #ccc; text-align: center; border-radius: 3px; }
`);
// --- CREAZIONE MENU ---
const container = document.createElement('div');
container.id = 'm-container';
const infoBox = document.createElement('div');
infoBox.id = 'status-info';
infoBox.innerText = 'In attesa di dati...';
const btnCompila = document.createElement('button');
btnCompila.className = 'm-btn';
btnCompila.id = 'btn-compila';
btnCompila.innerText = '1. CERCA VEICOLI';
const btnUpload = document.createElement('button');
btnUpload.className = 'm-btn';
btnUpload.id = 'btn-upload';
btnUpload.innerText = '2. ATTESA TABELLA...';
btnUpload.disabled = true;
container.appendChild(infoBox);
container.appendChild(btnCompila);
container.appendChild(btnUpload);
document.body.appendChild(container);
// Aggiorna l'etichetta info se ci sono dati salvati
const aggiornaInfoUI = () => {
const d = GM_getValue("lastTaskData");
if(d) infoBox.innerText = "Dati pronti: " + (d.valore1 || "Caricati");
};
aggiornaInfoUI();
// --- CONTROLLO TABELLA REAL-TIME ---
setInterval(() => {
const table = document.getElementById('listTable');
const rows = table ? table.querySelectorAll('tbody tr') : [];
if (table && rows.length > 0) {
btnUpload.classList.add('active');
btnUpload.disabled = false;
btnUpload.innerText = '2. INVIA DATI (Pronto)';
} else {
btnUpload.classList.remove('active');
btnUpload.disabled = true;
btnUpload.innerText = '2. ATTESA TABELLA...';
}
}, 1000);
// --- FUNZIONE POLLING ---
function startPolling() {
GM_xmlhttpRequest({
method: "GET",
url: LOCAL_GET_URL,
nocache: true,
onload: function(response) {
try {
const data = JSON.parse(response.responseText);
if (data.tipo === "DATI-COC" && data.task === "fare") {
console.log("Nuovo task ricevuto:", data);
// Salviamo i dati (sovrascrivendo i precedenti)
GM_setValue("lastTaskData", data);
aggiornaInfoUI();
// Comunichiamo al server che il task è stato preso in carico
aggiornaStatoTask(data);
// Esecuzione automatica solo se siamo nella pagina corretta
if (window.location.href.includes("pageStatus=SEARCH")) {
eseguiAutomazione(data);
} else {
window.location.href = SEARCH_URL;
}
}
} catch (e) { }
setTimeout(startPolling, POLLING_MS);
},
onerror: () => setTimeout(startPolling, POLLING_MS)
});
}
function aggiornaStatoTask(originalData) {
let updateData = {...originalData};
updateData.task = "COMPLETATO";
GM_xmlhttpRequest({
method: "POST",
url: LOCAL_GET_URL,
headers: { "Content-Type": "application/json" },
data: JSON.stringify(updateData)
});
}
function eseguiAutomazione(params) {
const familyInput = document.querySelector('input[name*="VEFrom.descrizioneFamigliaVeicolo"]');
if (familyInput) {
const inputs = document.querySelectorAll('input');
inputs.forEach(input => {
if (input.name.includes("VEFrom.descrizioneFamigliaVeicolo")) input.value = clean(params.valore1);
if (input.name.includes("VEFrom.descrizioneVarianteVeicolo")) input.value = clean(params.valore2);
if (input.name.includes("VE.codiceOmologazioneEuropea")) input.value = clean(params.valore3);
if (input.name.includes("VE.codiceMarchioVeicolo")) input.value = clean(params.valore4);
if (input.name.includes("progressivoCostruttoreVeicolo")) input.value = clean(params.valore5);
});
const btnSearch = document.getElementById('RicercaCertificatoConformitaDatiTecniciVE_button_value_searchElement');
if (btnSearch) btnSearch.click();
}
}
function validaELeggiTabella() {
const table = document.getElementById('listTable');
if (!table) return;
const rows = table.querySelectorAll('tbody tr');
let records = [];
rows.forEach(row => {
const cells = Array.from(row.querySelectorAll('td'))
.filter(td => !td.querySelector('input[type="radio"]'))
.map(td => td.innerText.trim());
if (cells.length > 0) records.push(cells);
});
GM_xmlhttpRequest({
method: "POST",
url: LOCAL_POST_URL,
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ tabella_dati: records }),
onload: function(response) {
try {
const res = JSON.parse(response.responseText);
if (res.status === "ok") {
// alert("INVIO RIUSCITO: " + res.messaggio);
// NOTA: Non cancelliamo GM_setValue né ricarichiamo la pagina qui.
} else {
alert("ERRORE PHP: " + res.messaggio);
}
} catch (e) {
alert("Dati inviati al server.");
}
}
});
}
btnCompila.addEventListener('click', () => {
const d = GM_getValue("lastTaskData");
if(d) {
eseguiAutomazione(d);
} else {
alert("Nessun dato in memoria. Attendi il caricamento automatico dal server.");
}
});
btnUpload.addEventListener('click', validaELeggiTabella);
startPolling();
})();