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 PuppeteerBrowser(IOptionsMonitor <BrowserConfiguration> optionsAccessor, ILogger <PuppeteerBrowser> logger) { _logger = logger; _options = optionsAccessor; _outputDir = _options.CurrentValue.OutputDirectory; // This can definitely fail and probably shouldn't be in a constructor, but... _browser = Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new [] { "--no-sandbox", "--disable-setuid-sandbox" } }).Result; }
public static async Task <bool> LoginToTiqa(PuppeteerSharp.Browser browser) { try { var pages = await browser.PagesAsync(); var page = pages[0]; await page.GoToAsync(AppConfig.tiqaUrl); var un = await page.WaitForSelectorAsync(AppConfig.unFld); var pw = await page.WaitForSelectorAsync(AppConfig.pwFld); var lgn = await page.WaitForSelectorAsync(AppConfig.lgnBtn); await un.TypeAsync(AppConfig.un); await pw.TypeAsync(AppConfig.pw); await lgn.ClickAsync(); await page.WaitForNavigationAsync(); var cks = await page.GetCookiesAsync(); AppConfig.cks = cks.ToList();//.Where(c => c.Domain == new Uri(page.Url).Host).ToList(); AppConfig.sessId = AppConfig.cks.Where(c => c.Name == AppConfig.sessIdName).FirstOrDefault().Value; AppConfig.WriteOut($">> session id for crawl set to {AppConfig.sessId}"); AppConfig.crawlStartUri = page.Url; return(true); } catch (NavigationException ex) { AppConfig.ErrHand(ex, $"XX failed to navigate to url"); return(false); } catch (Exception ex) { AppConfig.ErrHand(ex); return(false); } }
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 StartBrowserAsync() { var fetcher = new BrowserFetcher(); var revisions = fetcher.LocalRevisions(); if (!revisions.Any()) { _logger.LogInformation("Downloading Chromium browser"); await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision); } _logger.LogInformation("Launching Chromium browser"); Browser = await Puppeteer.LaunchAsync(new LaunchOptions { DefaultViewport = new ViewPortOptions() { Width = 1366, Height = 768 }, Headless = !Debugger.IsAttached }); }
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); } }
private void Cleanup(PuppeteerSharp.Browser browser) { browser.Disconnect(); Dispose(); }