Skip to content

Commit e8202af

Browse files
committed
Improve prevent-fetch scriptlet
- Add support for negated matches - Log caller's arguments when verbose logging is enabled
1 parent 89f0209 commit e8202af

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

assets/resources/scriptlets.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function safeSelf() {
9797
},
9898
initPattern(pattern, options = {}) {
9999
if ( pattern === '' ) {
100-
return { matchAll: true };
100+
return { matchAll: true, expect: true };
101101
}
102102
const expect = (options.canNegate !== true || pattern.startsWith('!') === false);
103103
if ( expect === false ) {
@@ -2151,7 +2151,7 @@ function noFetchIf(
21512151
key = 'url';
21522152
value = condition;
21532153
}
2154-
needles.push({ key, re: safe.patternToRegex(value) });
2154+
needles.push({ key, pattern: safe.initPattern(value, { canNegate: true }) });
21552155
}
21562156
const validResponseProps = {
21572157
ok: [ false, true ],
@@ -2180,6 +2180,9 @@ function noFetchIf(
21802180
const details = args[0] instanceof self.Request
21812181
? args[0]
21822182
: Object.assign({ url: args[0] }, args[1]);
2183+
if ( safe.logLevel > 1 ) {
2184+
safe.uboLog(logPrefix, `apply:\n\t${Object.entries(details).map(a => `${a[0]}: ${a[1]}`).join('\n\t')}`);
2185+
}
21832186
let proceed = true;
21842187
try {
21852188
const props = new Map();
@@ -2198,10 +2201,10 @@ function noFetchIf(
21982201
return Reflect.apply(target, thisArg, args);
21992202
}
22002203
proceed = needles.length === 0;
2201-
for ( const { key, re } of needles ) {
2204+
for ( const { key, pattern } of needles ) {
22022205
if (
2203-
props.has(key) === false ||
2204-
re.test(props.get(key)) === false
2206+
pattern.expect && props.has(key) === false ||
2207+
safe.testPattern(pattern, props.get(key)) === false
22052208
) {
22062209
proceed = true;
22072210
break;

0 commit comments

Comments
 (0)