1.0.1 • Published 4 months ago

@vnxdev/puppeteer v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

@vnxdev/puppeteer

🚀 A NestJS package for internal use at vnxdev
Easily integrate with NestJS projects using this package.

📦 Installation

yarn add @vnxdev/puppeteer
# or
npm install @vnxdev/puppeteer

🚀 Usage

Puppeteer Service for NestJS

import { Injectable } from "@nestjs/common";
import { Browser, Page, PuppeteerService } from "@vnxdev/puppeteer";

@Injectable()
export class MyService {
  constructor(private readonly puppeteerService: PuppeteerService) {}

  async demo() {
    try {
      const defaultLaunchOptions = this.puppeteerService.getLaunchOptions();

      // view defaultLaunchOptions
      console.log("~ defaultLaunchOptions:", defaultLaunchOptions);

      // update headless
      this.puppeteerService.updateLaunchOptions({
        headless: false,
      });

      // use proxy
      this.puppeteerService.updateLaunchOptions({
        args: defaultLaunchOptions.args.push(`--proxy-server=${host}:${port}`),
      });

      const browser: Browser = await this.puppeteerService.getBrowser();

      // Tạo thêm tab mới hoặc làm gì đó
      const page: Page = await browser.newPage();

      await this.puppeteerService.navigateSafely(
        page,
        "https://www.google.com"
      );

      await new Promise((resolve) => setTimeout(resolve, 1000));

      await page.click("textarea");

      // sleep 1s
      await new Promise((resolve) => setTimeout(resolve, 1000));

      await this.puppeteerService.humanType(page, "How to use puppeteer!");

      // sleep 1s
      await new Promise((resolve) => setTimeout(resolve, 1000));

      await page.keyboard.press("Enter");

      // sleep 5s
      await new Promise((resolve) => setTimeout(resolve, 5000));

      await page?.close();
    } catch (error) {
      console.log("Error:", error?.message);
    }
  }
}

Default Browser Launch Options

The service initializes the browser with the following options:

const defaultLauchOptions: LaunchOptions = {
  headless: true,
  defaultViewport: null,
  slowMo: 30,
  executablePath: process.env.CHROME_BIN || undefined,
  userDataDir: "puppeteer_data",
  args: [
    "--disable-infobars",
    "--window-position=0,0",
    "--ignore-certifcate-errors",
    "--ignore-certifcate-errors-spki-list",
    "--no-sandbox",
    "--disable-setuid-sandbox",
    "--disable-dev-shm-usage",
    "--disable-accelerated-2d-canvas",
    "--disable-gpu",
    "--window-size=1080,1080",
    "--hide-scrollbars",
    "--disable-notifications",
    "--aggressive-cache-discard",
    "--disable-cache",
    "--disable-application-cache",
    "--disable-offline-load-stale-cache",
    "--disable-gpu-shader-disk-cache",
    "--media-cache-size=0",
    "--disk-cache-size=0",
  ],
};

Human-Like Mouse Movement

The service integrates humanMouseMove and drawBezierMovement from common/mouseUtils.ts to simulate natural cursor movements, reducing the likelihood of bot detection.

Process Monitoring

It uses pidusage to monitor browser resource consumption, ensuring efficiency in automation tasks.

📜 License

This package is licensed under the MIT License. See LICENSE for details.