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 3 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
18 changes: 15 additions & 3 deletions extension-manifest-v3/src/utils/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ 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', true],
['env_firefox', checkUserAgent('Firefox')],
['env_chromium', checkUserAgent('Chrome')],
['env_edge', checkUserAgent('Edg')],
['env_mobile', checkUserAgent('Mobile')],
]);

const engines = new Map();

function loadFromMemory(name) {
Expand Down Expand Up @@ -95,6 +106,7 @@ async function loadFromStorage(name) {

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

Expand Down Expand Up @@ -191,7 +203,7 @@ async function update(name) {

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

engine.updateEnv(ENV);
shareExceptions(name, engine);
// Save the new engine to memory and storage
saveToMemory(name, engine);
Expand Down Expand Up @@ -280,7 +292,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 @@ -326,7 +338,7 @@ async function loadFromDisk(name) {

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

engine.updateEnv(ENV);
shareExceptions(name, engine);
saveToMemory(name, engine);
saveToStorage(name);
Expand Down
78 changes: 39 additions & 39 deletions package-lock.json

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

47 changes: 47 additions & 0 deletions patches/ghostery-common+1.3.14.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
diff --git a/node_modules/ghostery-common/build/gbe/adblocker/manager.js b/node_modules/ghostery-common/build/gbe/adblocker/manager.js
index d490618..e7e92ba 100644
--- a/node_modules/ghostery-common/build/gbe/adblocker/manager.js
+++ b/node_modules/ghostery-common/build/gbe/adblocker/manager.js
@@ -23,6 +23,17 @@ var _regions = _interopRequireDefault(require("./regions"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

+var { isMobile, isFirefox, isChromium, isEdge } = require("../core/platform");
+
+const ENV = new Map([
+ ['ext_ghostery', true],
+ ['cap_html_filtering', true],
+ ['env_firefox', isFirefox],
+ ['env_chromium', isChromium],
+ ['env_edge', isEdge],
+ ['env_mobile', isMobile],
+]);
+
/*!
* Copyright (c) 2014-present Cliqz GmbH. All rights reserved.
*
@@ -353,7 +364,7 @@ class EngineManager {

const cumulativeDiff = _adblocker.default.mergeDiffs(diffs);

- let updated = this.engine.updateFromDiff(cumulativeDiff);
+ let updated = this.engine.updateFromDiff(cumulativeDiff, ENV);
timer.stop();

if (updated === true) {
@@ -407,6 +418,7 @@ class EngineManager {

try {
this.engine = _adblocker.default.WebExtensionBlocker.deserialize(serialized);
+ this.engine.updateEnv(ENV);
} catch (ex) {
// In case there is a mismatch between the version of the code
// and the serialization format of the engine on disk, we might
@@ -449,6 +461,7 @@ class EngineManager {

try {
this.engine = _adblocker.default.WebExtensionBlocker.deserialize(serialized);
+ this.engine.updateEnv(ENV);
} catch (ex) {
_logger.default.error('exception while loading remote engine', ex);

Loading