Skip to content

Commit

Permalink
feat: emit cosmetic rules on match
Browse files Browse the repository at this point in the history
  • Loading branch information
seia-soto committed Jul 17, 2024
1 parent 5a760a8 commit f0a6bd9
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 148 deletions.
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
8 changes: 5 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,9 @@ import * as pw from 'playwright';
console.log('style', style.length, url);
});

await page.goto('https://www.mangareader.to/');
blocker.on('filter-matched', console.log.bind(console, 'filter-matched'));

await page.goto('https://www.mangareader.net/');
await page.screenshot({ path: 'output.png' });
await blocker.disableBlockingInPage(page);
await browser.close();
Expand Down
8 changes: 5 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,7 @@ function getUrlToLoad(): string {
console.log('style', style.length, url);
});

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

await page.goto(getUrlToLoad());
})();
10 changes: 6 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,9 +84,11 @@ 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', console.log.bind(console, 'filter-matched'));

console.log('Ready to roll!');
});
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

0 comments on commit f0a6bd9

Please sign in to comment.