Browserless Example
@tod
code:
data:privatelast updated:4 years agoarchived
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
post_to_hacker_news
auth
to use OAuth tokens and API keys in code via theauths object
(auths.browserless)
params
NewStories
[0]:
 
array ·params.newStories
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}
61
const acct = process.env.HACKERNEWS_USERNAME3
const pw = process.env.HACKERNEWS_PASS3
const newStories = params.newStories

// See the browserless docs for more info:
// https://docs.browserless.io/
const puppeteer = require('puppeteer-core') 

const browser = await puppeteer.connect({ 
  browserWSEndpoint: `wss://chrome.browserless.io?token=${auths.browserless.api_key}` 
})
const page = await browser.newPage()

await page.goto(`https://news.ycombinator.com/login`)

// enter username into login form
usernameField = await page.$('input[name="acct"]')
await usernameField.type(acct)
console.log(`submitted username at ${new Date()}`)
// enter password into login form
passwordField = await page.$('input[name="pw"]')
await passwordField.type(pw)
console.log(`submitted password at ${new Date()}`)
// submit login form
submitButton = await page.$('input[type=submit]')
await Promise.all([
  page.waitForNavigation(),
  submitButton.click(),
]);
console.log(`clicked on login button and redirected at ${new Date()}`)

for(story of newStories) {
  if (story.title && story.link) {
    console.log(story.title)
    console.log(story.link)
    await page.goto(`https://news.ycombinator.com/submit`,{waitUntil: 'domcontentloaded'})
    console.log(`Goto submit at ${new Date()}`)
    // enter story title (cannot be longer than 80 characters)
    if (story.title.toString().length > 80)
      storyTitle = story.title.toString().substring(0, 79)
    else
      storyTitle = story.title.toString()
    await page.type('input[name="title"]', storyTitle)
    console.log(`Typed title at ${new Date()}`)
    // enter story url
    await page.type('input[name="url"]', story.link.toString())
    console.log(`Typed url at ${new Date()}`)
    // submit form
    const screenshot = await page.screenshot()
    this.screenshot = Buffer.from(screenshot, 'binary').toString('base64')
    await Promise.all([
      page.waitForNavigation(),
      page.click('input[type=submit]'),
    ]);
    console.log(`Clicked submit and redirected at ${new Date()}`)
  }
}

await browser.close()