Migliorato auto-login e gestione cookie WebView2
- Introdotto il pre-caricamento di WebView2 per ridurre i tempi di attesa. - Implementato il pattern TaskCompletionSource per attendere l'inizializzazione di WebView2 (timeout 60s). - Centralizzata la logica di verifica e importazione automatica dei cookie. - Mostrate istruzioni di login solo se necessario, migliorando l'UX. - Risolti problemi di timeout e threading durante l'inizializzazione di WebView2. - Puliti e ottimizzati i log per maggiore chiarezza. - Rimossa la gestione manuale dei cookie, ora automatizzata.
This commit is contained in:
@@ -17,138 +17,59 @@ namespace AutoBidder
|
||||
{
|
||||
try
|
||||
{
|
||||
var settings = SettingsManager.Load();
|
||||
var settings = Utilities.SettingsManager.Load();
|
||||
|
||||
// === SEZIONE 1: Impostazioni Predefinite Aste ===
|
||||
// Carica impostazioni predefinite aste
|
||||
DefaultBidBeforeDeadlineMs.Text = settings.DefaultBidBeforeDeadlineMs.ToString();
|
||||
DefaultCheckAuctionOpen.IsChecked = settings.DefaultCheckAuctionOpenBeforeBid;
|
||||
DefaultMinPrice.Text = settings.DefaultMinPrice.ToString("F2", System.Globalization.CultureInfo.InvariantCulture);
|
||||
DefaultMaxPrice.Text = settings.DefaultMaxPrice.ToString("F2", System.Globalization.CultureInfo.InvariantCulture);
|
||||
DefaultMaxClicks.Text = settings.DefaultMaxClicks.ToString();
|
||||
|
||||
// === SEZIONE 2: Limiti Log ===
|
||||
Settings.MaxLogLinesPerAuction.Text = settings.MaxLogLinesPerAuction.ToString();
|
||||
Settings.MaxGlobalLogLines.Text = settings.MaxGlobalLogLines.ToString();
|
||||
// Carica limiti log
|
||||
Settings.MaxLogLinesPerAuctionTextBox.Text = settings.MaxLogLinesPerAuction.ToString();
|
||||
Settings.MaxGlobalLogLinesTextBox.Text = settings.MaxGlobalLogLines.ToString();
|
||||
|
||||
// === SEZIONE 3: Stati Iniziali Aste ===
|
||||
var loadAuctionsStopped = Settings.FindName("LoadAuctionsStopped") as System.Windows.Controls.RadioButton;
|
||||
var loadAuctionsPaused = Settings.FindName("LoadAuctionsPaused") as System.Windows.Controls.RadioButton;
|
||||
var loadAuctionsActive = Settings.FindName("LoadAuctionsActive") as System.Windows.Controls.RadioButton;
|
||||
// ? NUOVO: Carica limite minimo puntate
|
||||
MinimumRemainingBidsTextBox.Text = settings.MinimumRemainingBids.ToString();
|
||||
|
||||
if (loadAuctionsStopped != null) loadAuctionsStopped.IsChecked = settings.DefaultStartAuctionsOnLoad == "Stopped";
|
||||
if (loadAuctionsPaused != null) loadAuctionsPaused.IsChecked = settings.DefaultStartAuctionsOnLoad == "Paused";
|
||||
if (loadAuctionsActive != null) loadAuctionsActive.IsChecked = settings.DefaultStartAuctionsOnLoad == "Active";
|
||||
// Aggiorna indicatore visivo
|
||||
UpdateMinBidsIndicator(settings.MinimumRemainingBids);
|
||||
|
||||
var newAuctionStopped = Settings.FindName("NewAuctionStopped") as System.Windows.Controls.RadioButton;
|
||||
var newAuctionPaused = Settings.FindName("NewAuctionPaused") as System.Windows.Controls.RadioButton;
|
||||
var newAuctionActive = Settings.FindName("NewAuctionActive") as System.Windows.Controls.RadioButton;
|
||||
|
||||
if (newAuctionStopped != null) newAuctionStopped.IsChecked = settings.DefaultNewAuctionState == "Stopped";
|
||||
if (newAuctionPaused != null) newAuctionPaused.IsChecked = settings.DefaultNewAuctionState == "Paused";
|
||||
if (newAuctionActive != null) newAuctionActive.IsChecked = settings.DefaultNewAuctionState == "Active";
|
||||
|
||||
// === SEZIONE 4: Cookie (da SessionManager separato) ===
|
||||
var session = Services.SessionManager.LoadSession();
|
||||
if (session != null && !string.IsNullOrEmpty(session.CookieString))
|
||||
// Carica stato iniziale aste
|
||||
switch (settings.DefaultStartAuctionsOnLoad)
|
||||
{
|
||||
SettingsCookieTextBox.Text = session.CookieString;
|
||||
case "Active":
|
||||
Settings.LoadAuctionsActive.IsChecked = true;
|
||||
break;
|
||||
case "Paused":
|
||||
Settings.LoadAuctionsPaused.IsChecked = true;
|
||||
break;
|
||||
case "Stopped":
|
||||
default:
|
||||
Settings.LoadAuctionsStopped.IsChecked = true;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (settings.DefaultNewAuctionState)
|
||||
{
|
||||
case "Active":
|
||||
Settings.NewAuctionActive.IsChecked = true;
|
||||
break;
|
||||
case "Paused":
|
||||
Settings.NewAuctionPaused.IsChecked = true;
|
||||
break;
|
||||
case "Stopped":
|
||||
default:
|
||||
Settings.NewAuctionStopped.IsChecked = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Log($"[OK] Impostazioni caricate: Anticipo={settings.DefaultBidBeforeDeadlineMs}ms, LogAsta={settings.MaxLogLinesPerAuction}, LogGlobale={settings.MaxGlobalLogLines}, MinBids={settings.MinimumRemainingBids}", Utilities.LogLevel.Info);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"[ERRORE] Caricamento impostazioni: {ex.Message}", LogLevel.Error);
|
||||
|
||||
// Valori di fallback
|
||||
DefaultBidBeforeDeadlineMs.Text = "200";
|
||||
DefaultCheckAuctionOpen.IsChecked = false;
|
||||
DefaultMinPrice.Text = "0.00";
|
||||
DefaultMaxPrice.Text = "0.00";
|
||||
DefaultMaxClicks.Text = "0";
|
||||
Settings.MaxLogLinesPerAuction.Text = "500";
|
||||
Settings.MaxGlobalLogLines.Text = "1000";
|
||||
|
||||
var loadAuctionsStopped = Settings.FindName("LoadAuctionsStopped") as System.Windows.Controls.RadioButton;
|
||||
var newAuctionStopped = Settings.FindName("NewAuctionStopped") as System.Windows.Controls.RadioButton;
|
||||
if (loadAuctionsStopped != null) loadAuctionsStopped.IsChecked = true;
|
||||
if (newAuctionStopped != null) newAuctionStopped.IsChecked = true;
|
||||
}
|
||||
}
|
||||
|
||||
private async void SaveCookieButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cookie = SettingsCookieTextBox.Text?.Trim();
|
||||
|
||||
if (string.IsNullOrEmpty(cookie))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// ? NUOVO: Usa SessionService per validare e attivare
|
||||
var result = await _sessionService.ValidateAndActivateSessionAsync(cookie);
|
||||
|
||||
if (result.Success && result.Session != null)
|
||||
{
|
||||
// Salva sessione su disco
|
||||
_sessionService.SaveSession(result.Session);
|
||||
|
||||
StartButton.IsEnabled = true;
|
||||
Log($"[OK] Cookie valido e salvato - Utente: {result.Session.Username}, Puntate: {result.Session.RemainingBids}", LogLevel.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"[ERRORE] {result.ErrorMessage ?? "Cookie non valido o scaduto"}", LogLevel.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"[ERRORE] Salvataggio cookie: {ex.Message}", LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async void ImportCookieFromBrowserButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (EmbeddedWebView?.CoreWebView2 == null)
|
||||
{
|
||||
MessageBox.Show(this, "Browser non inizializzato", "Errore", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
var cookies = await EmbeddedWebView.CoreWebView2.CookieManager.GetCookiesAsync("https://it.bidoo.com");
|
||||
var stattrb = cookies.FirstOrDefault(c => c.Name == "__stattrb");
|
||||
|
||||
if (stattrb != null)
|
||||
{
|
||||
SettingsCookieTextBox.Text = stattrb.Value;
|
||||
Log("[OK] Cookie importato dal browser", LogLevel.Success);
|
||||
MessageBox.Show(this, "Cookie importato con successo!\nClicca 'Salva' per confermare.", "Importa Cookie", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log("[ERRORE] Cookie __stattrb non trovato nel browser", LogLevel.Error);
|
||||
MessageBox.Show(this, "Cookie __stattrb non trovato.\nAssicurati di aver effettuato il login su bidoo.com nella scheda Browser.", "Cookie Non Trovato", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"[ERRORE] Importazione cookie: {ex.Message}", LogLevel.Error);
|
||||
MessageBox.Show(this, "Errore durante importazione cookie: " + ex.Message, "Errore", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelCookieButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var session = Services.SessionManager.LoadSession();
|
||||
if (session != null && !string.IsNullOrEmpty(session.CookieString))
|
||||
{
|
||||
SettingsCookieTextBox.Text = session.CookieString;
|
||||
}
|
||||
else
|
||||
{
|
||||
SettingsCookieTextBox.Text = string.Empty;
|
||||
Log($"[ERRORE] Caricamento impostazioni predefinite: {ex.Message}", Utilities.LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,16 +123,7 @@ namespace AutoBidder
|
||||
// Ricarica impostazioni export
|
||||
LoadExportSettings();
|
||||
|
||||
// Ricarica cookie salvato
|
||||
var session = Services.SessionManager.LoadSession();
|
||||
if (session != null && !string.IsNullOrEmpty(session.CookieString))
|
||||
{
|
||||
SettingsCookieTextBox.Text = session.CookieString;
|
||||
}
|
||||
else
|
||||
{
|
||||
SettingsCookieTextBox.Text = string.Empty;
|
||||
}
|
||||
// NOTA: Reload cookie RIMOSSO - ora automatico tramite browser
|
||||
|
||||
MessageBox.Show(this, "Impostazioni ripristinate alle ultime salvate.", "Annulla", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
@@ -260,7 +172,7 @@ namespace AutoBidder
|
||||
}
|
||||
|
||||
// === SEZIONE DEFAULTS: Limiti Log ===
|
||||
if (int.TryParse(Settings.MaxLogLinesPerAuction.Text, out var maxLogPerAuction) && maxLogPerAuction > 0)
|
||||
if (int.TryParse(Settings.MaxLogLinesPerAuctionTextBox.Text, out var maxLogPerAuction) && maxLogPerAuction > 0)
|
||||
{
|
||||
settings.MaxLogLinesPerAuction = maxLogPerAuction;
|
||||
}
|
||||
@@ -269,7 +181,7 @@ namespace AutoBidder
|
||||
Log("[ERRORE] Valore max log per asta non valido (deve essere > 0)", LogLevel.Error);
|
||||
}
|
||||
|
||||
if (int.TryParse(Settings.MaxGlobalLogLines.Text, out var maxGlobalLog) && maxGlobalLog > 0)
|
||||
if (int.TryParse(Settings.MaxGlobalLogLinesTextBox.Text, out var maxGlobalLog) && maxGlobalLog > 0)
|
||||
{
|
||||
settings.MaxGlobalLogLines = maxGlobalLog;
|
||||
}
|
||||
@@ -278,6 +190,24 @@ namespace AutoBidder
|
||||
Log("[ERRORE] Valore max log globale non valido (deve essere > 0)", LogLevel.Error);
|
||||
}
|
||||
|
||||
// ? NUOVO: Salva limite minimo puntate
|
||||
if (int.TryParse(MinimumRemainingBidsTextBox.Text, out var minBids) && minBids >= 0)
|
||||
{
|
||||
settings.MinimumRemainingBids = minBids;
|
||||
|
||||
// Aggiorna indicatore visivo
|
||||
UpdateMinBidsIndicator(minBids);
|
||||
|
||||
if (minBids > 0)
|
||||
{
|
||||
Log($"[LIMIT] Impostato limite minimo puntate: {minBids}", LogLevel.Info);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log("[ERRORE] Valore limite minimo puntate non valido (deve essere >= 0)", LogLevel.Error);
|
||||
}
|
||||
|
||||
// === SEZIONE DEFAULTS: Stati Iniziali Aste ===
|
||||
var loadAuctionsActive = Settings.FindName("LoadAuctionsActive") as System.Windows.Controls.RadioButton;
|
||||
var loadAuctionsPaused = Settings.FindName("LoadAuctionsPaused") as System.Windows.Controls.RadioButton;
|
||||
|
||||
Reference in New Issue
Block a user