All Projects → tinovyatkin → nest-puppeteer

tinovyatkin / nest-puppeteer

Licence: MIT license
Puppeteer (Headless Chrome) provider for Nest.js

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nest-puppeteer

Phantomas
Headless Chromium-based web performance metrics collector and monitoring tool
Stars: ✭ 2,191 (+3122.06%)
Mutual labels:  headless-chrome, puppeteer
puppeteer-lambda
Module for using Headless-Chrome by Puppeteer on AWS Lambda.
Stars: ✭ 117 (+72.06%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Extra
💯 Teach puppeteer new tricks through plugins.
Stars: ✭ 3,397 (+4895.59%)
Mutual labels:  headless-chrome, puppeteer
puppeteer-instagram
Instagram automation driven by headless chrome.
Stars: ✭ 87 (+27.94%)
Mutual labels:  headless-chrome, puppeteer
prime-nestjs
A production-ready NestJS boilerplate using Typescript, Postgres, TypeORM, and Docker.
Stars: ✭ 140 (+105.88%)
Mutual labels:  nestjs, nestjs-library
Deno Puppeteer
A port of puppeteer running on Deno
Stars: ✭ 128 (+88.24%)
Mutual labels:  headless-chrome, puppeteer
CrawlerSamples
This is a Puppeteer+AngleSharp crawler console app samples, used C# 7.1 coding and dotnet core build.
Stars: ✭ 36 (-47.06%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Functions
Puppeteer Firebase Functions demo
Stars: ✭ 75 (+10.29%)
Mutual labels:  headless-chrome, puppeteer
codepen-puppeteer
Use Puppeteer to download pens from Codepen.io as single html pages
Stars: ✭ 22 (-67.65%)
Mutual labels:  headless-chrome, puppeteer
apify-cli
Apify command-line interface helps you create, develop, build and run Apify actors, and manage the Apify cloud platform.
Stars: ✭ 37 (-45.59%)
Mutual labels:  headless-chrome, puppeteer
Squidwarc
Squidwarc is a high fidelity, user scriptable, archival crawler that uses Chrome or Chromium with or without a head
Stars: ✭ 125 (+83.82%)
Mutual labels:  headless-chrome, puppeteer
puppeteer-autoscroll-down
Handle infinite scroll on websites by puppeteer
Stars: ✭ 40 (-41.18%)
Mutual labels:  headless-chrome, puppeteer
Awesome Puppeteer
A curated list of awesome puppeteer resources.
Stars: ✭ 1,728 (+2441.18%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Cluster
Puppeteer Pool, run a cluster of instances in parallel
Stars: ✭ 2,175 (+3098.53%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Dart
A Dart library to automate the Chrome browser over the DevTools Protocol. This is a port of the Puppeteer API
Stars: ✭ 92 (+35.29%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Examples
Puppeteer example scripts for running Headless Chrome from Node.
Stars: ✭ 2,781 (+3989.71%)
Mutual labels:  headless-chrome, puppeteer
Puppeteer Deep
Puppeteer, Headless Chrome;爬取《es6标准入门》、自动推文到掘金、站点性能分析;高级爬虫、自动化UI测试、性能分析;
Stars: ✭ 1,033 (+1419.12%)
Mutual labels:  headless-chrome, puppeteer
Page2image
📷 page2image is a npm package for taking screenshots which also provides CLI command
Stars: ✭ 66 (-2.94%)
Mutual labels:  headless-chrome, puppeteer
puppet-master
Puppeteer as a service hosted on Saasify.
Stars: ✭ 25 (-63.24%)
Mutual labels:  headless-chrome, puppeteer
after-work.js
[DEPRECATED] CLI for automated tests in web projects.
Stars: ✭ 56 (-17.65%)
Mutual labels:  headless-chrome, puppeteer

nest-puppeteer codecov

Description

This is a Puppeteer module for NestJS, making it easy to inject the Puppeteer into your project. It's modeled after the official modules, allowing for asynchronous configuration and such.

Installation

In your existing NestJS-based project:

npm install nest-puppeteer puppeteer
npm install -D @types/puppeteer

Usage

Overall, it works very similarly to any injectable module described in the NestJS documentation. You may want to refer to those docs as well -- and maybe the dependency injection docs too if you're still trying to wrap your head around the NestJS implementation of it.

Simple example

In the simplest case, you can explicitly specify options you'd normally provide to your puppeteer.launch or the instance name using PuppeteerModule.forRoot():

import { Module } from '@nestjs/common';
import { PuppeteerModule } from 'nest-puppeteer';

@Module({
  imports: [
    PuppeteerModule.forRoot(
      { pipe: true }, // optional, any Puppeteer launch options here or leave empty for good defaults */,
      'BrowserInstanceName', // optional, can be useful for using Chrome and Firefox in the same project
    ),
  ],
})
export class CatsModule {}

To inject the Puppeteer Browser object:

import type { Browser } from 'puppeteer';
import { Injectable } from '@nestjs/common';
import { InjectBrowser } from 'nest-puppeteer';
import { Cat } from './interfaces/cat';

@Injectable()
export class CatsRepository {
  constructor(@InjectBrowser() private readonly browser: Browser) {}

  async create(cat: Cat) {
    const version = await this.browser.version();
    return { version };
  }
}

To inject a new incognito BrowserContext object:

import { Module } from '@nestjs/common';
import { PuppeteerModule } from 'nest-puppeteer';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

@Module({
  imports: [PuppeteerModule.forFeature()],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}
import type { BrowserContext } from 'puppeteer';
import { Injectable } from '@nestjs/common';
import { InjectContext } from 'nest-puppeteer';
import { Cat } from './interfaces/cat';

@Injectable()
export class CatsRepository {
  constructor(
    @InjectContext() private readonly browserContext: BrowserContext,
  ) {}

  async create(cat: Cat) {
    const page = await this.browserContext.newPage();
    await page.goto('https://test.com/');
    return await page.content();
  }
}

Inject Page object:

import { Injectable } from '@nestjs/common';
import { InjectPage } from 'nest-puppeteer';
import type { Page } from 'puppeteer';

@Injectable()
export class CrawlerService {
  constructor(@InjectPage() private readonly page: Page) {}

  async crawl(url: string) {
    await this.page.goto(url, { waitUntil: 'networkidle2' });
    const content = await this.page.content();
    return { content };
  }
}

Asynchronous configuration

If you want to pass in Puppeteer configuration options from a ConfigService or other provider, you'll need to perform the Puppeteer module configuration asynchronously, using PuppeteerModule.forRootAsync(). There are several different ways of doing this.

Use a factory function

The first is to specify a factory function that populates the options:

import { Module } from '@nestjs/common'
import { PuppeteerModule } from 'nest-puppeteer'
import { ConfigService } from '../config/config.service'

@Module({
    imports: [PuppeteerModule.forRootAsync({
        imports: [ConfigModule],
        useFactory: (config: ConfigService) => {
            launchOptions: config.chromeLaunchOptions,
        },
        inject: [ConfigService]
    })]
})
export class CatsModule {}

Use a class

Alternatively, you can write a class that implements the PuppeteerOptionsFactory interface and use that to create the options:

import { Module } from '@nestjs/common';
import {
  PuppeteerModule,
  PuppeteerOptionsFactory,
  PuppeteerModuleOptions,
} from 'nest-puppeteer';

@Injectable()
export class PuppeteerConfigService implements PuppeteerOptionsFactory {
  private readonly launchOptions = { pipe: true };
  private readonly dbName = 'BestAppEver';

  createMongoOptions(): PuppeteerModuleOptions {
    return {
      launchOptions: this.launchOptions,
      instanceName: this.instanceName,
    };
  }
}

@Module({
  imports: [
    PuppeteerModule.forRootAsync({
      useClass: PuppeteerConfigService,
    }),
  ],
})
export class CatsModule {}

Just be aware that the useClass option will instantiate your class inside the PuppeteerModule, which may not be what you want.

Use existing

If you wish to instead import your PuppeteerConfigService class from a different module, the useExisting option will allow you to do that.

import { Module } from '@nestjs/common'
import { PuppeteerModule } from 'nest-puppeteer'
import { ConfigModule, ConfigService } from '../config/config.service'

@Module({
    imports: [PuppeteerModule.forRootAsync({
        imports: [ConfigModule]
        useExisting: ConfigService
    })]
})
export class CatsModule {}

In this example, we're assuming that ConfigService implements the PuppeteerOptionsFactory interface and can be found in the ConfigModule.

Use module globally

When you want to use PuppeteerModule in other modules, you'll need to import it (as is standard with any Nest module). Alternatively, declare it as a global module by setting the options object's isGlobal property to true, as shown below. In that case, you will not need to import PuppeteerModule in other modules once it's been loaded in the root module (e.g., AppModule).

PuppeteerModule.forRoot({
  isGlobal: true,
});

Stay in touch

License

nest-puppeteer is MIT licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].