← back to Delivery Address Fix

scripts/enter-code.js

35 lines

// Enter a 2FA / verification code into whatever code UI the page shows:
// N separate one-digit boxes, or a single text input. Then click Next/Submit.
// Usage: node enter-code.js --port 9223 --match uber.com --code 1234
const { parseArgs, loadConfig, platform, sessionDir, findTab, dumpState } = require('./lib');

(async () => {
  const args = parseArgs(process.argv);
  const cfg = loadConfig();
  const code = args.code;
  if (!code || !/^\d{3,8}$/.test(code)) { console.error('Need --code <3-8 digits>'); process.exit(2); }
  const match = args.match || (args.platform && platform(args.platform).match);
  if (!match) { console.error('Need --match <url-substring> or --platform'); process.exit(2); }

  const { browser, page } = await findTab(args.port || cfg.cdp_port || 9223, match);
  const inputs = await page.$$('input:visible');
  console.log('visible inputs:', inputs.length);
  if (inputs.length >= code.length) {
    for (let i = 0; i < code.length; i++) await inputs[i].fill(code[i]);
  } else if (inputs.length >= 1) {
    await inputs[0].click();
    await page.keyboard.type(code, { delay: 120 });
  } else {
    console.error('No visible input to enter the code into — run state.js first.');
    process.exit(2);
  }
  await page.waitForTimeout(1500);
  for (const label of ['Next', 'Submit', 'Verify', 'Continue', 'Sign in']) {
    const btn = await page.$(`button:has-text("${label}")`);
    if (btn) { await btn.click().catch(() => {}); break; }
  }
  await page.waitForTimeout(6000);
  await dumpState(page, sessionDir(cfg, args), 'after-code');
  await browser.close();
})().catch(e => { console.error('ERR', e.message); process.exit(1); });