Skip to content

Commit

Permalink
Safari: use single DNR list (#1367)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominik Lubański <dominik.lubanski@gmail.com>
  • Loading branch information
chrmod and smalluban authored Nov 3, 2023
1 parent 3c95d70 commit c01a676
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion extension-manifest-v3/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ if (manifest.declarative_net_request?.rule_resources) {

// https://github.com/WebKit/WebKit/blob/c85962a5c0e929991e5963811da957b75d1501db/Source/WebCore/contentextensions/ContentExtensionCompiler.cpp#L199
if (rulesCount > 75000) {
console.warn(
throw new Error(
`Warning: The number of rules exceeds the limit of 75k rules.`,
);
}
Expand Down
25 changes: 22 additions & 3 deletions extension-manifest-v3/scripts/download-engines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import { ENGINE_VERSION } from '@cliqz/adblocker';
import { getCompatRule, setupStream } from './utils.js';

const ENGINES = {
'dnr-ads': 'ads',
'dnr-tracking': 'tracking',
'dnr-annoyances': 'annoyances',
'dnr-cosmetics-ads': 'ads-cosmetics',
'dnr-cosmetics-tracking': 'tracking-cosmetics',
'dnr-cosmetics-annoyances': 'annoyances-cosmetics',
Expand Down Expand Up @@ -72,6 +69,28 @@ for (const [name, target] of Object.entries(ENGINES)) {
});

writeFileSync(`${TARGET_PATH}/engine-${target}.dat`, new Uint8Array(rules));
}

const DNR = {
'dnr-ads-2': 'ads',
'dnr-tracking-2': 'tracking',
'dnr-annoyances-2': 'annoyances',
};

for (const [name, target] of Object.entries(DNR)) {
console.log(`Downloading "${name}"...`);

const list = await fetch(
`https://cdn.ghostery.com/adblocker/configs/${name}/allowed-lists.json`,
).then((res) => {
if (!res.ok) {
throw new Error(
`Failed to download allowed list for "${name}": ${res.status}: ${res.statusText}`,
);
}

return res.json();
});

/* DNR rules */

Expand Down
9 changes: 9 additions & 0 deletions extension-manifest-v3/scripts/download-engines/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export function getCompatRule(rule, debug = false) {
log('Fixing regexp');
}

if (newRule.priority > 10000) {
log('Skipping complex rule');
return null;
}

return newRule;
}

Expand All @@ -107,6 +112,10 @@ export function setupStream(path) {
},
close: () => {
output.write(']');
console.log(`Generated ${currentId} rules`);
if (currentId > 75000) {
throw new Error('Too many DNR rules');
}
output.close();
},
};
Expand Down
6 changes: 6 additions & 0 deletions extension-manifest-v3/src/background/dnr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import { observe, ENGINES } from '/store/options.js';

if (__PLATFORM__ !== 'firefox') {
const DNR_RESOURCES = chrome.runtime
.getManifest()
.declarative_net_request.rule_resources.map(({ id }) => id);

// Ensure that DNR rulesets are equal to those from options.
// eg. when web extension updates, the rulesets are reset
// to the value from the manifest.
Expand All @@ -23,6 +27,8 @@ if (__PLATFORM__ !== 'firefox') {
const disableRulesetIds = [];

ENGINES.forEach(({ name, key }) => {
if (!DNR_RESOURCES.includes(name)) return;

const enabled = options.terms && options[key];
if (enabledRulesetIds.includes(name) !== enabled) {
(enabled ? enableRulesetIds : disableRulesetIds).push(name);
Expand Down
10 changes: 0 additions & 10 deletions extension-manifest-v3/src/manifest.safari.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@
"id": "ads",
"enabled": false,
"path": "rule_resources/dnr-safari-ads.json"
},
{
"id": "tracking",
"enabled": false,
"path": "rule_resources/dnr-safari-tracking.json"
},
{
"id": "annoyances",
"enabled": false,
"path": "rule_resources/dnr-safari-annoyances.json"
}
]
},
Expand Down

0 comments on commit c01a676

Please sign in to comment.