mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
d963fcdd80
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2.3 KiB
2.3 KiB
Running Puppeteer in the browser
Puppeteer is a powerful tool for automating browsers, but did you know it can also run within a browser itself? This enables you to leverage Puppeteer's capabilities for tasks that don't require Node.js specific features.
Supported Features
While running in the browser, Puppeteer offers a variety of functionalities including:
- WebSocket Connections: Establish connections to existing browser instances using WebSockets. Launching or downloading browsers directly is not supported as it relies on Node.js APIs.
- Script Evaluation: Execute JavaScript code within the browser context.
- Document Manipulation: Generate PDFs and screenshots of the current web page.
- Page Management: Create, close, and navigate between different web pages.
- Cookie Handling: Inspect, modify, and manage cookies within the browser.
- Network Control: Monitor and intercept network requests made by the browser.
How to run Puppeteer in the browser
:::note
See https://github.com/puppeteer/puppeteer/tree/main/examples/puppeteer-in-browser for a complete example.
:::
To run Puppeteer in the browser, first you need to produce a browser-compatible build using a bundler such as rollup or webpack:
- When importing Puppeteer use the browser-specific entrypoint from puppeteer-core
puppeteer-core/lib/esm/puppeteer/puppeteer-core-browser.js'
:
import puppeteer from 'puppeteer-core/lib/esm/puppeteer/puppeteer-core-browser.js';
const browser = await puppeteer.connect({
browserWSEndpoint: wsUrl,
});
alert('Browser has ' + (await browser.pages()).length + ' pages');
browser.disconnect();
- Build your app using a bundler. For example, the following configuration can be used with rollup:
import {nodeResolve} from '@rollup/plugin-node-resolve';
export default {
input: 'main.mjs',
output: {
format: 'esm',
dir: 'out',
},
plugins: [
nodeResolve({
// Indicate that we target a browser environment.
browser: true,
// Exclude any dependencies except for puppeteer-core.
// `npm install puppeteer-core` # To install puppeteer-core if needed.
resolveOnly: ['puppeteer-core'],
}),
],
};
:::note
Do not forget to include a valid browser WebSocket endpoint when connecting to an instance.
:::
- Include the produced bundle into a web page.