mirror of https://github.com/gorhill/uBlock.git
Improve `trusted-replace-fetch-response` as per feedback
Specifically, the variable argument `log` can take one of three values to enable logging mechanism: ..., log, match => log only when there is a match ..., log, nomatch => log only when there is no match ..., log, all => log unconditionally
This commit is contained in:
parent
60fcdf8e71
commit
2c2fd5f21d
|
@ -3358,7 +3358,11 @@ function trustedReplaceFetchResponse(
|
||||||
for ( const needle of propsToMatch.split(/\s+/) ) {
|
for ( const needle of propsToMatch.split(/\s+/) ) {
|
||||||
const [ prop, value ] = needle.split(':');
|
const [ prop, value ] = needle.split(':');
|
||||||
if ( prop === '' ) { continue; }
|
if ( prop === '' ) { continue; }
|
||||||
propNeedles.set(prop, patternToRegex(value));
|
if ( value === undefined ) {
|
||||||
|
propNeedles.set('url', prop);
|
||||||
|
} else {
|
||||||
|
propNeedles.set(prop, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.fetch = new Proxy(self.fetch, {
|
self.fetch = new Proxy(self.fetch, {
|
||||||
apply: function(target, thisArg, args) {
|
apply: function(target, thisArg, args) {
|
||||||
|
@ -3367,19 +3371,17 @@ function trustedReplaceFetchResponse(
|
||||||
}
|
}
|
||||||
const fetchPromise = Reflect.apply(target, thisArg, args);
|
const fetchPromise = Reflect.apply(target, thisArg, args);
|
||||||
if ( pattern === '' ) { return fetchPromise; }
|
if ( pattern === '' ) { return fetchPromise; }
|
||||||
let skip = false;
|
let outcome = 'match';
|
||||||
if ( propNeedles.size !== 0 ) {
|
if ( propNeedles.size !== 0 ) {
|
||||||
const fetchDetails = {};
|
const props = [
|
||||||
if ( args[0] instanceof self.Request ) {
|
'cache', 'credentials', 'destination', 'method',
|
||||||
Object.assign(fetchDetails, args[0]);
|
'redirect', 'referrer', 'referrer-policy', 'url',
|
||||||
} else {
|
];
|
||||||
Object.assign(fetchDetails, { url: args[0] });
|
const normalArgs = args[0] instanceof Object
|
||||||
}
|
? props.reduce((p, a) => { a[p] = args[0][p]; }, {})
|
||||||
if ( args[1] instanceof Object ) {
|
: { url: args[0] };
|
||||||
Object.assign(fetchDetails, args[1]);
|
for ( const prop of props ) {
|
||||||
}
|
let value = normalArgs[prop];
|
||||||
for ( const prop of Object.keys(fetchDetails) ) {
|
|
||||||
let value = fetchDetails[prop];
|
|
||||||
if ( typeof value !== 'string' ) {
|
if ( typeof value !== 'string' ) {
|
||||||
try { value = JSON.stringify(value); }
|
try { value = JSON.stringify(value); }
|
||||||
catch(ex) { }
|
catch(ex) { }
|
||||||
|
@ -3387,15 +3389,31 @@ function trustedReplaceFetchResponse(
|
||||||
if ( typeof value !== 'string' ) { continue; }
|
if ( typeof value !== 'string' ) { continue; }
|
||||||
const needle = propNeedles.get(prop);
|
const needle = propNeedles.get(prop);
|
||||||
if ( needle === undefined ) { continue; }
|
if ( needle === undefined ) { continue; }
|
||||||
if ( needle.test(value) ) { continue; }
|
if ( patternToRegex(needle).test(value) ) { continue; }
|
||||||
skip = true;
|
outcome = 'nomatch';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if ( outcome === logLevel || logLevel === 'all' ) {
|
||||||
|
log(
|
||||||
|
`trusted-replace-fetch-response (${outcome})`,
|
||||||
|
`\n\tpropsToMatch: ${JSON.stringify(Array.from(propNeedles)).slice(1,-1)}`,
|
||||||
|
`\n\tprops: ${JSON.stringify(normalArgs).slice(1,-1)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if ( skip ) { return fetchPromise; }
|
}
|
||||||
|
if ( outcome === 'nomatch' ) { return fetchPromise; }
|
||||||
return fetchPromise.then(responseBefore => {
|
return fetchPromise.then(responseBefore => {
|
||||||
return responseBefore.text().then(textBefore => {
|
return responseBefore.text().then(textBefore => {
|
||||||
const textAfter = textBefore.replace(rePattern, replacement);
|
const textAfter = textBefore.replace(rePattern, replacement);
|
||||||
|
const outcome = textAfter !== textBefore ? 'match' : 'nomatch';
|
||||||
|
if ( outcome === logLevel || logLevel === 'all' ) {
|
||||||
|
log(
|
||||||
|
`trusted-replace-fetch-response (${outcome})`,
|
||||||
|
`\n\tpattern: ${rePattern.source}`,
|
||||||
|
`\n\treplacement: ${replacement}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ( textAfter === textBefore ) { return responseBefore; }
|
||||||
const responseAfter = new Response(textAfter, {
|
const responseAfter = new Response(textAfter, {
|
||||||
status: responseBefore.status,
|
status: responseBefore.status,
|
||||||
statusText: responseBefore.statusText,
|
statusText: responseBefore.statusText,
|
||||||
|
|
Loading…
Reference in New Issue