auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params, auths) => {
}
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()