Skip to content

Commit c68df49

Browse files
committed
[mv3] Mind to= option when converting popup filters
Related issue: uBlockOrigin/uBOL-home#745
1 parent fb2775d commit c68df49

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

platform/mv3/extension/js/scripting/prevent-popup.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
a.slice(i).join('.')
3636
);
3737

38-
const hostnameSearch = (hostnames, targets) => {
38+
const hostnameSearch = hostnames => {
3939
let l = 0, i = 0, d = 0;
4040
let r = hostnames.length;
4141
let candidate;
@@ -59,28 +59,33 @@
5959
return -1;
6060
};
6161

62-
const regexSearch = (regexes, target) => {
62+
const regexSearch = regexes => {
6363
for ( let i = 0; i < regexes.length; i += 2 ) {
64-
const key = regexes[i+0];
65-
if ( target.includes(key.slice(1)) === false ) { continue; }
66-
const re = new RegExp(regexes[i+1], key.charAt(0).trimEnd());
67-
if ( re.test(target) ) { return i; }
64+
if ( href.includes(regexes[i+0]) === false ) { continue; }
65+
const entries = JSON.parse(regexes[i+1]);
66+
for ( const entry of entries ) {
67+
if ( entry.xto && hostnameSearch(entry.xto) ) { continue; }
68+
if ( entry.to && hostnameSearch(entry.to) === -1 ) { continue; }
69+
const re = new RegExp(entry.re, entry.f);
70+
if ( re.test(href) === false ) { continue; }
71+
return i;
72+
}
6873
}
6974
return -1;
7075
}
7176

7277
let shouldClose = false;
7378
for ( const { block } of preventPopupDetails ) {
74-
if ( hostnameSearch(block.hostnames, targets) === -1 ) {
75-
if ( regexSearch(block.regexes, href) === -1 ) { continue; }
79+
if ( hostnameSearch(block.hostnames) === -1 ) {
80+
if ( regexSearch(block.regexes) === -1 ) { continue; }
7681
}
7782
shouldClose = true;
7883
break;
7984
}
8085
if ( shouldClose === false ) { return; }
8186
for ( const { allow } of preventPopupDetails ) {
82-
if ( hostnameSearch(allow.hostnames, targets) === -1 ) {
83-
if ( regexSearch(allow.regexes, href) === -1 ) { continue; }
87+
if ( hostnameSearch(allow.hostnames) === -1 ) {
88+
if ( regexSearch(allow.regexes) === -1 ) { continue; }
8489
}
8590
shouldClose = false;
8691
break;

platform/mv3/make-rulesets.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,17 @@ async function processPopupRules(assetDetails, popupRules) {
911911
}
912912
if ( re === undefined ) { return data; }
913913
const token = literalStrFromRegex(re).slice(0, 7);
914-
const key = `${isUrlFilterCaseSensitive ? ' ' : 'i'}${token}`;
915-
if ( realm.regexes.has(key) ) {
916-
realm.regexes.set(key, `${realm.regexes.get(key)}|${re}`);
917-
} else {
918-
realm.regexes.set(key, re);
914+
const details = realm.regexes.get(token) ?? { token, rules: [] };
915+
if ( details.rules.length === 0 ) {
916+
realm.regexes.set(token, details)
917+
}
918+
const entry = { re, f: isUrlFilterCaseSensitive ? '' : 'i' };
919+
details.rules.push(entry);
920+
if ( condition.requestDomains ) {
921+
entry.to = condition.requestDomains.sort(hostnameCompare);
922+
}
923+
if ( condition.excludedRequestDomains ) {
924+
entry.xto = condition.excludedRequestDomains.sort(hostnameCompare);
919925
}
920926
return data;
921927
}
@@ -949,9 +955,13 @@ async function processPopupRules(assetDetails, popupRules) {
949955
const count = data.block.hostnames.length + data.block.regexes.size;
950956
if ( count === 0 ) { return; }
951957
data.block.hostnames = data.block.hostnames.toSorted(hostnameCompare);
952-
data.block.regexes = Array.from(data.block.regexes).flat();
958+
data.block.regexes = Array.from(data.block.regexes.values()).map(a =>
959+
[ a.token, JSON.stringify(a.rules) ]
960+
).flat();
953961
data.allow.hostnames = data.allow.hostnames.toSorted(hostnameCompare);
954-
data.allow.regexes = Array.from(data.allow.regexes).flat();
962+
data.allow.regexes = Array.from(data.allow.regexes.values()).map(a =>
963+
[ a.token, JSON.stringify(a.rules) ]
964+
).flat();
955965
const originalScriptletMap = await loadAllSourceScriptlets();
956966
let patchedScriptlet = originalScriptletMap.get(`prevent-popup`);
957967
patchedScriptlet = safeReplace(patchedScriptlet,

0 commit comments

Comments
 (0)