1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 15:33:38 +01:00

Dynamically load DNR conversion module on-demand

DNR conversion module is a dev tool, no point loading it by
default. This was done this way because in a distant past uBO
had to be compatible with browsers not supporting dynamically
loaded modules.

Currently all supported browser versions support dynamically
loaded modules.
This commit is contained in:
Raymond Hill 2024-09-14 12:13:47 -04:00
parent c265e849e0
commit 4b285c0593
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 105 additions and 91 deletions

View File

@ -21,6 +21,7 @@
/* global CodeMirror, uBlockDashboard */ /* global CodeMirror, uBlockDashboard */
import * as s14e from './s14e-serializer.js';
import { dom, qs$ } from './dom.js'; import { dom, qs$ } from './dom.js';
/******************************************************************************/ /******************************************************************************/
@ -79,6 +80,100 @@ function log(text) {
cmEditor.replaceRange(text.trim() + '\n\n', { line: 0, ch: 0 }); cmEditor.replaceRange(text.trim() + '\n\n', { line: 0, ch: 0 });
} }
/******************************************************************************/
function toDNRText(raw) {
const result = s14e.deserialize(raw);
if ( typeof result === 'string' ) { return result; }
const { network } = result;
const replacer = (k, v) => {
if ( k.startsWith('__') ) { return; }
if ( Array.isArray(v) ) {
return v.sort();
}
if ( v instanceof Object ) {
const sorted = {};
for ( const kk of Object.keys(v).sort() ) {
sorted[kk] = v[kk];
}
return sorted;
}
return v;
};
const isUnsupported = rule =>
rule._error !== undefined;
const isRegex = rule =>
rule.condition !== undefined &&
rule.condition.regexFilter !== undefined;
const isRedirect = rule =>
rule.action !== undefined &&
rule.action.type === 'redirect' &&
rule.action.redirect.extensionPath !== undefined;
const isCsp = rule =>
rule.action !== undefined &&
rule.action.type === 'modifyHeaders';
const isRemoveparam = rule =>
rule.action !== undefined &&
rule.action.type === 'redirect' &&
rule.action.redirect.transform !== undefined;
const { ruleset } = network;
const good = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRegex(rule) === false &&
isRedirect(rule) === false &&
isCsp(rule) === false &&
isRemoveparam(rule) === false
);
const unsupported = ruleset.filter(rule =>
isUnsupported(rule)
);
const regexes = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRegex(rule) &&
isRedirect(rule) === false &&
isCsp(rule) === false &&
isRemoveparam(rule) === false
);
const redirects = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRedirect(rule)
);
const headers = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isCsp(rule)
);
const removeparams = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRemoveparam(rule)
);
const out = [
`dnrRulesetFromRawLists(${JSON.stringify(result.listNames, null, 2)})`,
`Run time: ${result.runtime} ms`,
`Filters count: ${network.filterCount}`,
`Accepted filter count: ${network.acceptedFilterCount}`,
`Rejected filter count: ${network.rejectedFilterCount}`,
`Un-DNR-able filter count: ${unsupported.length}`,
`Resulting DNR rule count: ${ruleset.length}`,
];
out.push(`+ Good filters (${good.length}): ${JSON.stringify(good, replacer, 2)}`);
out.push(`+ Regex-based filters (${regexes.length}): ${JSON.stringify(regexes, replacer, 2)}`);
out.push(`+ 'redirect=' filters (${redirects.length}): ${JSON.stringify(redirects, replacer, 2)}`);
out.push(`+ 'csp=' filters (${headers.length}): ${JSON.stringify(headers, replacer, 2)}`);
out.push(`+ 'removeparam=' filters (${removeparams.length}): ${JSON.stringify(removeparams, replacer, 2)}`);
out.push(`+ Unsupported filters (${unsupported.length}): ${JSON.stringify(unsupported, replacer, 2)}`);
out.push(`+ generichide exclusions (${network.generichideExclusions.length}): ${JSON.stringify(network.generichideExclusions, replacer, 2)}`);
if ( result.specificCosmetic ) {
out.push(`+ Cosmetic filters: ${result.specificCosmetic.size}`);
for ( const details of result.specificCosmetic ) {
out.push(` ${JSON.stringify(details)}`);
}
} else {
out.push(' Cosmetic filters: 0');
}
return out.join('\n');
}
/******************************************************************************/ /******************************************************************************/
dom.on('#console-clear', 'click', ( ) => { dom.on('#console-clear', 'click', ( ) => {
@ -146,7 +241,7 @@ dom.on('#snfe-todnr', 'click', ev => {
vAPI.messaging.send('devTools', { vAPI.messaging.send('devTools', {
what: 'snfeToDNR', what: 'snfeToDNR',
}).then(result => { }).then(result => {
log(result); log(toDNRText(result));
dom.attr(button, 'disabled', null); dom.attr(button, 'disabled', null);
}); });
}); });

View File

@ -42,7 +42,6 @@ import {
import cacheStorage from './cachestorage.js'; import cacheStorage from './cachestorage.js';
import cosmeticFilteringEngine from './cosmetic-filtering.js'; import cosmeticFilteringEngine from './cosmetic-filtering.js';
import { denseBase64 } from './base64-custom.js'; import { denseBase64 } from './base64-custom.js';
import { dnrRulesetFromRawLists } from './static-dnr-filtering.js';
import { filteringBehaviorChanged } from './broadcast.js'; import { filteringBehaviorChanged } from './broadcast.js';
import htmlFilteringEngine from './html-filtering.js'; import htmlFilteringEngine from './html-filtering.js';
import { i18n$ } from './i18n.js'; import { i18n$ } from './i18n.js';
@ -1897,95 +1896,15 @@ const onMessage = function(request, sender, callback) {
), ),
env: vAPI.webextFlavor.env, env: vAPI.webextFlavor.env,
}; };
const t0 = Date.now(); import('./static-dnr-filtering.js').then(module => {
dnrRulesetFromRawLists(listPromises, options).then(result => { const t0 = Date.now();
const { network } = result; module.dnrRulesetFromRawLists(listPromises, options).then(dnrdata => {
const replacer = (k, v) => { dnrdata.listNames = listNames;
if ( k.startsWith('__') ) { return; } dnrdata.runtime = Date.now() - t0;
if ( Array.isArray(v) ) { callback(s14e.serialize(dnrdata));
return v.sort(); })
} }).catch(reason => {
if ( v instanceof Object ) { callback(reason);
const sorted = {};
for ( const kk of Object.keys(v).sort() ) {
sorted[kk] = v[kk];
}
return sorted;
}
return v;
};
const isUnsupported = rule =>
rule._error !== undefined;
const isRegex = rule =>
rule.condition !== undefined &&
rule.condition.regexFilter !== undefined;
const isRedirect = rule =>
rule.action !== undefined &&
rule.action.type === 'redirect' &&
rule.action.redirect.extensionPath !== undefined;
const isCsp = rule =>
rule.action !== undefined &&
rule.action.type === 'modifyHeaders';
const isRemoveparam = rule =>
rule.action !== undefined &&
rule.action.type === 'redirect' &&
rule.action.redirect.transform !== undefined;
const runtime = Date.now() - t0;
const { ruleset } = network;
const good = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRegex(rule) === false &&
isRedirect(rule) === false &&
isCsp(rule) === false &&
isRemoveparam(rule) === false
);
const unsupported = ruleset.filter(rule =>
isUnsupported(rule)
);
const regexes = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRegex(rule) &&
isRedirect(rule) === false &&
isCsp(rule) === false &&
isRemoveparam(rule) === false
);
const redirects = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRedirect(rule)
);
const headers = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isCsp(rule)
);
const removeparams = ruleset.filter(rule =>
isUnsupported(rule) === false &&
isRemoveparam(rule)
);
const out = [
`dnrRulesetFromRawLists(${JSON.stringify(listNames, null, 2)})`,
`Run time: ${runtime} ms`,
`Filters count: ${network.filterCount}`,
`Accepted filter count: ${network.acceptedFilterCount}`,
`Rejected filter count: ${network.rejectedFilterCount}`,
`Un-DNR-able filter count: ${unsupported.length}`,
`Resulting DNR rule count: ${ruleset.length}`,
];
out.push(`+ Good filters (${good.length}): ${JSON.stringify(good, replacer, 2)}`);
out.push(`+ Regex-based filters (${regexes.length}): ${JSON.stringify(regexes, replacer, 2)}`);
out.push(`+ 'redirect=' filters (${redirects.length}): ${JSON.stringify(redirects, replacer, 2)}`);
out.push(`+ 'csp=' filters (${headers.length}): ${JSON.stringify(headers, replacer, 2)}`);
out.push(`+ 'removeparam=' filters (${removeparams.length}): ${JSON.stringify(removeparams, replacer, 2)}`);
out.push(`+ Unsupported filters (${unsupported.length}): ${JSON.stringify(unsupported, replacer, 2)}`);
out.push(`+ generichide exclusions (${network.generichideExclusions.length}): ${JSON.stringify(network.generichideExclusions, replacer, 2)}`);
if ( result.specificCosmetic ) {
out.push(`+ Cosmetic filters: ${result.specificCosmetic.size}`);
for ( const details of result.specificCosmetic ) {
out.push(` ${JSON.stringify(details)}`);
}
} else {
out.push(' Cosmetic filters: 0');
}
callback(out.join('\n'));
}); });
return; return;
} }