npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

2fa-adb-tools

v1.0.0

Published

Capture 2FA verification codes from Android notifications for automated testing with Playwright

Readme

2FA ADB Tools

A Node.js library for automatically capturing 2FA verification codes from Android notifications using ADB and OCR, with seamless Playwright integration for end-to-end testing.

npm version License: MIT

📋 Table of Contents

✨ Features

  • Automatic 2FA Capture: Monitors Android notifications and extracts verification codes using OCR
  • Playwright Integration: Built-in helpers for seamless end-to-end testing
  • Customizable Gestures: Configure swipe coordinates and intervals for notification dismissal
  • Smart Management: Automatic screenshot cropping, cleanup, and storage with deduplication

📦 Installation

npm install 2fa-adb-tools

Peer Dependency (for Playwright)

npm install --save-dev @playwright/test

🔧 Prerequisites

Android Setup

  1. Enable USB Debugging on your Android device
  2. Install ADB (Android Debug Bridge)

Install ADB

  • Windows: Download Android Platform Tools
  • macOS: brew install android-platform-tools
  • Linux: sudo apt-get install adb (Debian/Ubuntu)

Verify Connection

adb devices
# Should show your device as "device"

🚀 Quick Start

Standalone Usage

const { TwoFAHelper } = require('2fa-adb-tools');

const helper = new TwoFAHelper({ screenshotInterval: 3000 });
await helper.startMonitoring();

try {
    const code = await helper.waitForCode(60000);
    console.log(`2FA code: ${code}`);
} finally {
    await helper.stopMonitoring();
}

With Playwright

const { test, expect } = require('@playwright/test');
const { TwoFAHelper, waitFor2FACode } = require('2fa-adb-tools');

test.describe('2FA Test', () => {
    let twoFAHelper;

    test.beforeAll(async () => {
        twoFAHelper = new TwoFAHelper({ screenshotInterval: 3000 });
        await twoFAHelper.startMonitoring();
    });

    test.afterAll(async () => {
        await twoFAHelper.stopMonitoring();
    });

    test('login with 2FA', async ({ page }) => {
        await page.goto('https://example.com/login');
        await page.fill('#username', process.env.USERNAME);
        await page.fill('#password', process.env.PASSWORD);
        await page.click('#submit');

        const code = await waitFor2FACode(page, twoFAHelper, {
            codeInputSelector: '#code-input',
            autoFill: true
        });

        await expect(page.locator('.success-message')).toBeVisible();
    });
});

📚 API Reference

TwoFAHelper

Main class for Playwright integration and simplified 2FA code management.

| Method | Description | |--------|-------------| | startMonitoring() | Begin monitoring for 2FA codes | | stopMonitoring() | Stop monitoring and cleanup | | waitForCode(timeout) | Wait for next code (returns Promise) |

ADBMonitor

Low-level class for direct ADB interaction.

| Event | Description | |-------|-------------| | code | Emitted when a verification code is found | | error | Emitted on errors |

OCRProcessor

Handles text recognition from images.

const ocr = new OCRProcessor({ languages: 'rus+eng' });
await ocr.init();
const text = await ocr.recognize('./screenshot.png');
const code = ocr.processText(text);
await ocr.terminate();

ScreenshotManager

Manages screenshot capture and storage.

const sm = new ScreenshotManager({
    adbPath: 'adb',
    deviceId: 'device-id',
    maxScreenshots: 50
});

const screenshot = await sm.takeScreenshot();
const cropped = await sm.cropScreenshot(screenshot);
sm.saveCodeScreenshot(screenshot, cropped, '123456');

⚙️ Configuration

TwoFAHelper Options

{
    // Monitoring
    screenshotInterval: 3000,      // How often to take screenshots (ms)
        autoSwipe: true,               // Enable/disable all swipes

        // Swipe behavior
        swipeAfterCode: true,          // Swipe after finding a code
        swipeInterval: 10000,          // Auto-swipe every 10 seconds (0 = disable)

        // Swipe coordinates
        swipeConfig: {
        startX: 150, startY: 1200,   // Start position
            endX: 950, endY: 1200,       // End position
            duration: 400                 // Swipe duration (ms)
    },

    // OCR crop region (where codes appear)
    cropConfig: {
        top: 930, bottom: 100,        // Vertical crop
            left: 100, right: 100         // Horizontal crop
    },

    // Advanced
    ocrLanguages: 'rus+eng',        // Tesseract languages
        deviceId: null,                  // Specific device ID
        maxScreenshots: 50               // Screenshots to keep
}

Finding Coordinates

  1. Enable Pointer Location in Developer Options on Android
  2. Note the coordinates for your swipe gesture
  3. Adjust swipeConfig accordingly

Optimizing Crop Region

Adjust cropConfig to focus only on the area where 2FA codes appear in notifications.

🔍 Troubleshooting

| Issue | Solution | |-------|----------| | ADB not found | Install Android platform-tools and add to PATH | | No devices | Run adb devices, enable USB debugging, re-authorize device | | OCR fails | Adjust cropConfig, improve screenshot quality, check contrast | | Swipe not working | Verify coordinates, increase duration, ensure screen is unlocked |

Debug Mode

// Enable verbose logging
process.env.DEBUG = '2fa-adb-tools:*';

// Log events
helper.on('code', console.log);
helper.on('error', console.error);

🤖 AI Disclosure

This project was developed with assistance from AI language models (DeepSeek) for code generation, documentation, and refactoring. The core logic, architecture decisions, and final code quality were reviewed, tested and approved by a human developer.

📄 License

MIT © pifpafi4 - делайте что хотите, я за код не отвечаю.