Skip to content

Commit 83ffe6f

Browse files
authored
fix(goto): continue intercepting after adblock teardown (#626)
1 parent 0813863 commit 83ffe6f

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

packages/browserless/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = ({ timeout: globalTimeout = 30000, ...launchOpts } = {}) => {
9797
const closePage = async (page, name) => {
9898
if (page && !page.isClosed()) {
9999
const duration = debug.duration('closePage')
100-
if (page.disableAdblock) await page.disableAdblock()
100+
if (page.disableAdblock) page.disableAdblock()
101101
const [browserProcess, browserContext] = await Promise.all([
102102
getBrowser(),
103103
getBrowserContext(),

packages/goto/src/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,10 @@ module.exports = ({ defaultDevice = 'Macbook Pro 13', timeout: globalTimeout, ..
235235
)
236236
}
237237

238-
// TODO: drop this when https://github.com/ghostery/adblocker/pull/5161 is merged
239-
let isRequestInterceptionEnabled = false
240-
page.isRequestInterceptionEnabled = () => isRequestInterceptionEnabled
241-
242238
const enableInterception =
243239
(onPageRequest || abortTypes.length > 0) &&
244240
run({
245-
fn: page.setRequestInterception(true).then(() => (isRequestInterceptionEnabled = true)),
241+
fn: page.setRequestInterception(true),
246242
debug: 'enableInterception'
247243
})
248244

@@ -273,15 +269,28 @@ module.exports = ({ defaultDevice = 'Macbook Pro 13', timeout: globalTimeout, ..
273269
}
274270

275271
if (adblock) {
276-
page.disableAdblock = async () => {
277-
await engine.disableBlockingInPage(page)
278-
isRequestInterceptionEnabled = false
272+
let adblockContext
273+
274+
page.disableAdblock = () => {
275+
// TODO: drop this when https://github.com/ghostery/adblocker/pull/5161 is merged
276+
277+
engine.contexts.delete(page)
278+
279+
if (adblockContext.blocker.config.loadNetworkFilters) {
280+
adblockContext.page.off('request', adblockContext.onRequest)
281+
}
282+
283+
if (adblockContext.blocker.config.loadCosmeticFilters) {
284+
adblockContext.page.off('frameattached', adblockContext.onFrameNavigated)
285+
adblockContext.page.off('domcontentloaded', adblockContext.onDomContentLoaded)
286+
}
287+
279288
debug.adblock('disabled')
280289
}
281290

282291
prePromises.push(
283292
run({
284-
fn: engine.enableBlockingInPage(page),
293+
fn: engine.enableBlockingInPage(page).then(context => (adblockContext = context)),
285294
timeout: actionTimeout,
286295
debug: 'adblock'
287296
})

0 commit comments

Comments
 (0)