Untitled

 avatar
unknown
javascript
2 years ago
2.7 kB
6
Indexable
import puppeteer from 'puppeteer'

const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min

const scale = async (page, num, min = 2, max = 5) => {
  const choice = random(min, max)
  const elements = await page.$$('.T5pZmf')
  const index = num * 5 + choice - 1
  await elements[index].click()
}

const checkbox = async (page, num, min = 1, max = 3) => {
  const picks = 1//random(min, max)
  const select = new Set()

  for (let i = 0; i < picks; i++) {
    let choice = random(min, max)
    while (select.has(choice)) choice = random(min, max)
    select.add(choice)
  }

  const elements = await page.$$('.Yri8Nb')

  for (const selectElement of select) {
    const index = num * 4 + selectElement - 1
    await elements[index].click()
  }
}

const yesNo = async (page, num) => {
  const choice = random(1, 10) > 2 ? 1 : 2
  const elements = await page.$$('.ajBQVb')
  const index = num * 2 + choice - 1
  await elements[index].click()
}

const start = async () => {
  console.log('Starting...')
  const startTime = performance.now()
  const browser = await puppeteer.launch({
    headless: false, // change to true to see the browser
    // change this to your Chromium path. Commenting it sometimes works, so it's not necessary
    executablePath: '/opt/homebrew/bin/chromium'
  })
  const page = await browser.newPage()

  const TOTAL_RESPONSES = 10

  for (let i = 0; i < TOTAL_RESPONSES; i++) {
    try {
      await page.goto('https://forms.gle/s94Mw7iHocsoex446')
      await page.waitForNavigation({waitUntil: 'networkidle2'})

      const questions = await page.$$('.Qr7Oae')

      const TOTAL_SCALE_QUESTIONS = 3
      let scaleQuestion = 0
      const TOTAL_CHECKBOX_QUESTIONS = 2
      let checkboxQuestion = 0
      const TOTAL_YES_NO_QUESTIONS = 2
      let yesNoQuestion = 0

      for (let i = 0; i < questions.length; i++) {
        if ([0, 4, 6].includes(i))
          await scale(page, scaleQuestion++ % TOTAL_SCALE_QUESTIONS, 1, 5)
        else if ([1, 3].includes(i))
          await checkbox(page, checkboxQuestion++ % TOTAL_CHECKBOX_QUESTIONS, 1, 3)
        else if ([2, 5].includes(i))
          await yesNo(page, yesNoQuestion++ % TOTAL_YES_NO_QUESTIONS)
      }

      await page.click('.lRwqcd div')
      await page.waitForNavigation({waitUntil: 'networkidle2'})
      console.log(`Submitted ${i + 1} of ${TOTAL_RESPONSES}`)
    } catch (e) {
      console.log(`Failed to submit form #${i + 1}`)
    }
  }

  await browser.close()
  const endTime = performance.now()
  const formatDifference = (endTime - startTime) / 1000
  console.log(`Generated ${TOTAL_RESPONSES} responses in ${formatDifference}ms`)
}

start()
Editor is loading...