4. Go to production
Use Lightpanda’s cloud offer to switch from a local browser to a remotely managed version.
Create a new account and an API token here .
To connect, the script will use an environment variable named LPD_TOKEN.
First export the variable with your token.
export LPD_TOKEN="paste your token here"Edit index.js to change the Puppeteer connection options:
const puppeteeropts = {
browserWSEndpoint: 'wss://euwest.cloud.lightpanda.io/ws?token=' + process.env.LPD_TOKEN,
};Clean up local-only lines
You no longer need to start a local browser process because you are using the cloud version. You can remove these parts of the script to simplify it:
const { lightpanda } = require('@lightpanda/browser')const lpdopts = {
host: '127.0.0.1',
port: 9222,
}; // Start Lightpanda browser in a separate process.
const proc = await lightpanda.serve(lpdopts); // Stop Lightpanda browser process.
proc.stdout.destroy();
proc.stderr.destroy();
proc.kill();Final version
Here is the final script using the cloud browser version:
'use strict'
const puppeteer = require('puppeteer-core');
const puppeteeropts = {
browserWSEndpoint: 'wss://euwest.cloud.lightpanda.io/ws?token=' + process.env.LPD_TOKEN,
};
(async () => {
// Connect Puppeteer to the browser.
const browser = await puppeteer.connect(puppeteeropts);
const context = await browser.createBrowserContext();
const page = await context.newPage();
// Go to hackernews home page.
await page.goto("https://news.ycombinator.com/");
// Find the search box at the bottom of the page and type the term lightpanda
// to search.
await page.type('input[name="q"]','lightpanda');
// Press enter key to run the search.
await page.keyboard.press('Enter');
// Wait until the search results are loaded on the page, with a 5 seconds
// timeout limit.
await page.waitForFunction(() => {
return document.querySelector('.Story_container') != null;
}, {timeout: 5000});
// Loop over search results to extract data.
const res = await page.evaluate(() => {
return Array.from(document.querySelectorAll('.Story_container')).map(row => {
return {
// Extract the title.
title: row.querySelector('.Story_title span').textContent,
// Extract the URL.
url: row.querySelector('.Story_title a').getAttribute('href'),
// Extract the list of meta data.
meta: Array.from(row.querySelectorAll('.Story_meta > span:not(.Story_separator, .Story_comment)')).map(row => {
return row.textContent;
}),
}
});
});
// Display the result.
console.log(res);
// Disconnect Puppeteer.
await page.close();
await context.close();
await browser.disconnect();
})();Interested in on premise deployment?
The core Lightpanda browser will always remain open source, including JavaScript execution, CDP compatibility, proxy support, and request interception.
If you require on premise deployment, proprietary licensing, or enterprise features such as multi-context tabs and sandboxing, reach out to us at hello@lightpanda.io.