diff --git a/packages/adblocker-electron-example/src/index.ts b/packages/adblocker-electron-example/src/index.ts index 4eabf94ae4..25a3bfb958 100644 --- a/packages/adblocker-electron-example/src/index.ts +++ b/packages/adblocker-electron-example/src/index.ts @@ -39,7 +39,15 @@ async function createWindow() { }, ); - blocker.enableBlockingInSession(mainWindow.webContents.session); + blocker.enableBlockingInSession(mainWindow.webContents.session, false); + mainWindow.webContents.session.webRequest.onHeadersReceived( + { urls: [''] }, + blocker.onHeadersReceived, + ); + mainWindow.webContents.session.webRequest.onBeforeRequest( + { urls: [''] }, + blocker.onBeforeRequest, + ); blocker.on('request-blocked', (request: Request) => { console.log('blocked', request.tabId, request.url); diff --git a/packages/adblocker-electron/src/index.ts b/packages/adblocker-electron/src/index.ts index d79a732306..fabb08f6e7 100644 --- a/packages/adblocker-electron/src/index.ts +++ b/packages/adblocker-electron/src/index.ts @@ -73,7 +73,9 @@ export class BlockingContext { constructor( private readonly session: Electron.Session, private readonly blocker: ElectronBlocker, + private readonly registerWebRequestHandlers: boolean, ) { + this.registerWebRequestHandlers = registerWebRequestHandlers; this.onBeforeRequest = (details, callback) => blocker.onBeforeRequest(details, callback); this.onInjectCosmeticFilters = (event, url, msg) => @@ -92,22 +94,14 @@ export class BlockingContext { ); } - if (this.blocker.config.loadNetworkFilters === true) { + if (this.blocker.config.loadNetworkFilters === true && this.registerWebRequestHandlers) { this.session.webRequest.onHeadersReceived({ urls: [''] }, this.onHeadersReceived); this.session.webRequest.onBeforeRequest({ urls: [''] }, this.onBeforeRequest); } } public disable(): void { - if (this.blocker.config.loadNetworkFilters === true) { - // NOTE - there is currently no support in Electron for multiple - // webRequest listeners registered for the same event. This means that - // adblocker's listeners can be overriden by other ones in the same - // application (or that the adblocker can override another listener - // registered previously). Because of this, the only way to disable the - // adblocker is to remove all listeners for the events we are interested - // in. In the future, we should consider implementing a webRequest - // pipeline allowing to register multiple listeners for the same event. + if (this.blocker.config.loadNetworkFilters === true && this.registerWebRequestHandlers) { this.session.webRequest.onHeadersReceived(null); this.session.webRequest.onBeforeRequest(null); } @@ -131,14 +125,17 @@ export class ElectronBlocker extends FiltersEngine { // Helpers to enable and disable blocking for 'browser' // ----------------------------------------------------------------------- // - public enableBlockingInSession(session: Electron.Session): BlockingContext { + public enableBlockingInSession( + session: Electron.Session, + registerWebRequestHandlers: boolean = true, + ): BlockingContext { let context: undefined | BlockingContext = this.contexts.get(session); if (context !== undefined) { return context; } // Create new blocking context for `session` - context = new BlockingContext(session, this); + context = new BlockingContext(session, this, registerWebRequestHandlers); this.contexts.set(session, context); context.enable();