Skip to content

Complete Browser Automation Suite with stealth mode, SSL handling, auto-detection, dialogs, console and network monitoring, drag & drop, iframes, mobile emulation, performance metrics, geolocation, downloads, and more.

Notifications You must be signed in to change notification settings

alsocoders/apna-browser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apna Browser

All-in-one Puppeteer-based browser automation manager with 25+ battle-tested utilities: stealth browsing, smart tab tracking, dialogs, clipboard, console + network monitoring, downloads, performance metrics, mobile emulation, geolocation, and more.

Installation

npm install apna-browser

Requires Node.js 18 or newer. Puppeteer is installed automatically.

Quick Start

import ApnaBrowser from 'apna-browser';

const browser = new ApnaBrowser();

await browser.launchBrowser({
  stealth: true,
  ignoreSSLErrors: true,
});

await browser.openNewTab('https://www.google.com');
await browser.typeText('textarea[name="q"]', 'Apna Browser automation');
await browser.click('input[name="btnK"]');
await browser.wait(5000);
await browser.takeScreenshot('google-search.png');

await browser.closeBrowser();

Highlights

  • Stealth-ready launch options that bypass common bot detections
  • Automatic tab tracking and tab management helpers
  • Rich interaction helpers (typing, clicking, drag-and-drop, hover, iframes, clipboard)
  • Built-in console log and network request capture
  • Performance, device emulation, geolocation and download helpers
  • Sensible Hindi-friendly logging out of the box

Table of Contents

Advanced Usage

Launch, Tabs & Navigation

import ApnaBrowser from 'apna-browser';

const browser = new ApnaBrowser();

await browser.launchBrowser({
  stealth: true,
  ignoreSSLErrors: true,
  autoDetect: true,
});

await browser.openNewTab('https://news.ycombinator.com');
await browser.openNewTab('https://github.com/alsocoders');

await browser.switchToTab(0);
await browser.reload();
await browser.nextTab();
await browser.navigateTo('https://alsocoder.com');

Dialogs, Console & Network Monitoring

browser.enableAutoDialogHandler({
  alert: 'accept',
  confirm: 'accept',
  prompt: 'accept',
  promptText: 'Namaste!',
});

await browser.enableNetworkMonitoring();
browser.setupConsoleCapture(browser.page);

await browser.click('#trigger-dialog');

const consoleErrors = browser.getConsoleErrors();
const recentRequests = browser.getNetworkRequests({ limit: 5 });

Interactions, Clipboard & Elements

await browser.typeText('#email', 'user@alsocoder.com');
await browser.typeText('#password', 'super-secret');
await browser.click('#login');
await browser.waitForNavigation();

await browser.selectText('.profile-name');
const copiedName = await browser.copySelectedText();

const linkCount = await browser.countElements('a');
const isVisible = await browser.isElementVisible('#dashboard');

Performance, Screenshots & Downloads

await browser.getPerformanceMetrics();
browser.printPerformanceMetrics();

await browser.takeScreenshot('dashboard.png');
await browser.generatePDF('dashboard.pdf');

await browser.enableDownloads('./downloads');
await browser.click('#download-report');
await browser.waitForDownload(5000);

Mobile Emulation, Geolocation & Devices

await browser.setViewport(390, 844);
await browser.emulateMobile('iPhone 13 Pro');

await browser.grantGeolocationPermission();
await browser.setGeolocation(28.6139, 77.2090); // New Delhi
await browser.reload();

Iframes, Drag & Drop, Keyboard Shortcuts

await browser.clickInFrame('#ad-frame', 'button.close');
await browser.dragAndDrop('#item-1', '#dropzone');

await browser.pressKeyCombination(['Control', 'Shift', 'KeyT']);
await browser.ctrlF();
await browser.escape();

Cleanup

await browser.closeAllTabs();
await browser.closeBrowser();

Complete Usage Reference

Each snippet assumes you created an instance via:

import ApnaBrowser from 'apna-browser';
const browser = new ApnaBrowser();

Browser Lifecycle

await browser.launchBrowser({ headless: false, stealth: true });
await browser.enableAutoDetection();
await browser.refreshTabs();
await browser.closeBrowser();

Tabs & Contexts

await browser.openNewTab('https://example.com');
await browser.switchToTab(0);
await browser.duplicateCurrentTab();
await browser.closeCurrentTab();
await browser.closeAllTabs();

const tabs = await browser.getAllTabs();

const { page: incognitoPage, context } = await browser.openIncognitoTab('https://alsocoder.com');
await context.close();

Navigation & Waiting

await browser.navigateTo('https://alsocoder.com/blog');
await browser.waitForNavigation();
await browser.goBack();
await browser.goForward();
await browser.reload();
await browser.wait(1500);
await browser.waitForSelector('#hero');

Dialog Handling

await browser.handleAlert(true);
await browser.handleConfirm(false);
await browser.handlePrompt('Namaste', true);

browser.enableAutoDialogHandler({
  alert: 'accept',
  confirm: 'dismiss',
  prompt: 'accept',
  promptText: 'नमस्ते',
});

browser.disableAutoDialogHandler();

Interaction Helpers

await browser.typeText('#search', 'Apna Browser rocks!');
await browser.click('.submit');
await browser.doubleClick('.card');
await browser.rightClick('.card');
await browser.hover('.menu');
await browser.hoverAndClick('.menu', '.menu-item:last-child');
await browser.dragAndDrop('#item-1', '#drop-target');

Clipboard & Form Utilities

await browser.selectText('article h1');
const headline = await browser.copySelectedText();
await browser.selectAll();
await browser.copy();
await browser.cut();
await browser.paste();
await browser.pasteIntoField('#notes', 'Copied content');

await browser.selectOption('#country', 'IN');
await browser.setCheckbox('#terms', true);
await browser.uploadFile('input[type="file"]', './assets/resume.pdf');
await browser.focus('#comments');

Element Info & Queries

const exists = await browser.elementExists('.ticket');
const titleText = await browser.getText('.title');
const href = await browser.getAttribute('.cta', 'href');
const count = await browser.countElements('a');
const allHeadings = await browser.getAllText('h2');
const visible = await browser.isElementVisible('#status');

Evaluation & Scrolling

await browser.evaluate(() => window.dispatchEvent(new Event('resize')));
await browser.scrollTo(0, 500);
await browser.scrollToBottom();
await browser.scrollToElement('#footer');

Screenshots & PDFs

await browser.takeScreenshot('full-page.png');
await browser.takeScreenshot('viewport.png', { fullPage: false });
await browser.generatePDF('report.pdf', { format: 'A4', printBackground: true });

Console & Network Monitoring

browser.setupConsoleCapture(browser.page);
await browser.enableNetworkMonitoring();
await browser.enableRequestBlocking(['image', 'font']);

browser.printConsoleLogs();
const errors = browser.getConsoleErrors();
const warnings = browser.getConsoleWarnings();

const requests = browser.getNetworkRequests({ type: 'xhr', limit: 10 });
browser.clearConsoleLogs();
browser.clearNetworkRequests();

Performance Metrics

const metrics = await browser.getPerformanceMetrics();
console.log(metrics.pageLoadTime);
browser.printPerformanceMetrics();

Storage & Cookies

const cookies = await browser.getCookies();
await browser.setCookie({ name: 'user', value: 'also-coder' });
await browser.clearCookies();

const localStorage = await browser.getLocalStorage();
await browser.clearLocalStorage();

Downloads & File Handling

await browser.enableDownloads('./tmp/downloads');
await browser.click('#download');
await browser.waitForDownload(10000);

Viewport, Devices & Geolocation

await browser.setViewport(1280, 800);
await browser.setUserAgent('Mozilla/5.0 (...custom UA...)');

await browser.emulateMobile('iPhone 13 Pro');
const availableDevices = browser.getAvailableDevices();

await browser.grantGeolocationPermission();
await browser.setGeolocation(12.9716, 77.5946); // Bengaluru

Keyboard Shortcuts

await browser.pressKey('Enter');
await browser.pressKeyCombination(['Control', 'Shift', 'KeyT']);
await browser.ctrlS();
await browser.ctrlF();
await browser.ctrlW();
await browser.ctrlT();
await browser.ctrlShiftT();
await browser.escape();
await browser.enter();
await browser.tab();

Frames & Multi-Context

await browser.switchToFrame('iframe#payment');
await browser.currentFrame.type('#card', '4242424242424242');
await browser.switchToMainFrame();

await browser.clickInFrame('iframe#ad', 'button.close');

Metrics & Utilities Combo

const dimensions = await browser.getPageDimensions();
console.log(dimensions.width, dimensions.height);

const url = await browser.getURL();
const title = await browser.getTitle();

API Overview

Category Key Methods
Browser launchBrowser, closeBrowser, enableAutoDetection, refreshTabs
Tabs openNewTab, switchToTab, nextTab, closeTab, duplicateCurrentTab
Navigation navigateTo, reload, goBack, goForward, waitForNavigation
Interactions typeText, click, doubleClick, hover, dragAndDrop, pressKeyCombination
Clipboard selectText, copySelectedText, pasteIntoField
Media takeScreenshot, generatePDF
Monitoring setupConsoleCapture, getConsoleLogs, enableNetworkMonitoring
Utilities getPerformanceMetrics, setViewport, emulateMobile, setGeolocation, enableDownloads

See lib/ApnaBrowser.js for the full API documentation via inline comments.

Example Script

The following script demonstrates a full-featured flow you can copy into your project:

import ApnaBrowser from 'apna-browser';

async function exampleDemo() {
  const manager = new ApnaBrowser();

  try {
    await manager.launchBrowser({ stealth: true, ignoreSSLErrors: true });

    manager.enableAutoDialogHandler({ alert: 'accept', confirm: 'accept' });

    await manager.openNewTab('https://www.google.com');
    await manager.wait(2000);

    await manager.openNewTab('https://www.github.com');
    await manager.wait(2000);

    await manager.openNewTab('https://www.youtube.com');
    await manager.wait(2000);

    await manager.switchToTab(0);
    await manager.typeText('textarea[name="q"]', 'Browser automation', { delay: 100 });
    await manager.wait(1000);
    await manager.click('input[name="btnK"]');
    await manager.wait(5000);

    try {
      await manager.selectText('h3');
      const copiedText = await manager.copySelectedText();
      console.log(`Copied: ${copiedText.substring(0, 50)}...`);
    } catch {
      console.log('Copy demo skipped');
    }

    await manager.getPerformanceMetrics();
    manager.printPerformanceMetrics();

    const linkCount = await manager.countElements('a');
    console.log(`Total links: ${linkCount}`);

    await manager.takeScreenshot('demo-screenshot.png');
  } catch (error) {
    console.error('Error:', error.message);
    await manager.takeScreenshot('error.png');
  } finally {
    await manager.closeBrowser();
  }
}

exampleDemo();

Support & Feedback

License

MIT © Also Coder · https://alsocoder.com
Use the package freely in your projects, but do not redistribute modified versions under the same name.

About

Complete Browser Automation Suite with stealth mode, SSL handling, auto-detection, dialogs, console and network monitoring, drag & drop, iframes, mobile emulation, performance metrics, geolocation, downloads, and more.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published