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

Support $redirect in DNR #1750

Merged
merged 11 commits into from
Aug 8, 2024
12 changes: 12 additions & 0 deletions extension-manifest-v3/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ if (manifest.declarative_net_request?.rule_resources) {
}
}

// copy redirect rule resources
shelljs.mkdir('-p', resolve(options.outDir, 'rule_resources/redirects'));
shelljs.cp(
'-r',
resolve(options.srcDir, 'rule_resources/redirects', '*'),
resolve(options.outDir, 'rule_resources/redirects'),
);

// generate license file
execSync('npm run licenses', { stdio: 'inherit' });

Expand Down Expand Up @@ -218,6 +226,10 @@ manifest.content_scripts?.forEach(({ js = [], css = [] }) => {

// web-accessible resources
manifest.web_accessible_resources?.forEach((entry) => {
if (entry.matches === undefined) {
return;
}

const paths = [];

if (typeof entry === 'string') {
Expand Down
52 changes: 51 additions & 1 deletion extension-manifest-v3/scripts/download-engines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createHash } from 'crypto';
import { resolve } from 'path';
import shelljs from 'shelljs';

import { ENGINE_VERSION } from '@cliqz/adblocker';
import { ENGINE_VERSION, FiltersEngine } from '@cliqz/adblocker';

function checksum(content) {
return createHash('sha256').update(content).digest('hex');
Expand Down Expand Up @@ -140,3 +140,53 @@ for (const [name, target] of Object.entries(DNR)) {
writeFileSync(outputPath, dnr);
}
}

// Extract resources from ads engine
console.log('Extracting resources...');

shelljs.mkdir('-p', resolve(TARGET_PATH, 'redirects'));

const seenResource = new Set();
const allowedResourceExtensions = [
'html',
'js',
'css',
'mp4',
'mp3',
'xml',
'txt',
'json',
'empty',
];

FiltersEngine.deserialize(
readFileSync(`${TARGET_PATH}/engine-ads.dat`),
).resources.resources.forEach((value, key) => {
// refs https://github.com/gorhill/uBlock/tree/master/src/web_accessible_resources
if (
value.contentType === 'application/javascript' &&
(value.body.includes('scriptletGlobals') || // Drop scriptlets
key.includes('/')) // Drop resources within a directory
) {
return;
}

if (
!allowedResourceExtensions.includes(key.split('.').pop()) // Drop resources with an unknown file extension
) {
return;
}

if (seenResource.has(value.body)) {
return;
}

seenResource.add(value.body);

// Decode base64
if (value.contentType.endsWith(';base64')) {
value.body = Buffer.from(value.body, 'base64').toString('binary');
}

writeFileSync(resolve(TARGET_PATH, 'redirects', key), value.body);
});
5 changes: 5 additions & 0 deletions extension-manifest-v3/src/manifest.chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
"matches": [
"<all_urls>"
]
},
smalluban marked this conversation as resolved.
Show resolved Hide resolved
{
"resources": [
"rule_resources/redirects/*"
]
}
],
"content_security_policy": {}
Expand Down
Loading