Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only register listeners when network/cosmetics filtering is enabled #288

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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