1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-05 10:22:30 +01:00

Improve trusted-replace-[fetch|xhr]-response scriptlets

Related discussion:
https://github.com/uBlockOrigin/uBlock-discussions/discussions/831#discussioncomment-9750621
This commit is contained in:
Raymond Hill 2024-06-13 09:32:30 -04:00
parent 441cf7c2b8
commit 9072772f61
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -1332,6 +1332,8 @@ function replaceFetchResponseFn(
if ( pattern === '*' ) { pattern = '.*'; }
const rePattern = safe.patternToRegex(pattern);
const propNeedles = parsePropertiesToMatch(propsToMatch, 'url');
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
const reIncludes = extraArgs.includes ? safe.patternToRegex(extraArgs.includes) : null;
self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) {
const fetchPromise = Reflect.apply(target, thisArg, args);
@ -1361,6 +1363,9 @@ function replaceFetchResponseFn(
return fetchPromise.then(responseBefore => {
const response = responseBefore.clone();
return response.text().then(textBefore => {
if ( reIncludes && reIncludes.test(textBefore) === false ) {
return responseBefore;
}
const textAfter = textBefore.replace(rePattern, replacement);
const outcome = textAfter !== textBefore ? 'match' : 'nomatch';
if ( outcome === 'nomatch' ) { return responseBefore; }
@ -4365,6 +4370,8 @@ function trustedReplaceXhrResponse(
if ( pattern === '*' ) { pattern = '.*'; }
const rePattern = safe.patternToRegex(pattern);
const propNeedles = parsePropertiesToMatch(propsToMatch, 'url');
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
const reIncludes = extraArgs.includes ? safe.patternToRegex(extraArgs.includes) : null;
self.XMLHttpRequest = class extends self.XMLHttpRequest {
open(method, url, ...args) {
const outerXhr = this;
@ -4402,6 +4409,9 @@ function trustedReplaceXhrResponse(
if ( typeof innerResponse !== 'string' ) {
return (xhrDetails.response = innerResponse);
}
if ( reIncludes && reIncludes.test(innerResponse) === false ) {
return (xhrDetails.response = innerResponse);
}
const textBefore = innerResponse;
const textAfter = textBefore.replace(rePattern, replacement);
if ( textAfter !== textBefore ) {