private void Init() { logger.Debug($"{this.Name}: Initiating PuppeteerBrowser"); var chromeBinaryFileName = Devmasters.Core.Util.Config.GetConfigValue("ChromeBinaryFullPath"); var launchOptions = new LaunchOptions() { Headless = true, DefaultViewport = new ViewPortOptions() { DeviceScaleFactor = 1, IsLandscape = false, HasTouch = false, IsMobile = false, Height = this.height, Width = this.width, } }; if (!string.IsNullOrEmpty(chromeBinaryFileName)) { launchOptions.ExecutablePath = chromeBinaryFileName; } this.browser = Puppeteer.LaunchAsync(launchOptions).Result; this.page = browser.NewPageAsync().Result; logger.Info($"{this.Name}: Initiated PuppeteerBrowser"); }
private static async Task SetPageLoadingOptions(BotData data, PuppeteerSharp.Page page) { if (data.ConfigSettings.PuppeteerSettings.LoadOnlyDocumentAndScript) { await page.SetRequestInterceptionAsync(true); page.Request += (sender, e) => { if (e.Request.ResourceType == ResourceType.Document || e.Request.ResourceType == ResourceType.Script) { e.Request.ContinueAsync(); } else { e.Request.AbortAsync(); } }; } if (data.ConfigSettings.PuppeteerSettings.DismissDialogs) { page.Dialog += (sender, e) => { data.Logger.Log($"Dialog automatically dismissed: {e.Dialog.Message}", LogColors.DarkSalmon); e.Dialog.Dismiss(); }; } }
static async System.Threading.Tasks.Task StartWebBrowser() { var browserRevision = PuppeteerSharp.BrowserFetcher.DefaultChromiumRevision; var browserFetcher = new PuppeteerSharp.BrowserFetcher(new PuppeteerSharp.BrowserFetcherOptions { Path = BrowserDownloadDirPath() }); await browserFetcher.DownloadAsync(browserRevision); browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions { Headless = false, UserDataDir = UserDataDirPath("default"), DefaultViewport = null, ExecutablePath = browserFetcher.RevisionInfo(browserRevision).ExecutablePath, }); browserPage = (await browser.PagesAsync()).FirstOrDefault() ?? await browser.NewPageAsync(); await browserPage.ExposeFunctionAsync("____callback____", (string returnValue) => { callbackFromBrowserDelegate?.Invoke(returnValue); return(0); }); }
private static async Task SetPageLoadingOptions(BotData data, PuppeteerSharp.Page page) { if (data.ConfigSettings.PuppeteerSettings.LoadOnlyDocumentAndScript) { await page.SetRequestInterceptionAsync(true); page.Request += (sender, e) => { if (e.Request.ResourceType == ResourceType.Document || e.Request.ResourceType == ResourceType.Script) { e.Request.ContinueAsync(); } else { e.Request.AbortAsync(); } }; } }
private static async Task SetPageLoadingOptions(BotData data, PuppeteerSharp.Page page) { await page.SetRequestInterceptionAsync(true); page.Request += (sender, e) => { // If we only want documents and scripts but the resource is not one of those, block if (data.ConfigSettings.PuppeteerSettings.LoadOnlyDocumentAndScript && e.Request.ResourceType != ResourceType.Document && e.Request.ResourceType != ResourceType.Script) { e.Request.AbortAsync(); } // If the url contains one of the blocked urls else if (data.ConfigSettings.PuppeteerSettings.BlockedUrls .Where(u => !string.IsNullOrWhiteSpace(u)) .Any(u => e.Request.Url.Contains(u, StringComparison.OrdinalIgnoreCase))) { e.Request.AbortAsync(); } // Otherwise all good, continue else { e.Request.ContinueAsync(); } }; if (data.ConfigSettings.PuppeteerSettings.DismissDialogs) { page.Dialog += (sender, e) => { data.Logger.Log($"Dialog automatically dismissed: {e.Dialog.Message}", LogColors.DarkSalmon); e.Dialog.Dismiss(); }; } }
private static void SetPageAndFrame(BotData data, PuppeteerSharp.Page page) { data.SetObject("puppeteerPage", page); SwitchToMainFramePrivate(data); }