Puppeteer
How to use in Puppeteer
- Launch nstbrowser agent client, the client will listen on port:
8848
- Get the remote debugging address of the browser:
browserWSEndpoint
- Connect to Puppeteer.
Common code
async function execPuppeteer(browserWSEndpoint) {
try {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
defaultViewport: null,
});
const page = await browser.newPage();
await page.goto('https://nstbrowser.io/');
await page.screenshot({path: 'screenshot.png'});
await browser.disconnect();
} catch (err) {
console.error(err);
}
}
1. LaunchExistBrowser
::: tip URLws://localhost:8848/devtool/launch/{profileId}?x-api-key={x-api-key}&config={config}
- Connect to or start an existing browser,you need to create the corresponding profile in advance
- Support custom config
:::
import puppeteer from 'puppeteer-core';
// LaunchExistBrowser: Connect to or start an existing browser
// You need to create the corresponding profile in advance
// Support custom config
async function launchAndConnectToBrowser(profileId) {
const host = 'localhost:8848';
const apiKey = 'your api key';
const config = {
headless: true, // support: true or false
autoClose: true,
};
const query = new URLSearchParams({
'x-api-key': apiKey, // required
config: encodeURIComponent(JSON.stringify((config))),
});
const browserWSEndpoint = `ws://${host}/devtool/launch/${profileId}?${query.toString()}`;
console.log('browserWSEndpoint: ', browserWSEndpoint);
await execPuppeteer(browserWSEndpoint);
}
launchAndConnectToBrowser('your profileId').then();
2. CreateAndConnectToBrowser
- Create a new Profile based on config and launch the browser
- Support custom config
import puppeteer from 'puppeteer-core';
// createAndConnectToBrowser: launch new browser and connect
// Create a new Profile based on config and launch the browser
// Support custom config
async function createAndConnectToBrowser() {
const host = 'localhost:8848';
const apiKey = 'your api key';
const config = {
name: 'testProfile',
platform: 'windows', // support: windows, mac, linux
kernel: 'chromium', // only support: chromium
kernelMilestone: '124', // support: 113, 120, 124
once: true, // one_time browser
headless: true, // support: true or false
autoClose: false,
remoteDebuggingPort: 9223,
proxy: '', // input format: schema://user:password@host:port eg: http://user:password@localhost:8080
args: {
"--proxy-bypass-list": "detect.nstbrowser.io"
}, // browser args
fingerprint: {
hardwareConcurrency: 4, // support: 2, 4, 8, 10, 12, 14, 16
deviceMemory: 8, // support: 2, 4, 8
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.56 Safari/537.36', // userAgent supportted since v0.15.0
},
};
const query = new URLSearchParams({
'x-api-key': apiKey, // required
config: encodeURIComponent(JSON.stringify((config))),
});
const browserWSEndpoint = `ws://${host}/devtool/launch?${query.toString()}`;
console.log('browserWSEndpoint: ', browserWSEndpoint);
await execPuppeteer(browserWSEndpoint);
}
createAndConnectToBrowser().then();
3. Config parameters notes
Parameter | Type | Description | |
---|---|---|---|
config.name | String | Browser name | |
config.platform | String | Operating system type (supported: windows, mac, linux) | |
config.kernel | String | Kernel type (only supported: chromium) | |
config.kernelMilestone | String | Kernel version (supported: 113, 120, 124) | |
config.once | Boolean | Disposable browser that does not create a Profile and automatically closes after execution; (only supported by "CreateAndConnectToBrowser") | |
config.headless | Boolean | true: enable headless; "new": enable new headless mode; difference reference https://developer.chrome.com/docs/chromium/new-headless. and automatically close after execution | |
config.autoClose | Boolean | Whether to automatically close the connection after the browser is closed | |
config.timedCloseSec | Number | Timed close (s) | |
config.remoteDebuggingPort | Number | Remote debugging port | |
config.proxy | String | Proxy server address and port number (format: schema://user:password@host:port, e.g. http://user/:password@localhost:8080/) | |
config.args | Object | Browser args, invalid to LaunchExistBrowser API |
|
config.fingerprint | Object | Browser fingerprint configuration; invalid to LaunchExistBrowser API |
|
hardwareConcurrency | Number | CPU core count (supported: 2, 4, 8, 10, 12, 14, 16) | |
deviceMemory | Number | Device memory size (supported: 2, 4, 8) | |
userAgent | String | UserAgent |
the full config
refer to LaunchNewBrowser.
![](https://assets.apidog.com/app/apidoc-image/custom/20240507/7fb0949c-346d-40a1-b8e8-b2962cfaf3d7.png)
Modified at 9 days ago