public static async Task <decimal> ValidateAmazonAsync(this IWebView view, string username, string password, string claimcode, CancellationToken ct, int delayMS) { Application.Current.MainWindow.Dispatcher.Invoke(() => { ((MainWindow)Application.Current.MainWindow).browserPlaceHolder.GetChildOfType <System.Windows.Controls.Canvas>().Background = System.Windows.Media.Brushes.Gray; }); string amazonValidateInUrl = "https://www.amazon.com/gc/redeem/"; if (view.Source.ToString().ToLower().Contains(amazonValidateInUrl) == false) { await view.WaitPageLoadComplete(() => { view.Source = amazonValidateInUrl.ToUri(); }, ct : ct); } await view.WaitPageLoadComplete(() => { view.Reload(false); }, ct : ct); view.ExecuteJavascript(string.Format("document.querySelector('#gc-redemption-input').value='{0}'", claimcode)); await Task.Delay(250); view.ExecuteJavascript("document.querySelector('#gc-redemption-check-value input').click()"); await Task.Delay(delayMS); string gcValue; bool codeError = false; bool captchaError = false; decimal parsedValue = -1; while (true) { gcValue = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-heading').innerHTML").ToString()); codeError = bool.Parse(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-error') ? (document.querySelector('#gc-redemption-error').innerHTML.toLowerCase().indexOf('claim code is invalid')>-1 ? true : false) : false").ToString()); captchaError = bool.Parse(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-captcha') ? (document.querySelector('#gc-redemption-captcha').innerHTML.toLowerCase().indexOf('security verification')>-1 ? true : false) : false").ToString()); if (codeError) { break; } if (captchaError) { captchaSolvedSignal = new SemaphoreSlim(0, 1); // Application.Current.MainWindow.Dispatcher.Invoke(() => { ((MainWindow)Application.Current.MainWindow).browserPlaceHolder.IsEnabled = true; }); // Application.Current.MainWindow.Dispatcher.Invoke(() => { MessageBox.Show("Captcha Detected. Enter Captcha and manually click 'check'."); }); while (true) { await Task.Delay(500); gcValue = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-heading').innerHTML").ToString()); codeError = bool.Parse(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-error') ? (document.querySelector('#gc-redemption-error').innerHTML.toLowerCase().indexOf('claim code is invalid')>-1 ? true : false) : false").ToString()); if (codeError) { Application.Current.MainWindow.Dispatcher.Invoke(() => { ((MainWindow)Application.Current.MainWindow).CaptchaPanel.Visibility = Visibility.Hidden; }); return(parsedValue); } if (gcValue == null || gcValue == "undefined" || gcValue == "null" || gcValue == "") { await Task.Delay(500); var captchaImageData = view.GetCaptchaData(); var captchaResult = SolveCaptcha_Tesseract(captchaImageData.BitmapImageToBitmap()); if (captchaResult.Key < .6) { await Application.Current.MainWindow.Dispatcher.Invoke(async() => { ((MainWindow)Application.Current.MainWindow).CaptchaPanel.Visibility = Visibility.Visible; ((MainWindow)Application.Current.MainWindow).CaptchaImage.Source = captchaImageData; await captchaSolvedSignal.WaitAsync(); captchaResult = new KeyValuePair <float, string>(1, ((MainWindow)Application.Current.MainWindow).txtCaptchaResult.Text); ((MainWindow)Application.Current.MainWindow).txtCaptchaResult.Text = string.Empty; }); } view.ExecuteJavascript(string.Format("document.querySelector('input[name=\"captchaInput\"]').value='{0}'", captchaResult.Value)); await Task.Delay(250); view.ExecuteJavascript("document.querySelector('#gc-redemption-check-value input').click()"); } try { await Task.Delay(500); string gcParsedClaimCode = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-result-box span').innerHTML").ToString()).Split()[2].ToLower(); gcValue = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-heading').innerHTML").ToString()); if (gcParsedClaimCode == claimcode.Replace("-", "").ToLower()) { decimal.TryParse(gcValue.Split()[0].Replace("$", ""), out parsedValue); Application.Current.MainWindow.Dispatcher.Invoke(() => { ((MainWindow)Application.Current.MainWindow).CaptchaPanel.Visibility = Visibility.Hidden; }); return(parsedValue); } else { } } catch (IndexOutOfRangeException ex) { } //else //{ // //await Application.Current.MainWindow.Dispatcher.Invoke(async () => // //{ // // ((MainWindow)Application.Current.MainWindow).CaptchaPanel.Visibility = Visibility.Visible; // // ((MainWindow)Application.Current.MainWindow).CaptchaImage.Source = await view.GetCaptchaData(); // // await captchaSolvedSignal.WaitAsync(); // // await Task.Delay(250); // // view.ExecuteJavascript(string.Format("document.querySelector('input[name=\"captchaInput\"]').value='{0}'", ((MainWindow)Application.Current.MainWindow).txtCaptchaResult.Text)); // // view.ExecuteJavascript("document.querySelector('#gc-redemption-check-value input').click()"); // //}); // //continue; //} } } if (gcValue == null || gcValue == "undefined" || gcValue == "null" || gcValue == "") { //await view.WaitPageLoadComplete(() => //{ // view.Reload(false); //}); await Task.Delay(delayMS); continue; } else { gcValue = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-heading').innerHTML").ToString()); string gcParsedClaimCode = HttpUtility.HtmlDecode(view.ExecuteJavascriptWithResult("document.querySelector('#gc-redemption-check-value-result-box span').innerHTML").ToString()).Split()[2].ToLower(); if (gcParsedClaimCode == claimcode.Replace("-", "").ToLower()) { decimal.TryParse(gcValue.Split()[0].Replace("$", ""), out parsedValue); return(parsedValue); } else { continue; } } } return(parsedValue); }