8000 [mv3] Add support for ancestor context syntax in scriptlets · gorhill/uBlock@d006fd0 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Commit d006fd0

Browse files
committed
[mv3] Add support for ancestor context syntax in scriptlets
Related commit: a483f79
1 parent 536f0fb commit d006fd0

File tree

3 files changed

+78
-107
lines changed

3 files changed

+78
-107
lines changed

platform/mv3/make-rulesets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1526,8 +1526,8 @@ async function main() {
15261526

15271527
await rulesetFromURLs({
15281528
id: 'est-0',
1529-
group: 'regions',
1530-
lang: 'et',
1529+
group: 'regions',
1530+
lang: 'et',
15311531
name: '🇪🇪ee: Eesti saitidele kohandatud filter',
15321532
enabled: false,
15331533
urls: [ 'https://ubol-et.adblock.ee/list.txt' ],

platform/mv3/make-scriptlets.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ export function compile(assetDetails, details) {
9797
world: resourceEntry.world,
9898
args: new Map(),
9999
hostnames: new Map(),
100-
entities: new Map(),
101100
exceptions: new Map(),
101+
hasEntities: false,
102+
hasAncestors: false,
102103
matches: new Set(),
103104
});
104105
}
@@ -109,23 +110,21 @@ export function compile(assetDetails, details) {
109110
const iArgs = scriptletDetails.args.get(argsToken);
110111
if ( details.matches ) {
111112
for ( const hn of details.matches ) {
112-
if ( hn.endsWith('.*') ) {
113+
const isEntity = hn.endsWith('.*') || hn.endsWith('.*>>');
114+
scriptletDetails.hasEntities ||= isEntity;
115+
const isAncestor = hn.endsWith('>>')
116+
scriptletDetails.hasAncestors ||= isAncestor;
117+
if ( isEntity || isAncestor ) {
113118
scriptletDetails.matches.clear();
114119
scriptletDetails.matches.add('*');
115-
const entity = hn.slice(0, -2);
116-
if ( scriptletDetails.entities.has(entity) === false ) {
117-
scriptletDetails.entities.set(entity, new Set());
118-
}
119-
scriptletDetails.entities.get(entity).add(iArgs);
120-
} else {
121-
if ( scriptletDetails.matches.has('*') === false ) {
122-
scriptletDetails.matches.add(hn);
123-
}
124-
if ( scriptletDetails.hostnames.has(hn) === false ) {
125-
scriptletDetails.hostnames.set(hn, new Set());
126-
}
127-
scriptletDetails.hostnames.get(hn).add(iArgs);
128120
}
121+
if ( scriptletDetails.matches.has('*') === false ) {
122+
scriptletDetails.matches.add(hn);
123+
}
124+
if ( scriptletDetails.hostnames.has(hn) === false ) {
125+
scriptletDetails.hostnames.set(hn, new Set());
126+
}
127+
scriptletDetails.hostnames.get(hn).add(iArgs);
129128
}
130129
} else {
131130
scriptletDetails.matches.add('*');
@@ -172,8 +171,12 @@ export async function commit(rulesetId, path, writeFn) {
172171
JSON.stringify(patchHnMap(details.hostnames))
173172
);
174173
content = safeReplace(content,
175-
'self.$entitiesMap$',
176-
JSON.stringify(patchHnMap(details.entities))
174+
'self.$hasEntities$',
175+
JSON.stringify(details.hasEntities)
176+
);
177+
content = safeReplace(content,
178+
'self.$hasAncestors$',
179+
JSON.stringify(details.hasAncestors)
177180
);
178181
content = safeReplace(content,
179182
'self.$exceptionsMa F438 p$',

platform/mv3/scriptlets/scriptlet.template.js

+56-88
Original file line numberDiff line numberDiff line change
@@ -20,129 +20,97 @@
2020
2121
*/
2222

23-
/* eslint-disable indent */
24-
2523
// ruleset: $rulesetId$
2624

2725
// Important!
2826
// Isolate from global scope
2927

3028
// Start of local scope
31-
(( ) => {
29+
(function uBOL_$scriptletName$() {
3230

3331
/******************************************************************************/
3432

35-
// Start of code to inject
36-
const uBOL_$scriptletName$ = function() {
33+
function $scriptletName$(){}
3734

38-
const scriptletGlobals = {}; // eslint-disable-line
35+
/******************************************************************************/
3936

37+
const scriptletGlobals = {}; // eslint-disable-line
4038
const argsList = self.$argsList$;
41-
4239
const hostnamesMap = new Map(self.$hostnamesMap$);
43-
44-
const entitiesMap = new Map(self.$entitiesMap$);
45-
4640
const exceptionsMap = new Map(self.$exceptionsMap$);
41+
const hasEntities = self.$hasEntities$;
42+
const hasAncestors = self.$hasAncestors$;
4743

48-
/******************************************************************************/
49-
50-
function $scriptletName$(){}
51-
52-
/******************************************************************************/
53-
54-
const hnParts = [];
55-
try {
56-
let origin = document.location.origin;
57-
if ( origin === 'null' ) {
58-
const origins = document.location.ancestorOrigins || [];
59-
for ( let i = 0; i < origins.length; i++ ) {
60-
origin = origins[i];
61-
if ( origin !== 'null' ) { break; }
44+
const collectArgIndices = (hn, map, out) => {
45+
let argsIndices = map.get(hn);
46+
if ( argsIndices === undefined ) { return; }
47+
if ( typeof argsIndices !== 'number' ) {
48+
for ( const argsIndex of argsIndices ) {
49+
out.add(argsIndex);
6250
}
51+
} else {
52+
out.add(argsIndices);
6353
}
64-
const beg = origin.lastIndexOf('://');
65-
if ( beg === -1 ) { return; }
66-
let hn = origin.slice(beg+3)
67-
const end = hn.indexOf(':');
68-
if ( end !== -1 ) { hn = hn.slice(0, end); }
69-
hnParts.push(...hn.split('.'));
70-
} catch {
71-
}
72-
cons C95D t hnpartslen = hnParts.length;
73-
if ( hnpartslen === 0 ) { return; }
74-
75-
const todoIndices = new Set();
76-
const tonotdoIndices = [];
54+
};
7755

78-
// Exceptions
79-
if ( exceptionsMap.size !== 0 ) {
56+
const indicesFromHostname = (hostname, suffix = '') => {
57+
const hnParts = hostname.split('.');
58+
const hnpartslen = hnParts.length;
59+
if ( hnpartslen === 0 ) { return; }
8060
for ( let i = 0; i < hnpartslen; i++ ) {
81-
const hn = hnParts.slice(i).join('.');
82-
const excepted = exceptionsMap.get(hn);
83-
if ( excepted ) { tonotdoIndices.push(...excepted); }
61+
const hn = `${hnParts.slice(i).join('.')}${suffix}`;
62+
collectArgIndices(hn, hostnamesMap, todoIndices);
63+
collectArgIndices(hn, exceptionsMap, tonotdoIndices);
8464
}
85-
exceptionsMap.clear();
86-
}
87-
88-
// Hostname-based
89-
if ( hostnamesMap.size !== 0 ) {
90-
const collectArgIndices = hn => {
91-
let argsIndices = hostnamesMap.get(hn);
92-
if ( argsIndices === undefined ) { return; }
93-
if ( typeof argsIndices === 'number' ) { argsIndices = [ argsIndices ]; }
94-
for ( const argsIndex of argsIndices ) {
95-
if ( tonotdoIndices.includes(argsIndex) ) { continue; }
96-
todoIndices.add(argsIndex);
65+
if ( hasEntities ) {
66+
const n = hnpartslen - 1;
67+
for ( let i = 0; i < n; i++ ) {
68+
for ( let j = n; j > i; j-- ) {
69+
const en = `${hnParts.slice(i,j).join('.')}.*${suffix}`;
70+
collectArgIndices(en, hostnamesMap, todoIndices);
71+
collectArgIndices(en, exceptionsMap, tonotdoIndices);
72+
}
9773
}
98-
};
99-
for ( let i = 0; i < hnpartslen; i++ ) {
100-
const hn = hnParts.slice(i).join('.');
101-
collectArgIndices(hn);
10274
}
103-
collectArgIndices('*');
104-
hostnamesMap.clear();
105-
}
75+
};
10676

107-
// Entity-based
108-
if ( entitiesMap.size !== 0 ) {
109-
const n = hnpartslen - 1;
110-
for ( let i = 0; i < n; i++ ) {
111-
for ( let j = n; j > i; j-- ) {
112-
const en = hnParts.slice(i,j).join('.');
113-
let argsIndices = entitiesMap.get(en);
114-
if ( argsIndices === undefined ) { continue; }
115-
if ( typeof argsIndices === 'number' ) { argsIndices = [ argsIndices ]; }
116-
for ( const argsIndex of argsIndices ) {
117-
if ( tonotdoIndices.includes(argsIndex) ) { continue; }
118-
todoIndices.add(argsIndex);
119-
}
120-
}
77+
const entries = (( ) => {
78+
const docloc = document.location;
79+
const origins = [ docloc.origin ];
80+
if ( docloc.ancestorOrigins ) {
81+
origins.push(...docloc.ancestorOrigins);
82+
}
83+
return origins.map((origin, i) => {
84+
const beg = origin.lastIndexOf('://');
85+
if ( beg === -1 ) { return; }
86+
const hn = origin.slice(beg+3)
87+
const end = hn.indexOf(':');
88+
return { hn: end === -1 ? hn : hn.slice(0, end), i };
89+
}).filter(a => a !== undefined);
90+
})();
91+
if ( entries.length === 0 ) { return; }
92+
93+
const todoIndices = new Set();
94+
const tonotdoIndices = new Set();
95+
96+
indicesFromHostname(entries[0].hn);
97+
if ( hasAncestors ) {
98+
for ( const entry of entries ) {
99+
if ( entry.i === 0 ) { continue; }
100+
indicesFromHostname(entry.hn, '>>');
121101
}
122-
entitiesMap.clear();
123102
}
124103

125104
// Apply scriplets
126105
for ( const i of todoIndices ) {
106+
if ( tonotdoIndices.has(i) ) { continue; }
127107
try { $scriptletName$(...argsList[i]); }
128108
catch { }
129109
}
130-
argsList.length = 0;
131-
132-
/******************************************************************************/
133-
134-
};
135-
// End of code to inject
136-
137-
/******************************************************************************/
138-
139-
uBOL_$scriptletName$();
140110

141111
/******************************************************************************/
142112

143113
// End of local scope
144114
})();
145115

146-
/******************************************************************************/
147-
148116
void 0;

0 commit comments

Comments
 (0)
0