summaryrefslogtreecommitdiff
path: root/browser.js
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-03-15 03:56:59 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-03-15 03:56:59 +0300
commite075f11bdb1b194062d153cad181a6b04ad8fcfc (patch)
treefdadba7ab1c19c70cff5e247e173ad0bf939b8a6 /browser.js
parent4f867d7cbcb7cd23b9cdfab3422bad9dc4a92415 (diff)
much fixes so improvementsHEADmaster
Diffstat (limited to 'browser.js')
-rw-r--r--browser.js93
1 files changed, 0 insertions, 93 deletions
diff --git a/browser.js b/browser.js
deleted file mode 100644
index d1f5c50..0000000
--- a/browser.js
+++ /dev/null
@@ -1,93 +0,0 @@
-const puppeteer = require("puppeteer-extra");
-const StealthPlugin = require('puppeteer-extra-plugin-stealth');
-const cookiesStorage = require("./cookies-storage");
-puppeteer.use(StealthPlugin());
-
-
-const chromeOptions = {
- headless: true,
- args: []
-};
-
-let browser = null
-let cdpClient = null
-
-class PageWrapper {
- constructor() {
- this.intrNeededCallback = null
- this.intrCallback = null
-
- this.page = null
- }
-
- async getPage(interceptionNeededCallback, interceptCallback) {
- this.intrCallback = interceptCallback
- this.intrNeededCallback = interceptionNeededCallback
-
- if (this.page !== null && this.page.isClosed()) {
- this.page.removeAllListeners && this.page.removeAllListeners()
- this.page = null
- }
-
- if (this.page !== null)
- return this.page
-
- this.page = await browser.newPage()
- this.page.on('domcontentloaded', async () => {
- try {
- let cookies = await this.page.cookies();
- if (cookies)
- await cookiesStorage.save(cookies)
- } catch (e) {
- console.warn(e)
- }
- })
-
- await this.page.setCookie(...(await cookiesStorage.get()))
-
- cdpClient = await this.page.target().createCDPSession();
- await cdpClient.send('Network.setRequestInterception', {
- patterns: [{
- urlPattern: '*',
- resourceType: 'Document',
- interceptionStage: 'HeadersReceived'
- }],
- })
- await cdpClient.on('Network.requestIntercepted', async e => {
- let obj = { interceptionId: e.interceptionId }
- if (this.intrNeededCallback && this.intrNeededCallback(e) === true) {
- let ret = await cdpClient.send('Network.getResponseBodyForInterception', {
- interceptionId: e.interceptionId
- })
- this.intrCallback(ret, e.responseHeaders)
- obj['errorReason'] = 'BlockedByClient'
- }
- await cdpClient.send('Network.continueInterceptedRequest', obj)
- })
-
- return this.page
- }
-}
-
-let singlePageWrapper = new PageWrapper()
-
-module.exports = {
- async launch(options) {
- if (options.proxy)
- chromeOptions.args.push(`--proxy-server=${options.proxy}`)
-
- if (options.noSandbox)
- chromeOptions.args.push(
- '--no-sandbox',
- '--disable-setuid-sandbox'
- )
-
- if (options.headful)
- chromeOptions.headless = false
-
- browser = await puppeteer.launch(chromeOptions)
- },
-
- singlePageWrapper,
- PageWrapper,
-} \ No newline at end of file