Skip to content

Commit

Permalink
enhancement: only register listeners when network/cosmetics filtering…
Browse files Browse the repository at this point in the history
… is enabled
  • Loading branch information
remusao committed Aug 20, 2019
1 parent 8111a30 commit ad0b143
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

*not released*

* only register listeners when network/cosmetics filtering is enabled [#288](https://github.com/cliqz-oss/adblocker/pull/288)

## 0.13.2

*2019-08-17*
Expand Down
19 changes: 11 additions & 8 deletions packages/adblocker-electron/adblocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ export function fromElectronDetails({
*/
export class ElectronBlocker extends FiltersEngine {
public enableBlockingInSession(ses: Electron.Session) {
ses.webRequest.onHeadersReceived({ urls: ['<all_urls>'] }, this.onHeadersReceived);
ses.webRequest.onBeforeRequest({ urls: ['<all_urls>'] }, this.onBeforeRequest);

ipcMain.on('get-cosmetic-filters', this.onGetCosmeticFilters);
ses.setPreloads([join(__dirname, './preload.js')]);
if (this.config.loadNetworkFilters === true) {
ses.webRequest.onHeadersReceived({ urls: ['<all_urls>'] }, this.onHeadersReceived);
ses.webRequest.onBeforeRequest({ urls: ['<all_urls>'] }, this.onBeforeRequest);
}

ipcMain.on('is-mutation-observer-enabled', (event: Electron.IpcMainEvent) => {
event.returnValue = this.config.enableMutationObserver;
});
if (this.config.loadCosmeticFilters === true) {
ipcMain.on('get-cosmetic-filters', this.onGetCosmeticFilters);
ses.setPreloads([join(__dirname, './preload.js')]);
ipcMain.on('is-mutation-observer-enabled', (event: Electron.IpcMainEvent) => {
event.returnValue = this.config.enableMutationObserver;
});
}
}

private onGetCosmeticFilters = (
Expand Down
26 changes: 15 additions & 11 deletions packages/adblocker-puppeteer/adblocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ export class PuppeteerBlocker extends FiltersEngine {
// currently no way to modify responses in puppeteer. This feature could
// easily be added if puppeteer implements the required capability.

// Register callback for network requets filtering
page.on('request', this.onRequest);

// Register callback to cosmetics injection (CSS + scriptlets)
page.on('framenavigated', async (frame) => {
try {
await this.onFrame(frame);
} catch (ex) {
// Ignore
}
});
if (this.config.loadNetworkFilters === true) {
// Register callback for network requets filtering
page.on('request', this.onRequest);
}

if (this.config.loadCosmeticFilters === true) {
// Register callback to cosmetics injection (CSS + scriptlets)
page.on('framenavigated', async (frame) => {
try {
await this.onFrame(frame);
} catch (ex) {
// Ignore
}
});
}
});
}

Expand Down
22 changes: 12 additions & 10 deletions packages/adblocker-webextension/adblocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ export function updateResponseHeadersWithCSP(
*/
export class WebExtensionBlocker extends FiltersEngine {
public enableBlockingInBrowser() {
chrome.webRequest.onBeforeRequest.addListener(
this.onBeforeRequest,
{ urls: ['<all_urls>'] },
['blocking'],
);
if (this.config.loadNetworkFilters === true) {
chrome.webRequest.onBeforeRequest.addListener(
this.onBeforeRequest,
{ urls: ['<all_urls>'] },
['blocking'],
);

chrome.webRequest.onHeadersReceived.addListener(
this.onHeadersReceived,
{ urls: ['<all_urls>'], types: ['main_frame'] },
['blocking', 'responseHeaders'],
);
chrome.webRequest.onHeadersReceived.addListener(
this.onHeadersReceived,
{ urls: ['<all_urls>'], types: ['main_frame'] },
['blocking', 'responseHeaders'],
);
}

// Start listening to messages coming from the content-script
chrome.runtime.onMessage.addListener(this.onRuntimeMessage);
Expand Down

0 comments on commit ad0b143

Please sign in to comment.