@@ -26,38 +26,29 @@ <h1 id="welcome-message">Benvenuto!</h1>
2626 const name = localStorage . getItem ( 'name' ) ;
2727 const email = localStorage . getItem ( 'email' ) ;
2828
29- // UI: Nome
3029 const displayName = name || 'Utente' ;
3130 document . getElementById ( 'welcome-message' ) . innerText = `Benvenuto, ${ displayName } !` ;
3231
33- // UI: Email
3432 if ( email ) {
3533 document . getElementById ( 'email-line' ) . innerText = `Email: ${ email } ` ;
3634 } else {
3735 document . getElementById ( 'email-line' ) . innerText = `Email non disponibile` ;
3836 }
3937
40- // UI: Gestione Click Bottone
4138 const btnChat = document . getElementById ( 'btn-open-chat' ) ;
4239
4340 btnChat . onclick = function ( ) {
4441 if ( btnChat . classList . contains ( 'disabled' ) ) {
4542 console . warn ( "Chat non ancora pronta." ) ;
4643 return ;
4744 }
48-
4945 console . log ( "Tentativo di apertura chat..." ) ;
50- // Avvia la chat
5146 embeddedservice_bootstrap . utilAPI . launchChat ( )
52- . then ( ( ) => console . log ( "Chat aperta." ) )
5347 . catch ( ( e ) => console . error ( "Errore apertura chat:" , e ) ) ;
5448 } ;
5549 </ script >
5650
5751 < script type ='text/javascript '>
58- // Variabile globale per capire quando è pronto
59- window . isSalesforceReady = false ;
60-
6152 function initEmbeddedMessaging ( ) {
6253 console . log ( "--> INIZIO INIT SALESFORCE <--" ) ;
6354 try {
@@ -66,37 +57,41 @@ <h1 id="welcome-message">Benvenuto!</h1>
6657
6758 embeddedservice_bootstrap . settings . language = 'it' ;
6859
69- // Configurazione (con il nome corretto che mi hai dato prima)
60+ // ASCOLTATORE DI EVENTI:
61+ // Invece di usare .then(), diciamo al browser di aspettare questo segnale
62+ window . addEventListener ( "onEmbeddedMessagingReady" , ( ) => {
63+ console . log ( "2. EVENTO RICEVUTO: Messaging Ready!" ) ;
64+
65+ // Ora siamo sicuri che l'API esiste
66+ try {
67+ embeddedservice_bootstrap . prechatAPI . setHiddenPrechatFields ( {
68+ "Login_Email_Consulente" : userEmail
69+ } ) ;
70+ console . log ( "3. Parametri nascosti impostati." ) ;
71+ } catch ( e ) {
72+ console . error ( "Errore nel settare i campi nascosti:" , e ) ;
73+ }
74+
75+ // Abilitiamo il bottone
76+ const btn = document . getElementById ( 'btn-open-chat' ) ;
77+ btn . innerText = "Parla con l'Agente" ;
78+ btn . classList . remove ( 'disabled' ) ;
79+ } ) ;
80+
81+ // INIZIALIZZAZIONE (Senza .then)
7082 embeddedservice_bootstrap . init (
7183 '00DfZ000000Aod3' ,
7284 'Case_Support_Agent_With_variable' ,
7385 'https://chogangroup--devai1.sandbox.my.site.com/ESWCaseSupportAgentTes1764164304917' ,
7486 {
7587 scrt2URL : 'https://chogangroup--devai1.sandbox.my.salesforce-scrt.com'
7688 }
77- ) . then ( ( ) => {
78- // SUCCESSO!
79- console . log ( "2. Init completata con successo." ) ;
80-
81- // Passaggio parametri nascosti
82- embeddedservice_bootstrap . prechatAPI . setHiddenPrechatFields ( {
83- "Login_Email_Consulente" : userEmail
84- } ) ;
85- console . log ( "3. Parametri nascosti impostati." ) ;
86-
87- // Abilitiamo il bottone
88- const btn = document . getElementById ( 'btn-open-chat' ) ;
89- btn . innerText = "Premi per parlare con l'Agente" ;
90- btn . classList . remove ( 'disabled' ) ;
91- window . isSalesforceReady = true ;
92-
93- } ) . catch ( error => {
94- console . error ( "ERRORE FATALE INIT:" , error ) ;
95- document . getElementById ( 'btn-open-chat' ) . innerText = "Errore Chat" ;
96- } ) ;
89+ ) ;
90+ // Se arriviamo qui senza errori, l'init è partita.
91+ // Ora aspettiamo l'evento "onEmbeddedMessagingReady" definito sopra.
9792
9893 } catch ( err ) {
99- console . error ( 'Eccezione nello script init: ' , err ) ;
94+ console . error ( 'Eccezione nello script di init: ' , err ) ;
10095 }
10196 } ;
10297 </ script >
0 commit comments