@@ -25,33 +25,59 @@ You will find here are a couple of examples of how to use Lightpanda with Trigge
25
25
In this task, we use Lightpanda browser to get links from a provided URL.
26
26
You will have to pass the URL as a payload when triggering the task.
27
27
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
+
28
33
``` 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'
30
36
31
37
export 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
+ }
35
55
56
+ // Connect to Lightpanda's cloud
36
57
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 } ` ,
38
59
})
39
60
const context = await browser .createBrowserContext ()
40
61
const page = await context .newPage ()
41
62
42
63
// Dump all the links from the page.
43
- await page .goto (url )
64
+ await page .goto (payload . url )
44
65
45
66
const links = await page .evaluate (() => {
46
67
return Array .from (document .querySelectorAll (' a' )).map (row => {
47
68
return row .getAttribute (' href' )
48
69
})
49
70
})
50
71
72
+ logger .info (' Processing done' )
73
+ logger .info (' Shutting down…' )
74
+
51
75
await page .close ()
52
76
await context .close ()
53
77
await browser .disconnect ()
54
78
79
+ logger .info (' ✅ Completed' )
80
+
55
81
return {
56
82
links ,
57
83
}
@@ -99,8 +125,17 @@ export const lightpandaFetch = task({
99
125
throw new Error (' payload.url is undefined' )
100
126
}
101
127
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
+
102
135
const e = execSync (` ${process .env .LIGHTPANDA_BROWSER_PATH } fetch --dump ${payload .url } ` )
103
136
137
+ logger .info (' ✅ Completed' )
138
+
104
139
return {
105
140
message: e .toString (),
106
141
}
@@ -163,7 +198,6 @@ export const lightpandaCDP = task({
163
198
logger .warn (' Please define the env variable $LIGHTPANDA_BROWSER_PATH' , {
164
199
env: process .env ,
165
200
})
166
-
167
201
throw new Error (' $LIGHTPANDA_BROWSER_PATH is undefined' )
168
202
}
169
203
0 commit comments