Skip to content

Easy manage and instance a puppeteer using a singleton pattern. Minimal configuration implemented for install and use.

License

Notifications You must be signed in to change notification settings

devalexanderdaza/puppeteer-extends

Repository files navigation

puppeteer-extends

License Build Status NPM Package semantic-release

Easy manage and instance a puppeteer using a singleton pattern. Minimal configuration implemented for install and use.

Install

npm install puppeteer-extends

Use

const { PuppeteerExtends, Logger } = require("puppeteer-extends");
const path = require("path");
const { Browser, Page } = require("puppeteer");

// Define your custom browser arguments
const customArguments = [
  "--no-sandbox",
  "--disable-web-security",
  "--disable-setuid-sandbox",
  "--aggressive-cache-discard",
  "--disable-cache",
  "--disable-infobars",
  "--disable-application-cache",
  "--window-position=0,0",
  "--disable-offline-load-stale-cache",
  "--disk-cache-size=0",
  "--disable-background-networking",
  "--disable-default-apps",
  "--disable-extensions",
  "--disable-sync",
  "--disable-translate",
  "--hide-scrollbars",
  "--metrics-recording-only",
  "--mute-audio",
  "--no-first-run",
  "--safebrowsing-disable-auto-update",
  "--ignore-certificate-errors",
  "--ignore-ssl-errors",
  "--ignore-certificate-errors-spki-list",
];

// Folder of puppeteer store data
const puppeteerDataDirectory = path.join(__dirname, "./tmp/puppeteer-extends");

const main = async () => {
  // Set custom parameters on init browser instance
  const browser = await PuppeteerExtends.getBrowser({
    isHeadless: true,
    isDebug: false,
    customArguments: customArguments,
    userDataDir: puppeteerDataDirectory,
  });
  if (browser) {
    const page = await browser.newPage();

    // * An example of crawling a page with CloudFlare applied.
    Logger.debug("🚧  Crawling in progress...");

    const url = "https://github.com/devalexanderdaza";
    await PuppeteerExtends.goto(page, url);
    await page.screenshot({
      path: path.join(__dirname, "./tmp/puppeteer-extends/example-js.png"),
    });

    Logger.debug("🚧  Crawling is complete.");
    Logger.debug("🚧  Closing page...");
    PuppeteerExtends.closePage(page);
    Logger.debug("🚧  Exit the Puppeteer...");
    PuppeteerExtends.closeBrowser(browser);
  }
};

main().then();

TypeScript support

import { PuppeteerExtends, Logger } from "puppeteer-extends";
import path from "path";
import { Browser, Page } from "puppeteer";

// Define your custom browser arguments
const customArguments: string[] = [
  "--no-sandbox",
  "--disable-web-security",
  "--disable-setuid-sandbox",
  "--aggressive-cache-discard",
  "--disable-cache",
  "--disable-infobars",
  "--disable-application-cache",
  "--window-position=0,0",
  "--disable-offline-load-stale-cache",
  "--disk-cache-size=0",
  "--disable-background-networking",
  "--disable-default-apps",
  "--disable-extensions",
  "--disable-sync",
  "--disable-translate",
  "--hide-scrollbars",
  "--metrics-recording-only",
  "--mute-audio",
  "--no-first-run",
  "--safebrowsing-disable-auto-update",
  "--ignore-certificate-errors",
  "--ignore-ssl-errors",
  "--ignore-certificate-errors-spki-list",
];

// Folder of puppeteer store data
const puppeteerDataDirectory = path.join(__dirname, "./tmp/puppeteer-extends");

const main = async (): Promise<void> => {
  // Set custom parameters on init browser instance
  const browser: Browser | undefined = await PuppeteerExtends.getBrowser({
    isHeadless: true,
    isDebug: false,
    customArguments: customArguments,
    userDataDir: puppeteerDataDirectory,
  });
  if (browser) {
    const page: Page = await browser.newPage();

    // * An example of crawling a page with CloudFlare applied.
    Logger.debug("🚧  Crawling in progress...");

    const url: string = "https://github.com/devalexanderdaza";
    await PuppeteerExtends.goto(page, url);
    await page.screenshot({
      path: path.join(__dirname, "./tmp/puppeteer-extends/example.png"),
    });

    Logger.debug("🚧  Crawling is complete.");
    Logger.debug("🚧  Closing page...");
    PuppeteerExtends.closePage(page);
    Logger.debug("🚧  Exit the Puppeteer...");
    PuppeteerExtends.closeBrowser(browser);
  }
};

main().then();