| | 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; |
| | } |
| |
|
| | |
| | |
| | |
| | async launch() { |
| | console.log("EXECUTE" + executablePath()) |
| | this.browser = await puppeteer.launch({ |
| | headless: "new", |
| | defaultViewport: null, |
| | args: ["--no-sandbox"], |
| | executablePath: executablePath() |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | async getPage(url) { |
| | if (!this.browser) { |
| | await this.launch() |
| | } |
| | let page = await this.browser.newPage() |
| | await page.goto(url, { |
| | waitUntil: "load", |
| | }) |
| | return page |
| | } |
| |
|
| | |
| | |
| | |
| | async close() { |
| | if (this.browser) { |
| | await this.browser.close(); |
| | this.browser = null; |
| | } |
| | } |
| | } |
| |
|
| | export const puppeteerSg = new PuppeteerSg() |
| |
|