Skip to content

Commit

Permalink
chore(adblock): setup priority (#321)
Browse files Browse the repository at this point in the history
* chore(adblock): setup priority

* Update index.js

* chore(adblock): setup priority out of async block

* chore: add continue debug log

* chore: register aborTypes before adblock
  • Loading branch information
Kikobeats committed Oct 19, 2021
1 parent 58b992a commit 098b73e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/goto/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const engine = PuppeteerBlocker.deserialize(

engine.on('request-blocked', ({ url }) => debugAdblock('block', url))
engine.on('request-redirected', ({ url }) => debugAdblock('redirect', url))
engine.setRequestInterceptionPriority(1)

const isEmpty = val => val == null || !(Object.keys(val) || val).length

Expand Down Expand Up @@ -158,6 +159,19 @@ module.exports = ({

const prePromises = []

if (abortTypes.length > 0) {
await page.setRequestInterception(true)
page.on('request', req => {
const resourceType = req.resourceType()
if (!abortTypes.includes(resourceType)) {
debug('continue', { url: req.url(), resourceType })
return req.continue(req.continueRequestOverrides(), 2)
}
debug('abort', { url: req.url(), resourceType })
return req.abort('blockedbyclient', 2)
})
}

if (adblock) {
prePromises.push(
run({
Expand Down Expand Up @@ -271,16 +285,6 @@ module.exports = ({

await Promise.all(prePromises.concat(applyEvasions))

if (abortTypes.length > 0) {
await page.setRequestInterception(true)
page.on('request', req => {
const resourceType = req.resourceType()
if (!abortTypes.includes(resourceType)) return req.continue()
debug('abort', { url: req.url(), resourceType })
return req.abort('blockedbyclient')
})
}

const { value } = await run({
fn: html ? page.setContent(html, args) : page.goto(url, args),
timeout,
Expand Down

0 comments on commit 098b73e

Please sign in to comment.