diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-03-13 23:42:40 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-03-13 23:42:40 +0300 |
commit | 08b28d1f256f750e89b703249f717ba520ec7e5b (patch) | |
tree | a60f5df43424b80b20286c434cf8625d5d2cbc41 /browser.js | |
parent | 444902d07d618b70f82b300a6165f32d7b4346db (diff) |
save
Diffstat (limited to 'browser.js')
-rw-r--r-- | browser.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..81f7cc6 --- /dev/null +++ b/browser.js @@ -0,0 +1,55 @@ +const puppeteer = require("puppeteer-extra"); +const StealthPlugin = require('puppeteer-extra-plugin-stealth'); +const cookiesStorage = require("./cookies-storage"); +puppeteer.use(StealthPlugin()); + + +const options = { + headless: true, + args: [] +}; + +let browser = null +let page = null + + +module.exports = { + async launch() { + browser = await puppeteer.launch(options) + }, + + async getPage() { + if (page && page.isClosed()) { + page.removeAllListeners && page.removeAllListeners() + page = null + } + + if (!page) { + page = await browser.newPage() + page.on('framenavigated', async () => { + let cookies = await page.cookies(); + if (cookies) + await cookiesStorage.save(cookies) + }) + + page.setCookie(...(await cookiesStorage.get())) + } + + return page + }, + get() { + return browser + }, + setProxy(proxy) { + options.args.push(`--proxy-server=${proxy}`) + }, + disableSandbox() { + options.args.push( + '--no-sandbox', + '--disable-setuid-sandbox' + ) + }, + setHeadful() { + options.headless = false; + } +}
\ No newline at end of file |