diff options
Diffstat (limited to 'browser.js')
-rw-r--r-- | browser.js | 37 |
1 files changed, 31 insertions, 6 deletions
@@ -11,19 +11,24 @@ const options = { let browser = null let page = null - +let cdpClient = null +let interceptCallback = null +let interceptionNeededCallback = null module.exports = { async launch() { browser = await puppeteer.launch(options) }, - async getPage() { + async getPage(_interceptionNeededCallback, _interceptCallback) { if (page && page.isClosed()) { page.removeAllListeners && page.removeAllListeners() page = null } + interceptionNeededCallback = _interceptionNeededCallback + interceptCallback = _interceptCallback + if (!page) { page = await browser.newPage() page.on('framenavigated', async () => { @@ -33,23 +38,43 @@ module.exports = { }) page.setCookie(...(await cookiesStorage.get())) + + cdpClient = await 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 (interceptionNeededCallback && interceptionNeededCallback(e) === true) { + let ret = await cdpClient.send('Network.getResponseBodyForInterception', { + interceptionId: e.interceptionId + }) + interceptCallback(ret, e.responseHeaders) + obj['errorReason'] = 'BlockedByClient' + } + await cdpClient.send('Network.continueInterceptedRequest', obj) + }) } 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; + options.headless = false } }
\ No newline at end of file |