Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
Signed-off-by: nullptropy <nullptropy@tutanota.com>
  • Loading branch information
nullptropy committed Oct 17, 2024
1 parent 0fab6e0 commit 65c0cde
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.
40 changes: 9 additions & 31 deletions packages/adblocker-electron-preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@

import { ipcRenderer } from 'electron';

import {
DOMMonitor,
IBackgroundCallback,
} from '@ghostery/adblocker-content';
import { DOMMonitor, IBackgroundCallback } from '@ghostery/adblocker-content';

interface ICosmeticFiltersResponse = {
active: boolean;
error?: string;
};

function injectCosmeticFilters(
data?: Omit<IBackgroundCallback, 'lifecycle'>,
): Promise<CosmeticFiltersResponse> {
function injectCosmeticFilters(data?: Omit<IBackgroundCallback, 'lifecycle'>): Promise<void> {
return ipcRenderer.invoke(
'@ghostery/adblocker/inject-cosmetic-filters',
window.location.href,
Expand All @@ -29,12 +19,7 @@ function injectCosmeticFilters(
}

if (window === window.top && window.location.href.startsWith('devtools://') === false) {
(async () => {
const enableMutationObserver = await ipcRenderer.invoke(
'@ghostery/adblocker/is-mutation-observer-enabled',
);

let ACTIVE: boolean = true;
(() => {
let DOM_MONITOR: DOMMonitor | null = null;

const unload = () => {
Expand All @@ -44,10 +29,7 @@ if (window === window.top && window.location.href.startsWith('devtools://') ===
}
};

const response = await injectCosmeticFilters();
if (response.error)
throw new Error(`error injecting initial cosmetic filters: ${response.error}`);
if (!(ACTIVE = response.active)) return;
injectCosmeticFilters();

// On DOMContentLoaded, start monitoring the DOM. This means that we will
// first check which ids and classes exist in the DOM as a one-off operation;
Expand All @@ -59,21 +41,17 @@ if (window === window.top && window.location.href.startsWith('devtools://') ===
() => {
DOM_MONITOR = new DOMMonitor((update) => {
if (update.type === 'features') {
injectCosmeticFilters().then((response) => {
if (response.error) {
console.error(`error injecting updated cosmetic filters: ${response.error}`);
}
});
injectCosmeticFilters();
}
});

DOM_MONITOR.queryAll(window);

// Start observing mutations to detect new ids and classes which would
// need to be hidden.
if (ACTIVE && enableMutationObserver) {
DOM_MONITOR.start(window);
}
ipcRenderer
.invoke('@ghostery/adblocker/is-mutation-observer-enabled')
.then((enableMutationObserver) => enableMutationObserver && DOM_MONITOR?.start(window));
},
{ once: true, passive: true },
);
Expand All @@ -82,4 +60,4 @@ if (window === window.top && window.location.href.startsWith('devtools://') ===
})();
}

export type { CosmeticFiltersResponse, IBackgroundCallback };
export type { IBackgroundCallback };
31 changes: 10 additions & 21 deletions packages/adblocker-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import * as electron from 'electron';
import { parse } from 'tldts-experimental';

import { ElectronRequestType, FiltersEngine, Request } from '@ghostery/adblocker';
import type {
CosmeticFiltersResponse,
IBackgroundCallback,
} from '@ghostery/adblocker-electron-preload';
import type { IBackgroundCallback } from '@ghostery/adblocker-electron-preload';

import { PRELOAD_PATH } from './preload_path.js';

Expand Down Expand Up @@ -62,7 +59,7 @@ export class BlockingContext {
event: Electron.IpcMainInvokeEvent,
url: string,
msg?: IBackgroundCallback,
) => Promise<CosmeticFiltersResponse>;
) => Promise<void>;

private readonly onHeadersReceived: (
details: Electron.OnHeadersReceivedListenerDetails,
Expand Down Expand Up @@ -176,7 +173,7 @@ export class ElectronBlocker extends FiltersEngine {
event: Electron.IpcMainInvokeEvent,
url: string,
msg?: IBackgroundCallback,
): Promise<CosmeticFiltersResponse> => {
): Promise<void> => {
const parsed = parse(url);
const hostname = parsed.hostname || '';
const domain = parsed.domain || '';
Expand Down Expand Up @@ -211,28 +208,20 @@ export class ElectronBlocker extends FiltersEngine {
});

if (active === false) {
return { active: false };
return;
}

if (styles.length > 0) {
event.sender.insertCSS(styles, { cssOrigin: 'user' });
}

const scriptResults = await Promise.allSettled(
scripts.map((script) => event.sender.executeJavaScript(script, true)),
);
const scriptErrors = scriptResults
.filter((result) => result.status === 'rejected')
.map((result) => result.reason);

if (scriptErrors.length > 0) {
return {
active: false,
error: `Some scripts failed to execute: ${scriptErrors.join(', ')}`,
};
for (const script of scripts) {
try {
event.sender.executeJavaScript(script, true);
} catch (e) {
console.error('@ghostery/adblocker scriptlet crashed', e);
}
}

return { active: true };
};

public onHeadersReceived = (
Expand Down

0 comments on commit 65c0cde

Please sign in to comment.