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

Improve engine events to expose internal workflow #3881

Merged
merged 2 commits into from
Jul 17, 2024
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
6 changes: 4 additions & 2 deletions packages/adblocker-electron-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ async function createWindow() {
console.log('whitelisted', request.tabId, request.url);
});

blocker.on('csp-injected', (request: Request) => {
console.log('csp', request.url);
blocker.on('csp-injected', (request: Request, csps: string) => {
console.log('csp', request.url, csps);
});

blocker.on('script-injected', (script: string, url: string) => {
Expand All @@ -65,6 +65,8 @@ async function createWindow() {
console.log('style', style.length, url);
});

blocker.on('filter-matched', console.log.bind(console, 'filter-matched'));

mainWindow.loadURL(getUrlToLoad());
mainWindow.webContents.openDevTools();

Expand Down
12 changes: 12 additions & 0 deletions packages/adblocker-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export class ElectronBlocker extends FiltersEngine {
getExtendedRules: true,
getRulesFromHostname: true,
getRulesFromDOM: false, // Only done on updates (see `onGetCosmeticFiltersUpdated`)

callerContext: {
frameId: event.frameId,
processId: event.processId,
},
});

if (active === false) {
Expand Down Expand Up @@ -232,6 +237,13 @@ export class ElectronBlocker extends FiltersEngine {

// This will be done every time we get information about DOM mutation
getRulesFromDOM: true,

callerContext: {
frameId: event.frameId,
processId: event.processId,

lifecycle: msg.lifecycle,
},
});

if (active === false) {
Expand Down
10 changes: 7 additions & 3 deletions packages/adblocker-playwright-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import * as pw from 'playwright';
console.log('whitelisted', request.url);
});

blocker.on('csp-injected', (request: Request) => {
console.log('csp', request.url);
blocker.on('csp-injected', (request: Request, csps: string) => {
console.log('csp', request.url, csps);
});

blocker.on('script-injected', (script: string, url: string) => {
Expand All @@ -41,7 +41,11 @@ import * as pw from 'playwright';
console.log('style', style.length, url);
});

await page.goto('https://www.mangareader.to/');
blocker.on('filter-matched', ({ filter, exception }, context) => {
console.log('filter-matched', filter, exception, context);
});

await page.goto('https://www.mangareader.net/');
await page.screenshot({ path: 'output.png' });
await blocker.disableBlockingInPage(page);
await browser.close();
Expand Down
10 changes: 7 additions & 3 deletions packages/adblocker-puppeteer-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as puppeteer from 'puppeteer';
import { promises as fs } from 'fs';

function getUrlToLoad(): string {
let url = 'https://www.mangareader.to/';
let url = 'https://www.mangareader.net/';
if (process.argv[process.argv.length - 1].endsWith('.js') === false) {
url = process.argv[process.argv.length - 1];
}
Expand Down Expand Up @@ -47,8 +47,8 @@ function getUrlToLoad(): string {
console.log('whitelisted', request.url);
});

blocker.on('csp-injected', (request: Request) => {
console.log('csp', request.url);
blocker.on('csp-injected', (request: Request, csps: string) => {
console.log('csp', request.url, csps);
});

blocker.on('script-injected', (script: string, url: string) => {
Expand All @@ -59,5 +59,9 @@ function getUrlToLoad(): string {
console.log('style', style.length, url);
});

blocker.on('filter-matched', ({ filter, exception }, context) => {
console.log('filter-matched', filter, exception, context);
});

await page.goto(getUrlToLoad());
})();
12 changes: 8 additions & 4 deletions packages/adblocker-webextension-example/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ WebExtensionBlocker.fromLists(fetch, fullLists, {
console.log('redirect', request.url, result);
});

blocker.on('csp-injected', (request: Request) => {
console.log('csp', request.url);
blocker.on('csp-injected', (request: Request, csps: string) => {
console.log('csp', request.url, csps);
});

blocker.on('script-injected', (script: string, url: string) => {
Expand All @@ -84,8 +84,12 @@ WebExtensionBlocker.fromLists(fetch, fullLists, {
console.log('style', url, style.length);
});

blocker.on('html-filtered', (htmlSelectors: HTMLSelector[]) => {
console.log('html selectors', htmlSelectors);
blocker.on('html-filtered', (htmlSelectors: HTMLSelector[], url: string) => {
console.log('html selectors', htmlSelectors, url);
});

blocker.on('filter-matched', ({ filter, exception }, context) => {
console.log('filter-matched', filter, exception, context);
});

console.log('Ready to roll!');
Expand Down
15 changes: 15 additions & 0 deletions packages/adblocker-webextension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ export class WebExtensionBlocker extends FiltersEngine {
getExtendedRules: false,
getRulesFromDOM: false,
getRulesFromHostname: true,

callerContext: {
tabId: details.tabId,
frameId: details.frameId,
},
});
if (active === false) {
return;
Expand Down Expand Up @@ -460,6 +465,11 @@ export class WebExtensionBlocker extends FiltersEngine {
getExtendedRules: false,
getRulesFromDOM: false,
getRulesFromHostname: false,

callerContext: {
tabId: sender.tab?.id,
frameId: sender.frameId,
},
});

if (active === false) {
Expand Down Expand Up @@ -497,6 +507,11 @@ export class WebExtensionBlocker extends FiltersEngine {

// This will be done every time we get information about DOM mutation
getRulesFromDOM: msg.lifecycle === 'dom-update',

callerContext: {
tabId: sender.tab?.id,
frameId: sender.frameId,
},
});

if (active === false) {
Expand Down
Loading
Loading