/// <summary> /// enters phrase in a way depending on clickerInput /// </summary> /// <param name="clickerInput">bool if clicker enabled</param> /// <param name="webBrowser">cafsharp browser interface</param> /// <param name="phrase">built search phrase</param> public async Task EnterPhraseAsync(bool clickerInput, IWpfWebBrowser webBrowser, string phrase) { _log.Called(clickerInput, string.Empty, phrase); try { if (!clickerInput) { phrase = ValidatePhrase(phrase); await webBrowser.EvaluateScriptAsync(@" var arr = document.getElementsByTagName('input')[2].value = '" + phrase + @"'; "); } else { await _inputService.ClickSearchBarInputAsync(); await _inputService.EnterPhraseInputAsync(phrase); } } catch (Exception e) { _log.Error(e.Message); } }
private async void GetInnerHTML() { try { var response = await webBrowser.EvaluateScriptAsync("myBytes=window.document.body.innerHTML;"); if (response.Success && response.Result is IJavascriptCallback) { response = await((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript"); } MainWindow.strInnerHtml = (string)(response.Success ? (response.Result ?? "null") : response.Message); ParseHtmlHelper m_ParseHtmlHelper = new ParseHtmlHelper((string)MainWindow.strInnerHtml); m_ParseHtmlHelper.GetMessageUrl(); for (int i = 0; i < m_ParseHtmlHelper.MessageUrlMatchs.Count; i++) { string m_Message = m_ParseHtmlHelper.MessageUrlMatchs[i].Groups[1].Captures[0].Value; /* need handle */ string ifrm_name = string.Format(@"frm{0}", i); string JsCmd = "document.body.insertAdjacentHTML(\"beforeEnd\", \"<iframe width='400' height='50' id='" + ifrm_name + "' src='" + m_Message + "'/>\");"; response = await webBrowser.EvaluateScriptAsync(JsCmd); Random ran = new Random(); /* 搞个随机数防检测 */ int RandKey = ran.Next(200, 1500); Thread.Sleep(RandKey); JsCmd = "var iFrame = document.getElementById('" + ifrm_name + "');var iFrameElement = iFrame.contentWindow.document.body.innerHTML;MyByte=iFrameElement;"; response = await webBrowser.EvaluateScriptAsync(JsCmd); MainWindow.strMessageHtml += (string)(response.Success ? (response.Result ?? "null") : response.Message); } response = await webBrowser.EvaluateScriptAsync("myBytes=window.document.body.innerHTML;"); if (response.Success && response.Result is IJavascriptCallback) { response = await((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript"); } MainWindow.strInnerHtml = (string)(response.Success ? (response.Result ?? "null") : response.Message); } catch (Exception e) { MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void BrowserOnFrameLoadEnd(object _, FrameLoadEndEventArgs __) { Script.Initialize(_webBrowser.ExecuteScriptAsync, s => _webBrowser.EvaluateScriptAsync(s)); // var map = new Map( "map" ) // .SetView( new LatLng( 47.6097, -122.3331 ) ) // .AddTileLayer( @"http://{s}.tile.osm.org/{z}/{x}/{y}.png" ); // // var marker = new Marker( new LatLng( 47.6097, -122.3331 ) ); // marker.AddTo( map ); }
private async void EvaluateJavaScript(string s) { try { var response = await webBrowser.EvaluateScriptAsync(s); EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; } catch (Exception e) { MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private async void EvaluateJavaScript(string s) { try { var response = await webBrowser.EvaluateScriptAsync(s); if (response.Success && response.Result is IJavascriptCallback) { response = await((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript"); } EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; } catch (Exception e) { MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// clicks html document object in a way depending on clickerInput /// </summary> /// <param name="clickerInput">bool if clicker enabled</param> /// <param name="webBrowser">cefsharp browser interface</param> public async Task ClickSearchBtnAsync(bool clickerInput, IWpfWebBrowser webBrowser) { _log.Called(clickerInput, string.Empty); try { if (!clickerInput) { await webBrowser.EvaluateScriptAsync(@" var arr = document.getElementsByTagName('input')[3].click(); "); } } catch (Exception e) { _log.Error(e.Message); } }
private void EvaluateJavaScript(string s) { try { var task = webBrowser.EvaluateScriptAsync(s); task.ContinueWith(t => { if (!t.IsFaulted) { var response = t.Result; EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; } }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception e) { MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// checks if in the html document is captcha /// </summary> /// <param name="webBrowser">cefsharp browser interface</param> public async Task <bool> CheckForRecaptchaAsync(IWpfWebBrowser webBrowser) { _log.Called(string.Empty); try { bool isCaptcha = false; Task <JavascriptResponse> verifyReCaptcha = webBrowser.EvaluateScriptAsync(@" (function() { const arr = document.getElementsByTagName('a'); for (i = 0; i < arr.length; ++i) { item = arr[i]; if (document.getElementById('infoDiv')) { return true; } } } )();" ); await verifyReCaptcha.ContinueWith(t => { if (!t.IsFaulted) { var response = t.Result; var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; isCaptcha = Convert.ToBoolean(response.Result); } } ); return(isCaptcha); } catch (Exception e) { _log.Error(e.Message); return(true); } }
/// <summary> /// searches for URL data in html document /// </summary> /// <param name="webBrowser">cefsharp browser interface</param> /// <returns>string tag data</returns> public async Task <string> GetUrlAsync(IWpfWebBrowser webBrowser) { _log.Called(string.Empty); try { string url = ""; Task <JavascriptResponse> findUrl = webBrowser.EvaluateScriptAsync(@" (function() { var results = document.getElementsByClassName('srg')[0]; var result = results.getElementsByClassName('g')[0]; var div = result.getElementsByTagName('div')[0]; return div.innerHTML; } )();" ); await findUrl.ContinueWith(t => { if (!t.IsFaulted) { var response = t.Result; var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; url = Convert.ToString(response.Result); } } ); url = SplitUrl(url); return(url); } catch (Exception e) { _log.Error(e.Message); return(string.Empty); } }
/// <summary> /// switches off JS alerts /// </summary> /// <param name="webBrowser">cefsharp browser interface</param> public async Task TurnOffAlertsAsync(IWpfWebBrowser webBrowser) { await webBrowser.EvaluateScriptAsync(@" window.alert = function(){} " ); }