Libraries
::: warning Notice
Docker is standalone with Nstbrowser dashboard, please do not confuse the APIs between them.
:::
Websocket API
- Connect to browser:
ws://127.0.0.1:8848/connect
Connect to or launch a browser instance, query parameters:
Name | Kind | Type | Reqiured | Description |
---|---|---|---|---|
profileId | query | string | False | the profileId to connect to, a browser will be launched with random config if not passed |
config | query | string | False | launch config, in encoded json string, you can custom launch config if profileId not passed,the full config refer to Start Browser REST API |
You can use Puppeteer, Playwright, Chromedp or another CDP library to connect our websocket API.
JavaScript Puppeteer
Puppeteer is a Node.js library which provides a high-level API to control Nstchrome over the DevTools Protocol.
const puppeteer = require("puppeteer-core");
(async () => {
const host = "127.0.0.1:8848";
const browserWSEndpoint = `ws://${host}/connect`;
try {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
});
const page = await browser.newPage();
await page.goto("https://nstbrowser.io/");
await page.screenshot({path: "screenshot.png", fullPage: true});
// ...
await browser.close();
} catch (err) {
// ...
}
})();
JavaScript Playwright CDP
Playwright is a framework for Web Testing and Automation. It allows testing Nstchrome with a single API.
// javascript playwright CDP: npm install playwright
import {chromium} from "playwright";
(async () => {
const host = "127.0.0.1:8848";
const browserWSEndpoint = `ws://${host}/connect`;
let browser = await chromium.connectOverCDP(browserWSEndpoint);
const context = await browser.newContext();
const page = await context.newPage();
await page.goto("https://nstbrowser.com/");
await page.screenshot({path: "screenshot.png"});
// ...
})();
Python Pyppeteer
Pyppeteer is a headless chrome/chromium automation library for Python and requires python version >= 3.8
import asyncio
from pyppeteer import launcher
async def main():
browser = await launcher.connect(
browserWSEndpoint='ws://127.0.0.1:8848/connect'
)
page = await browser.newPage()
await page.goto('https://nstbrowser.io/')
await page.screenshot({'path': 'screenshot.png'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
Golang Chromedp
Package chromedp is a faster, simpler way to drive browsers supporting the Chrome DevTools Protocol in Go without external dependencies.
package main
import (
"context"
"flag"
"log"
"os"
"github.com/chromedp/chromedp"
)
func main() {
devtoolsWsURL := flag.String("devtools-ws-url", "ws://127.0.0.1:8848/connect", "DevTools WebSsocket URL")
flag.Parse()
allocatorContext, cancel := chromedp.NewRemoteAllocator(context.Background(), *devtoolsWsURL, chromedp.NoModifyURL)
defer cancel()
ctx, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
var buf []byte
// capture entire browser viewport, returning png with quality=90
if err := chromedp.Run(ctx, fullScreenshot(`https://nstbrowser.io/`, 90, &buf)); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("screenshot.png", buf, 0o644); err != nil {
log.Fatal(err)
}
}
// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.FullScreenshot(res, quality),
}
}