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"); }
public static async Task <string> ProcessVideo( [ActivityTrigger] DurableActivityContext ctx, TraceWriter log ) { var url = ctx.GetInput <string>(); var asm = Assembly.Load("Microsoft.Extensions.Options, Culture=neutral, PublicKeyToken=adb9793829ddae60"); PuppeteerSharp.Browser browser = null; try { browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = Constants.BrowserWSEndpoint }); var page = await browser.NewPageAsync(); await page.GoToAsync(url, new NavigationOptions { Timeout = 4000 }); } catch (Exception ex) { return(ex.Message); } finally { if (browser != null) { browser.Disconnect(); } } return($"Processed {url} shit all."); }
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); }); }
public async Task <byte[]> ScreenshotPNG(string url, IEnumerable <CookieParam> cookies, ViewPortOptions viewport, ScreenshotOptions options, RevisionInfo revision) { LaunchOptions launchOptions = new LaunchOptions() { ExecutablePath = revision.ExecutablePath, Args = BrowserArgs, Headless = true, DefaultViewport = viewport, Timeout = 0 }; browser = await Puppeteer.LaunchAsync(launchOptions); try { var page = await browser.NewPageAsync(); try { NavigationOptions navigationOptions = new NavigationOptions() { Timeout = 0 }; string originalUserAgent = await browser.GetUserAgentAsync(); await page.SetUserAgentAsync($"{originalUserAgent} {USER_AGENT_SUFFIX}"); if (cookies.Any()) { await page.SetCookieAsync(cookies.ToArray()); } await page.GoToAsync(url, navigationOptions); return(await page.ScreenshotDataAsync(options)); } finally { await Cleanup(page); } } finally { Cleanup(browser); } }
public async Task Screenshot(string uri, string outputFile) { outputFile = Path.Combine(_outputDir, outputFile); var options = _options.CurrentValue; var page = await _browser.NewPageAsync(); await page.GoToAsync(uri); await page.SetViewportAsync(new ViewPortOptions { Width = options.PageWidth, Height = options.PageHeight }); _logger.LogDebug($"Screenshotting url '{uri}' to file '{outputFile}'"); // TODO: Async wait x seconds since the page might not display correctly right away? await page.ScreenshotAsync(outputFile); _logger.LogDebug($"Success, sending saved message."); await page.CloseAsync(); }
public async Task <byte[]> PrintPDF(string url, IEnumerable <CookieParam> cookies, ViewPortOptions viewport, PdfOptions options, RevisionInfo revision) { LaunchOptions launchOptions = new LaunchOptions() { ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", Args = BrowserArgs, Headless = true, DefaultViewport = viewport, Timeout = 0 }; browser = await Puppeteer.LaunchAsync(launchOptions); try { var page = await browser.NewPageAsync(); try { WaitUntilNavigation[] waitUntilNavigations = { WaitUntilNavigation.Networkidle0 }; NavigationOptions navigationOptions = new NavigationOptions() { Timeout = 0, WaitUntil = waitUntilNavigations }; string originalUserAgent = await browser.GetUserAgentAsync(); await page.SetUserAgentAsync($"{originalUserAgent} {USER_AGENT_SUFFIX}"); if (cookies.Any()) { await page.SetCookieAsync(cookies.ToArray()); } await page.GoToAsync(url, navigationOptions); await InjectCustomStylesAsync(page, ref options); bool hasPageNumbers = await page.EvaluateFunctionAsync <bool>("(window.UltimatePDF? window.UltimatePDF.hasPageNumbers : function() { return false; })"); if (hasPageNumbers) { /* * When the layout has page numbers, we first retrieve a * first blank pdf to calculate the number of pages. * Then, knowing how many pages, we can layout the headers and footers, * and retrieve the final pdf. */ byte[] blankContents = await page.PdfDataAsync(options); using (var blankPdf = new PDFUtils(blankContents)) { await page.EvaluateFunctionAsync("window.UltimatePDF.layoutPages", blankPdf.Pages); return(await page.PdfDataAsync(options)); } } else { return(await page.PdfDataAsync(options)); } } finally { await Cleanup(page); } } finally { Cleanup(browser); } }