// ==UserScript== // @name Portaledeltrasporto - Full Automation + PDF Download // @namespace http://tampermonkey.net/ // @version 3.0 // @description Ricerca, Procedi e Download automatico PDF rinominati col Telaio // @author Tu // @match *://www.ilportaledeltrasporto.it/nove/gestioneistanzanove/spa/* // @grant none // @run-at document-end // ==/UserScript== (function() { 'use strict'; const TELAIO = 'VR3UPHNE7R5039330'; let downloadEseguiti = false; // Per evitare loop di download infiniti function clickReactSafe(element) { if (!element) return; const opts = { bubbles: true, cancelable: true, view: window }; element.dispatchEvent(new MouseEvent('mousedown', opts)); element.dispatchEvent(new MouseEvent('mouseup', opts)); element.click(); } function setInputReactSafe(input, value) { const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set; input.focus(); setter.call(input, value); input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true })); input.blur(); } function gestisciDownload() { if (downloadEseguiti) return; // Cerchiamo i link che hanno l'icona download (spesso sono dentro bottoni o link con svg) const righeTabella = document.querySelectorAll('tr'); let count = 0; righeTabella.forEach((riga) => { const link = riga.querySelector('a, button[class*="download"]'); if (link && (link.href || link.onclick || riga.querySelector('svg'))) { count++; downloadEseguiti = true; setTimeout(() => { // Prendiamo il nome del documento dalla prima colonna della riga const descrizioneDoc = riga.cells[0]?.innerText.trim().replace(/[/\\?%*:|"<>]/g, '-') || 'Documento'; const nomeFile = `${TELAIO}_${descrizioneDoc}.pdf`; console.log(`[TM] Scarico: ${nomeFile}`); // Se è un link diretto ( con href) if (link.href && link.href !== 'javascript:void(0)') { const a = document.createElement('a'); a.href = link.href; a.download = nomeFile; document.body.appendChild(a); a.click(); document.body.removeChild(a); } else { // Se è un bottone React/Angular che genera il file al volo clickReactSafe(link); } }, count * 1200); // Ritardo progressivo di 1.2s per ogni file } }); } function eseguiAutomazione() { const url = window.location.href; // FASE 1: RICERCA if (url.includes('le-mie-istanze')) { const input = document.getElementById('numeroTelaio'); const btnCerca = Array.from(document.querySelectorAll('button')).find(b => b.textContent.includes('Cerca')); const btnTabella = document.querySelector('button.action-button-table'); if (input && input.value !== TELAIO) { setInputReactSafe(input, TELAIO); setTimeout(() => { if(btnCerca) clickReactSafe(btnCerca); }, 500); } if (btnTabella) clickReactSafe(btnTabella); } // FASE 2: PROCEDI if (url.includes('/abilitazione/') && !url.includes('dettaglio-istanza')) { const btnProcedi = Array.from(document.querySelectorAll('button')).find(b => b.textContent.trim() === 'Procedi'); if (btnProcedi && !btnProcedi.disabled) clickReactSafe(btnProcedi); } // FASE 3: DOWNLOAD NELLA PAGINA DETTAGLIO if (url.includes('dettaglio-istanza')) { gestisciDownload(); } } const observer = new MutationObserver(() => eseguiAutomazione()); observer.observe(document.body, { childList: true, subtree: true }); setTimeout(eseguiAutomazione, 1000); })();