Skip to content

Commit fd67cde

Browse files
committed
[mv3] Add support for broad scriptlet exception (#@#js())
1 parent c25d4da commit fd67cde

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

‎platform/mv3/extension/js/offscreen/make-scriptlets.js‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const worldTemplate = {
3232
scriptletFunctions: new Map(),
3333
allFunctions: new Map(),
3434
args: new Map(),
35-
arglists: new Map(),
35+
arglists: new Map([['',0]]),
3636
hostnames: new Map(),
3737
regexesOrPaths: new Map(),
3838
matches: new Set(),
@@ -64,6 +64,29 @@ function createScriptletCoreCode(worldDetails, resourceEntry) {
6464

6565
/******************************************************************************/
6666

67+
function compileBroadExclusion(details) {
68+
if ( Boolean(details.excludeMatches?.length) === false ) { return; }
69+
for ( const worldDetails of Object.values(worlds) ) {
70+
for ( const hn of details.excludeMatches ) {
71+
if ( isHnRegexOrPath(hn) ) {
72+
const refs = worldDetails.regexesOrPaths.get(hn) ?? new Set();
73+
if ( refs.size === 0 ) {
74+
worldDetails.regexesOrPaths.set(hn, refs);
75+
}
76+
refs.add(0);
77+
continue;
78+
}
79+
const refs = worldDetails.hostnames.get(hn) ?? new Set();
80+
if ( refs.size === 0 ) {
81+
worldDetails.hostnames.set(hn, refs);
82+
}
83+
refs.add(0);
84+
}
85+
}
86+
}
87+
88+
/******************************************************************************/
89+
6790
export function reset() {
6891
worlds.ISOLATED = structuredClone(worldTemplate);
6992
worlds.MAIN = structuredClone(worldTemplate);
@@ -72,6 +95,9 @@ export function reset() {
7295
/******************************************************************************/
7396

7497
export function compile(rulesetId, details) {
98+
if ( details.args.length === 0 ) {
99+
return compileBroadExclusion(details);
100+
}
75101
if ( details.args[0].endsWith('.js') === false ) {
76102
details.args[0] += '.js';
77103
}

‎platform/mv3/extension/js/offscreen/scriptlet.template.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ if ( $hasRegexes$ ) {
162162
}
163163

164164
// Execute scriptlets
165-
if ( todo.size ) {
165+
if ( todo.size && todo.has(0) === false ) {
166166
const $scriptletFunctions$ = self.$scriptletFunctions$;
167167
const $scriptletArgs$ = self.$scriptletArgs$;
168168
const $scriptletArglists$ = self.$scriptletArglists$;

‎src/js/static-dnr-filtering.js‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,25 @@ function addExtendedToDNR(context, parser) {
107107
context.scriptletFilters = new Map();
108108
}
109109
const exception = parser.isException();
110-
const args = parser.getScriptletArgs();
110+
const args = parser.getScriptletArgs() || [];
111111
const argsToken = JSON.stringify(args);
112112
for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) {
113113
if ( bad ) { continue; }
114-
if ( exception ) { continue; }
115-
let details = context.scriptletFilters.get(argsToken);
116-
if ( details === undefined ) {
117-
context.scriptletFilters.set(argsToken, details = { args });
114+
if ( exception && not ) { continue; }
115+
const details = context.scriptletFilters.get(argsToken) ?? {};
116+
if ( details.args === undefined ) {
117+
context.scriptletFilters.set(argsToken, details);
118+
details.args = args;
118119
if ( context.trustedSource ) {
119120
details.trustedSource = true;
120121
}
121122
}
122-
if ( not ) {
123-
if ( details.excludeMatches === undefined ) {
124-
details.excludeMatches = [];
125-
}
123+
if ( exception || not ) {
124+
details.excludeMatches ??= [];
126125
details.excludeMatches.push(hn);
127126
continue;
128127
}
129-
if ( details.matches === undefined ) {
130-
details.matches = [];
131-
}
128+
details.matches ??= [];
132129
if ( details.matches.includes('*') ) { continue; }
133130
if ( hn === '*' ) {
134131
details.matches = [ '*' ];

0 commit comments

Comments
 (0)