Browser.psm1
<#
.SYNOPSIS This function returns the JS code for setting the chrome browser options. #> function Get-SetChromeBrowserOptionsCode { param ( # Select if the chrome browser is executed as headless (no GUI) [Parameter(Mandatory=$false)] [ValidateNotNull()] [Bool]$isHeadless = $true, # The width of the browser viewport [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [String]$width = "1920", # The height of the browser viewport [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [String]$height = "1080" ) $code = @" // Set the Chrome browser options var chromeBrowserOptions = {}; // Select whether Chrome will be executed as headless (no GUI) chromeBrowserOptions.headless = IS_HEADLESS; // Set default viewport to be larger, otherwise some page elements will not be available chromeBrowserOptions.defaultViewport = Object; chromeBrowserOptions.defaultViewport.width = WIDTH_VALUE; chromeBrowserOptions.defaultViewport.height = HEIGHT_VALUE; "@ $code = $code -replace "IS_HEADLESS", "$($isHeadless)".ToLower() $code = $code -replace "WIDTH_VALUE", $width $code = $code -replace "HEIGHT_VALUE", $height return $code } <# .SYNOPSIS This function returns the JS code for launching a chrome browser. #> function Get-LaunchChromeBrowserCode { return " const browser = await puppeteer.launch(chromeBrowserOptions);" } <# .SYNOPSIS This function returns the JS code for closing a chrome browser. #> function Get-CloseChromeBrowserCode { return " await browser.close();" } <# .SYNOPSIS This function returns the JS code for navigating a new browser page to a link. #> function Get-NavigateNewBrowserPageToLinkCode { param ( # The link to navigate the new browser page to. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$link ) return @" const page = await browser.newPage(); await page.goto(`"LINK`"); await page.setViewport(chromeBrowserOptions.defaultViewport) "@ -replace "LINK", $link } <# .SYNOPSIS This function returns the JS code for clicking on a selector on a browser page. #> function Get-ClickOnBrowserPageSelectorCode { param ( # The name of the selector. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$selectorName, # Whether to wait for the page to load after clicking [Parameter(Mandatory=$false)] [Switch]$waitForLoad = [Switch]::Present ) $code = @" selector = "SELECTOR_NAME"; await page.waitForSelector(selector); await page.click(selector, { delay: 10 }); "@ -replace "SELECTOR_NAME", $selectorName if ($waitForLoad) { $code += @" await page.waitForNavigation({ waitUntil: 'networkidle0' }); await delay(100) "@ } return $code } function Get-ClickOnEitherBrowserPageSelectorCode { param ( # The name of the first selector. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$firstSelectorName, # The name of the second selector. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$secondSelectorName, # Whether to wait for the page to load after clicking [Parameter(Mandatory=$false)] [Switch]$waitForLoad = [Switch]::Present ) $code = (@" firstSelector = "FIRST_SELECTOR_NAME"; secondSelector = "SECOND_SELECTOR_NAME"; await Promise.race([ page.waitForSelector(firstSelector), page.waitForSelector(secondSelector) ]) if (await page.$(selector) !== null) await page.click(firstSelector, { delay: 10 }); else await page.click(secondSelector, { delay: 10 }); "@ -replace "FIRST_SELECTOR_NAME", $firstSelectorName) -replace "SECOND_SELECTOR_NAME", $secondSelectorName } <# .SYNOPSIS This function returns the JS code for typing text into a selector on a browser page. #> function Get-EnterTextIntoBrowserPageSelectorCode { param ( # The name of the selector. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$selectorName, # The text to enter. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$text ) return (@" selector = "SELECTOR_NAME"; await page.waitForSelector(selector); await page.type(selector, TEXT) "@ -replace "SELECTOR_NAME", $selectorName) -replace "TEXT", $text } <# .SYNOPSIS This function returns the JS code for retrieving text from a selector on a browser page. #> function Get-RetrieveTextFromBrowserPageSelectorCode { param ( # The name of the selector. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$selectorName, # The variable to save the text in. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$variable ) return (@" selector = "SELECTOR_NAME" await page.waitForSelector(selector); VARIABLE_NAME = await page.evaluate(selector => { const anchors = Array.from(document.querySelectorAll(selector)); return anchors.map(anchor => { return anchor.textContent; }); }, selector); "@ -replace "SELECTOR_NAME", $selectorName) -replace "VARIABLE_NAME", $variable } <# .SYNOPSIS This function returns the JS code for taking a screenshot of a browser page. #> function Get-TakeScreenshotOfBrowserPageCode { param ( # The path to save the screenshot. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$filePath ) # Generate the code $code = " await page.screenshot({ 'path': '%filepath%', 'quality': 50 })" $code = $code -replace "%filepath%", $filePath # Return the code return $code } |