// ==UserScript== // @name Estrattore Tabella Portale Trasporto - Fixed // @namespace http://tampermonkey.net/ // @version 3.2 // @description Risolto loop infinito su tabella vuota // @author Gemini // @match https://www.ilportaledeltrasporto.it/nove/gestioneistanzanove/spa/le-mie-istanze* // @grant GM_xmlhttpRequest // @connect 192.168.1.33 // ==/UserScript== (function() { 'use strict'; const CONFIG = { commandEndpoint: 'http://192.168.1.33/hook/dati3.php', answerEndpoint: 'http://192.168.1.33/hook/ans3.php', pollingInterval: 1000, searchDelay: 1500 }; function checkCommand() { GM_xmlhttpRequest({ method: "GET", url: CONFIG.commandEndpoint, headers: { "Cache-Control": "no-cache" }, onload: function(response) { try { const res = JSON.parse(response.responseText); if (res.tipo === "DATI_NRAP" && res.task === "fare") { console.log("%c [COMANDO] Ricevuto 'fare'.", "color: #FFA500; font-weight: bold;"); eseguiCiclo(); } else { setTimeout(checkCommand, CONFIG.pollingInterval); } } catch (e) { setTimeout(checkCommand, CONFIG.pollingInterval); } }, onerror: () => setTimeout(checkCommand, CONFIG.pollingInterval) }); } function eseguiCiclo() { const buttons = Array.from(document.querySelectorAll('button')); const searchButton = buttons.find(btn => btn.innerText.trim().toUpperCase() === "CERCA"); if (searchButton) { console.log("[ACTION] Click Cerca..."); searchButton.click(); setTimeout(estraiEInvia, CONFIG.searchDelay); } else { console.error("[ERRORE] Tasto Cerca non trovato."); // Resettiamo comunque per evitare loop se il tasto sparisce impostaStatoFinale("ERRORE_BOTTONE"); } } function estraiEInvia() { const rows = document.querySelectorAll('[role="row"]'); const results = []; rows.forEach(row => { const cells = row.querySelectorAll('[role="cell"]'); if (cells.length >= 5) { const idIstanza = cells[0].innerText.trim(); if (idIstanza !== "" && idIstanza.toLowerCase() !== "identificativo istanza") { results.push({ identificativo: idIstanza, data_inserimento: cells[1]?.innerText.trim(), stato: cells[2]?.innerText.trim(), tipologia: cells[3]?.innerText.trim(), targa_telaio: cells[4]?.innerText.trim() }); } } }); if (results.length > 0) { console.log(`[DATA] Invio ${results.length} record...`); GM_xmlhttpRequest({ method: "POST", url: CONFIG.answerEndpoint, headers: { "Content-Type": "application/json" }, data: JSON.stringify(results), onload: () => { console.log("%c [SUCCESS] Dati inviati.", "color: #00FF00;"); impostaStatoFinale("COMPLETATO"); }, onerror: () => setTimeout(checkCommand, CONFIG.pollingInterval) }); } else { // MODIFICA CRUCIALE: Se la tabella è vuota, impostiamo comunque COMPLETATO // per fermare il loop, indicando che non ci sono record. console.warn("[WARN] Tabella vuota. Fermo il comando."); impostaStatoFinale("COMPLETATO_VUOTO"); } } function impostaStatoFinale(nuovoStato) { GM_xmlhttpRequest({ method: "POST", url: CONFIG.commandEndpoint, headers: { "Content-Type": "application/json" }, data: JSON.stringify({ tipo: "DATI_NRAP", task: nuovoStato, // Sarà COMPLETATO, COMPLETATO_VUOTO o ERRORE valore1: "0", valore2: "0", valore3: "0" }), onload: function() { console.log(`%c [STATUS] Comando chiuso con stato: ${nuovoStato}`, "color: #FFF; background: #333;"); setTimeout(checkCommand, CONFIG.pollingInterval); } }); } checkCommand(); })();