Usage
Dump a URL
./lightpanda fetch --dump https://lightpanda.io
info(browser): GET https://lightpanda.io/ http.Status.ok
info(browser): fetch script https://api.website.lightpanda.io/js/script.js: http.Status.ok
info(browser): eval remote https://api.website.lightpanda.io/js/script.js: TypeError: Cannot read properties of undefined (reading 'pushState')
<!DOCTYPE html>
Start a CDP server
To control Lightpanda with CDP clients like Playwright or Puppeteer , you need to start the browser as a CDP server.
./lightpanda serve --host 127.0.0.1 --port 9222
info(websocket): starting blocking worker to listen on 127.0.0.1:9222
info(server): accepting new conn...
Connect with Puppeteer
Once the CDP server started, you can run a Puppeteer
script by configuring the browserWSEndpoint
.
'use strict'
import puppeteer from 'puppeteer-core'
// use browserWSEndpoint to pass the Lightpanda's CDP server address.
const browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:9222",
})
// The rest of your script remains the same.
const context = await browser.createBrowserContext()
const page = await context.newPage()
// Dump all the links from the page.
await page.goto('https://wikipedia.com/')
const links = await page.evaluate(() => {
return Array.from(document.querySelectorAll('a')).map(row => {
return row.getAttribute('href')
})
})
console.log(links)
await page.close()
await context.close()
await browser.disconnect()
Connect with Playwright
Try Lightpanda with Playwright by using
chromium.connectOverCDP
to connect.
import { chromium } from 'playwright';
// use connectOverCDP to pass the Lightpanda's CDP server address.
const browser = await chromium.connectOverCDP('ws://127.0.0.1:9222');
// The rest of your script remains the same.
const context = await browser.newContext({});
const page = await context.newPage();
await page.goto('https://wikipedia.com/');
const title = await page.locator('h1').textContent();
console.log(title);
await page.close();
await context.close();
await browser.close();
Connect with ChromeDP
Use Lightpanda with Chromedp , a Golang client for CDP servers.
package main
import (
"context"
"flag"
"log"
"github.com/chromedp/chromedp"
)
func main() {
ctx, cancel = chromedp.NewRemoteAllocator(ctx,
"ws://127.0.0.1:9222", chromedp.NoModifyURL,
)
defer cancel()
ctx, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
var title string
if err := chromedp.Run(ctx,
chromedp.Navigate("https://wikipedia.com/"),
chromedp.Title(&title),
); err != nil {
log.Fatalf("Failed getting page's title: %v", err)
}
log.Println("Got title of:", title)
}