/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsCode"></param> /// <returns></returns> public async Task <T> ExecuteJava <T>(string jsCode) where T : struct { return(await HtmlPage.EvaluateFunctionAsync <T>(jsCode)); // TODO: add await selector here! }
public bool TryGrabPage(string url, out Task <Response> responseTsk, out Page page, bool inNewPage = false, bool?noImage = false) { if (!IsReady) { throw new Exception("Chromium instance is not ready yet."); } var pg = HtmlPage; if (inNewPage) { Task.Run(async() => { pg = await Browser.NewPageAsync(); if (!IsHeadless) { await pg.SetViewportAsync(new ViewPortOptions() { Width = 1024, Height = 842 }); } }).Wait(); } Task.Run(async() => { if (noImage.HasValue && noImage.Value) { await pg.SetRequestInterceptionAsync(true); // disable images to download pg.Request += (sender, e) => { if (e.Request.ResourceType == ResourceType.Image) { e.Request.AbortAsync().Wait(); } else { e.Request.ContinueAsync().Wait(); } }; } }).Wait(); var catContTsk = pg.GoToAsync(url, WaitUntilNavigation.Networkidle2); catContTsk.Wait(); var jsCode = @"() => { const selector = document.querySelector('[class=""error-404""]'); return selector ? (selector.innerHTML || '') : ''; }"; var checkErrorTsk = HtmlPage.EvaluateFunctionAsync <string>(jsCode); var checkError = checkErrorTsk.Result; responseTsk = catContTsk; page = pg; if (!noImage.HasValue || !noImage.Value) { return(string.IsNullOrEmpty(checkError)); } pg.SetRequestInterceptionAsync(false).Wait(); pg.Request -= null; return(string.IsNullOrEmpty(checkError)); }