BrowserPool <Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
Hierarchy
- TypedEmitter<BrowserPoolEvents<BrowserControllerReturn, PageReturn>>
- BrowserPool
Implements
- IBrowserPool<PageReturn>
Index
Constructors
Properties
- activeBrowserControllers
- browserPlugins
- closeInactiveBrowserAfterMillis
- fingerprintCache
- fingerprintGenerator
- fingerprintInjector
- fingerprintOptions
- maxOpenBrowsers
- maxOpenPagesPerBrowser
- operationTimeoutMillis
- pageCounter
- pageIds
- pages
- pageToBrowserController
- postLaunchHooks
- postPageCloseHooks
- postPageCreateHooks
- preLaunchHooks
- prePageCloseHooks
- prePageCreateHooks
- retireBrowserAfterPageCount
- retiredBrowserControllers
- startingBrowserControllers
- useFingerprints
- defaultMaxListeners
Methods
- [asyncDispose]
- addListener
- closeAllBrowsers
- closePage
- destroy
- emit
- eventNames
- extractPageState
- getBrowserControllerByPage
- getMaxListeners
- getPage
- getPageId
- hasActiveBrowserWithFreeCapacity
- hasFreeBrowserSlot
- injectPageState
- listenerCount
- listeners
- newPage
- newPageInNewBrowser
- newPageWithEachPlugin
- off
- on
- once
- prependListener
- prependOnceListener
- rawListeners
- removeAllListeners
- removeListener
- retireAllBrowsers
- retireBrowserByPage
- retireBrowserController
- setMaxListeners
Constructors
constructor
Parameters
options: Options & BrowserPoolHooks<BrowserControllerReturn, LaunchContextReturn, PageReturn>
Returns BrowserPool<Options, BrowserPlugins, BrowserControllerReturn, LaunchContextReturn, PageOptions, PageReturn>
Properties
activeBrowserControllers
browserPlugins
closeInactiveBrowserAfterMillis
optionalfingerprintCache
optionalfingerprintGenerator
optionalfingerprintInjector
fingerprintOptions
maxOpenBrowsers
maxOpenPagesPerBrowser
operationTimeoutMillis
pageCounter
pageIds
pages
pageToBrowserController
postLaunchHooks
postPageCloseHooks
postPageCreateHooks
preLaunchHooks
prePageCloseHooks
prePageCreateHooks
retireBrowserAfterPageCount
retiredBrowserControllers
startingBrowserControllers
optionaluseFingerprints
staticexternalinheriteddefaultMaxListeners
Methods
[asyncDispose]
Returns Promise<void>
externalinheritedaddListener
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
closeAllBrowsers
Closes all managed browsers without waiting for pages to close.
Returns Promise<void>
closePage
Releases a page back to the pool. The page is closed and, if the optional
erroris a SessionError, the browser controller that served the page is retired so that its tainted state (cookies, storage, etc.) cannot leak into future sessions.This is the primary way the crawler should return pages to the pool.
Parameters
page: PageReturn
The page to release.
optionaloptions: { error?: Error }
optionalerror: Error
The error that caused the page to be released, if any.
Returns Promise<void>
destroy
Closes all managed browsers and tears down the pool.
Returns Promise<void>
externalinheritedemit
Parameters
externalevent: U
externalrest...args: Parameters<BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]>
Returns boolean
externalinheritedeventNames
Returns U[]
extractPageState
Extracts the relevant state (currently just cookies) from a page via its owning BrowserController. Returns empty state when the page is no longer associated with a controller.
As with BrowserPool.injectPageState, cookies are isolated per page only when the pool is configured with
useIncognitoPages: true. With the defaultuseIncognitoPages: false, the extracted cookies include those set by any sibling page sharing the same browser.Parameters
page: PageReturn
Returns Promise<PageState>
getBrowserControllerByPage
Retrieves a BrowserController for a given page. This is useful when you're working only with pages and need to access the browser manipulation functionality.
You could access the browser directly from the page, but that would circumvent
BrowserPooland most likely cause weird things to happen, so please always useBrowserControllerto control your browsers. The function returnsundefinedif the browser is closed.Parameters
page: PageReturn
Browser plugin page
Returns undefined | BrowserControllerReturn
externalinheritedgetMaxListeners
Returns number
getPage
If you provided a custom ID to one of your pages or saved the randomly generated one, you can use this function to retrieve the page. If the page is no longer open, the function will return
undefined.Parameters
id: string
Returns undefined | PageReturn
getPageId
Page IDs are used throughout
BrowserPoolas a method of linking events. You can use a page ID to track the full lifecycle of the page. It is created even before a browser is launched and stays with the page until it's closed.Parameters
page: PageReturn
Returns undefined | string
hasActiveBrowserWithFreeCapacity
Returns
trueif any active browser has room for another page.Returns boolean
hasFreeBrowserSlot
Returns
trueif the pool can accept a new browser launch without exceeding BrowserPoolOptions.maxOpenBrowsers. Counts starting, active, and retired browsers.Returns boolean
injectPageState
Injects state into a page via its owning BrowserController.
No-op when the page is no longer associated with a controller.
Note that cookies are isolated per page only when the pool is configured with
useIncognitoPages: true— each page then gets its own browser context. With the defaultuseIncognitoPages: false, all pages in a browser share a single context, so injected cookies are visible to every page served by that browser.Parameters
page: PageReturn
state: PageState
Returns Promise<void>
externalinheritedlistenerCount
Parameters
externaltype: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Returns number
externalinheritedlisteners
Parameters
externaltype: U
Returns BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U][]
newPage
Opens a new page in one of the running browsers or launches a new browser and opens a page there, if no browsers are active, or their page limits have been exceeded.
Session injection (best-effort): When a session is provided, this implementation uses it as a cache key for browser fingerprints (when fingerprinting is enabled) and reads session.proxyInfo.url / session.proxyInfo.ignoreTlsErrors as defaults for
proxyUrlandignoreTlsErrorsrespectively. ExplicitproxyUrl/ignoreTlsErrorsvalues in the options take precedence.Beyond fingerprint caching and proxy configuration, no other session properties are consumed — cookie and header injection remain the crawler's responsibility.
Parameters
options: BrowserPoolNewPageOptions<PageOptions, BrowserPlugins[number]> = {}
Returns Promise<PageReturn>
newPageInNewBrowser
Unlike newPage,
newPageInNewBrowseralways launches a new browser to open the page in. Use thelaunchOptionsoption to configure the new browser.Parameters
options: BrowserPoolNewPageInNewBrowserOptions<PageOptions, BrowserPlugins[number]> = {}
Returns Promise<PageReturn>
newPageWithEachPlugin
Opens new pages with all available plugins and returns an array of pages in the same order as the plugins were provided to
BrowserPool. This is useful when you want to run a script in multiple environments at the same time, typically in testing or website analysis.Example:
const browserPool = new BrowserPool({browserPlugins: [new PlaywrightPlugin(playwright.chromium),new PlaywrightPlugin(playwright.firefox),new PlaywrightPlugin(playwright.webkit),]});const pages = await browserPool.newPageWithEachPlugin();const [chromiumPage, firefoxPage, webkitPage] = pages;Parameters
optionsList: Omit<BrowserPoolNewPageOptions<PageOptions, BrowserPlugins[number]>, browserPlugin>[] = []
Returns Promise<PageReturn[]>
externalinheritedoff
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
externalinheritedon
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
externalinheritedonce
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
externalinheritedprependListener
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
externalinheritedprependOnceListener
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
externalinheritedrawListeners
Parameters
externaltype: U
Returns BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U][]
externalinheritedremoveAllListeners
Parameters
externaloptionalevent: keyof BrowserPoolEvents<BrowserControllerReturn, PageReturn>
Returns this
externalinheritedremoveListener
Parameters
externalevent: U
externallistener: BrowserPoolEvents<BrowserControllerReturn, PageReturn>[U]
Returns this
retireAllBrowsers
Removes all active browsers from the pool. The browsers will be closed after all their pages are closed.
Returns void
retireBrowserByPage
Removes a browser from the pool. It will be closed after all its pages are closed.
Parameters
page: PageReturn
Returns void
retireBrowserController
Removes a browser controller from the pool. The underlying browser will be closed after all its pages are closed.
Parameters
browserController: BrowserControllerReturn
Returns void
externalinheritedsetMaxListeners
Parameters
externaln: number
Returns this
The
BrowserPoolclass is the most important class of thebrowser-poolmodule. It manages opening and closing of browsers and their pages and its constructor options allow easy configuration of the browsers' and pages' lifecycle.The most important and useful constructor options are the various lifecycle hooks. Those allow you to sequentially call a list of (asynchronous) functions at each stage of the browser / page lifecycle.
Example: