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

Adblocker preprocessor support #1519

Merged
merged 9 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions extension-manifest-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
},
"homepage": "https://github.com/ghostery/ghostery-extension#readme",
"dependencies": {
"@cliqz/adblocker": "^1.26.12",
"@cliqz/adblocker-webextension": "^1.26.12",
"@cliqz/adblocker": "^1.27.0",
"@cliqz/adblocker-webextension": "^1.27.0",
"@cliqz/url-parser": "^1.1.5",
"@duckduckgo/autoconsent": "^10.3.2",
"@ghostery/libs": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions extension-manifest-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"web-ext": "^7.11.0"
},
"dependencies": {
"@cliqz/adblocker": "^1.26.12",
"@cliqz/adblocker-webextension-cosmetics": "^1.26.12",
"@cliqz/adblocker": "^1.27.0",
"@cliqz/adblocker-webextension-cosmetics": "^1.27.0",
"@duckduckgo/autoconsent": "^10.3.2",
"@ghostery/libs": "^1.0.0",
"@ghostery/trackers-preview": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function parseFilters(text = '') {
}

async function submitFilters(host) {
const { networkFilters, cosmeticFilters } = host.filters;
const { networkFilters } = host.filters;

// Update DNR
if (__PLATFORM__ !== 'firefox') {
Expand Down Expand Up @@ -75,7 +75,7 @@ async function submitFilters(host) {
// Update engine
await chrome.runtime.sendMessage({
action: 'customFilters:engine',
filters: [...networkFilters, ...cosmeticFilters].join('\n'),
filters: host.input.text,
});

// Save input
Expand Down
28 changes: 22 additions & 6 deletions extension-manifest-v3/src/utils/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ import { registerDatabase } from '/utils/indexeddb.js';

export const CUSTOM_ENGINE = 'custom-filters';

const checkUserAgent = (pattern) => navigator.userAgent.indexOf(pattern) !== -1;

const ENV = new Map([
['ext_ghostery', true],
['cap_html_filtering', checkUserAgent('Firefox')],
['env_firefox', checkUserAgent('Firefox')],
['env_chromium', checkUserAgent('Chrome')],
['env_edge', checkUserAgent('Edg')],
['env_mobile', checkUserAgent('Mobile')],
]);

const deserializeEngine = (engineBytes) => {
const engine = FiltersEngine.deserialize(engineBytes);
engine.updateEnv(ENV);
return engine;
};

const engines = new Map();

function loadFromMemory(name) {
Expand Down Expand Up @@ -94,7 +111,7 @@ async function loadFromStorage(name) {
const engineBytes = await table.get(name);

if (engineBytes) {
const engine = FiltersEngine.deserialize(engineBytes);
const engine = deserializeEngine(engineBytes);
shareExceptions(name, engine);
saveToMemory(name, engine);

Expand Down Expand Up @@ -190,8 +207,7 @@ async function update(name) {
.then((res) => res.arrayBuffer());

const engineBytes = new Uint8Array(arrayBuffer);
engine = FiltersEngine.deserialize(engineBytes);

engine = deserializeEngine(engineBytes);
shareExceptions(name, engine);
// Save the new engine to memory and storage
saveToMemory(name, engine);
Expand Down Expand Up @@ -280,7 +296,7 @@ async function update(name) {
// `engine.update` method will return `true` if anything was
// updated and `false` otherwise.
const cumulativeDiff = mergeDiffs(diffs);
let updated = engine.updateFromDiff(cumulativeDiff);
let updated = engine.updateFromDiff(cumulativeDiff, ENV);

// Last but not least, check if resources.txt should be updated. This can be
// done independently of filters as the data is stored in a separate object.
Expand Down Expand Up @@ -325,8 +341,7 @@ async function loadFromDisk(name) {
);

const engineBytes = new Uint8Array(await response.arrayBuffer());
const engine = FiltersEngine.deserialize(engineBytes);

const engine = deserializeEngine(engineBytes);
shareExceptions(name, engine);
saveToMemory(name, engine);
saveToStorage(name);
Expand Down Expand Up @@ -378,6 +393,7 @@ export async function createCustomEngine(filters = '') {
enableHtmlFiltering: true,
});
const engine = FiltersEngine.parse(filters, config);
engine.updateEnv(ENV);

saveToMemory(CUSTOM_ENGINE, engine);
saveToStorage(CUSTOM_ENGINE);
Expand Down
88 changes: 44 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading