import puppeteer from 'puppeteer' import { executablePath } from 'puppeteer'; class PuppeteerSg { constructor() { if (!PuppeteerSg.instance) { PuppeteerSg.instance = this; process.on('exit', () => { this.close(); }); } return PuppeteerSg.instance; } /** * Launch a browser */ async launch() { console.log("EXECUTE" + executablePath()) this.browser = await puppeteer.launch({ headless: "new", defaultViewport: null, args: ["--no-sandbox"], executablePath: executablePath() }); } /** * New a page * @param {string} url * @returns */ async getPage(url) { if (!this.browser) { await this.launch() } let page = await this.browser.newPage() await page.goto(url, { waitUntil: "load", }) return page } /** * Close the browser */ async close() { if (this.browser) { await this.browser.close(); this.browser = null; } } } export const puppeteerSg = new PuppeteerSg()