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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@syncmarket/browser-facade

v1.0.2

Published

A robust library providing an enhanced facade for Puppeteer and Playwright, simplifying browser automation tasks with a unified API and additional utilities.

Downloads

5

Readme

@syncmarket/browser-facade

Importar

import { App, PageProcessor, Runner } from '@syncmarket/browser-facade'

Uso

Primero vamos a crear una clase que será la encargada de realizar el proceso de scrapeo y las operaciones necesarias que se pueda requerir tales como:

  • Persistencia/Lectura en Base de Datos
  • Consumo de información de alguna Api
  • Lectura/Escritura en sistemas de archivos.

Esta clase debe extender de Runner que es la que contiene los métodos necesarios para el proceso de scrapping.

class ScrapperExample extends Runner<ExampleProcessor> {
  async beforeRun(): Promise<void> {
    this.logger.info('Before run');
  }

  async afterRun(): Promise<void> {
    this.logger.info('After run');
  }

  async run(): Promise<void> {
    try {
      const pageProcessor = await this.process('https://sync-market.netlify.app/')
      const data = await pageProcessor.getData()

      console.log(data)

    } catch (error: any) {
      this.logger.error(error.message);
    }
  }

  async makeResponseProcessor(data: unknown): Promise<ExampleProcessor> {
    return new ExampleProcessor(data);
  }
}

Para la manipulación del dom, vamos a crear otra clase que será la encargada de procesar los datos, transformarlos, limpiarlo etc.

Esta clase debe heredar de PageProcessor y recibe como parametro en su constructor la pagina, aqui es donde vamos a definir todas las acciones que se pueden realizar dentro de la pagina. Por ejemplo:

  • Obtener datos
  • Rellenar un formulario
  • Click sobre algun boton
  • Etc.
class ExampleProcessor extends PageProcessor {
  async getData(): Promise<any> {
    const data = await this.page.getTextContent(selectors.title)
    const [title, description] = data.split('\n')

    return { title, description: description.trim() }
  }

  async fillForm(data: any): Promise<any> {
    // do stuff
  }
}

Por ùltimo creamos la instancia de la app, esté será la encargada de iniciarlizar todo. Este recibe algunas configuraciones obligatorias

executablePath: La ruta completa del navegador chrome

const app = new App({
  client: {
    executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  }
})

const scrapper = new ScrapperExample(app)

await scrapper.start()

Código completo.

import { App, Runner, PageProcessor } from '@syncmarket/browser-facade'

const selectors = {
  title: 'h1'
}

class ExampleProcessor extends PageProcessor {
  async getData(): Promise<any> {
    const data = await this.page.getTextContent(selectors.title)
    const [title, description] = data.split('\n')

    return { title, description: description.trim() }
  }
}

class ScrapperExample extends Runner<ExampleProcessor> {
  async beforeRun(): Promise<void> {
    this.logger.info('Before run');
  }

  async afterRun(): Promise<void> {
    this.logger.info('After run');
  }

  async run(): Promise<void> {
    try {
      const pageProcessor = await this.process('https://sync-market.netlify.app/')
      const data = await pageProcessor.getData()

      console.log(data)

    } catch (error: any) {
      this.logger.error(error.message);
    }
  }

  async makeResponseProcessor(data: unknown): Promise<ExampleProcessor> {
    return new ExampleProcessor(data);
  }
}

const app = new App({
  client: {
    executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  }
})

const scrapper = new ScrapperExample(app)

await scrapper.start()

Probando la app.

bun index.ts

8:24:16 PM [INFO]: Before run
{
  title: "SyncMarket",
  description: "Automatiza tus Publicaciones en Facebook Marketplace con Nuestro Bot",
}
8:24:25 PM [INFO]: After run