@@ -25,33 +25,59 @@ You will find here are a couple of examples of how to use Lightpanda with Trigge
2525In this task, we use Lightpanda browser to get links from a provided URL.
2626You will have to pass the URL as a payload when triggering the task.
2727
28+ Make sure to add ` $LIGHTPANDA_TOKEN ` to your Trigger.dev dashboard on the Environment Variables page:
29+ ``` bash
30+ LIGHTPANDA_TOKEN: " <your-token>" ,
31+ ```
32+
2833``` ts trigger/lightpanda-cloud-puppeteer.ts
29- import puppeteer from " puppeteer"
34+ import { logger , task } from ' @trigger.dev/sdk/v3'
35+ import puppeteer from ' puppeteer'
3036
3137export const lightpandaCloudPuppeteer = task ({
32- id: " lightpanda-cloud-puppeteer" ,
33- run : async (payload : { url: string }) => {
34- const { url } = payload
38+ id: ' lightpanda-cloud-puppeteer' ,
39+ machine: {
40+ preset: ' micro' ,
41+ },
42+ run : async (payload : { url: string }, { ctx }) => {
43+ logger .log (" Lets get a page's links with Lightpanda!" , { payload , ctx })
44+ if (! payload .url ) {
45+ logger .warn (' Please define the payload url' )
46+ throw new Error (' payload.url is undefined' )
47+ }
48+
49+ if (typeof process .env .LIGHTPANDA_TOKEN === ' undefined' ) {
50+ logger .warn (' Please define the env variable $LIGHTPANDA_TOKEN' , {
51+ env: process .env ,
52+ })
53+ throw new Error (' $LIGHTPANDA_TOKEN is undefined' )
54+ }
3555
56+ // Connect to Lightpanda's cloud
3657 const browser = await puppeteer .connect ({
37- browserWSEndpoint: " wss://cloud.lightpanda.io/ws?browser=lightpanda&token=TOKEN " ,
58+ browserWSEndpoint: ` wss://cloud.lightpanda.io/ws?browser=lightpanda&token=${ process . env . LIGHTPANDA_TOKEN } ` ,
3859 })
3960 const context = await browser .createBrowserContext ()
4061 const page = await context .newPage ()
4162
4263 // Dump all the links from the page.
43- await page .goto (url )
64+ await page .goto (payload . url )
4465
4566 const links = await page .evaluate (() => {
4667 return Array .from (document .querySelectorAll (' a' )).map (row => {
4768 return row .getAttribute (' href' )
4869 })
4970 })
5071
72+ logger .info (' Processing done' )
73+ logger .info (' Shutting down…' )
74+
5175 await page .close ()
5276 await context .close ()
5377 await browser .disconnect ()
5478
79+ logger .info (' ✅ Completed' )
80+
5581 return {
5682 links ,
5783 }
@@ -99,8 +125,17 @@ export const lightpandaFetch = task({
99125 throw new Error (' payload.url is undefined' )
100126 }
101127
128+ if (typeof process .env .LIGHTPANDA_BROWSER_PATH === ' undefined' ) {
129+ logger .warn (' Please define the env variable $LIGHTPANDA_BROWSER_PATH' , {
130+ env: process .env ,
131+ })
132+ throw new Error (' $LIGHTPANDA_BROWSER_PATH is undefined' )
133+ }
134+
102135 const e = execSync (` ${process .env .LIGHTPANDA_BROWSER_PATH } fetch --dump ${payload .url } ` )
103136
137+ logger .info (' ✅ Completed' )
138+
104139 return {
105140 message: e .toString (),
106141 }
@@ -163,7 +198,6 @@ export const lightpandaCDP = task({
163198 logger .warn (' Please define the env variable $LIGHTPANDA_BROWSER_PATH' , {
164199 env: process .env ,
165200 })
166-
167201 throw new Error (' $LIGHTPANDA_BROWSER_PATH is undefined' )
168202 }
169203
0 commit comments