mirror of
https://github.com/gorhill/uBlock.git
synced 2024-07-05 11:37:01 +02: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:
parent
441cf7c2b8
commit
9072772f61
|
@ -1332,6 +1332,8 @@ function replaceFetchResponseFn(
|
||||||
if ( pattern === '*' ) { pattern = '.*'; }
|
if ( pattern === '*' ) { pattern = '.*'; }
|
||||||
const rePattern = safe.patternToRegex(pattern);
|
const rePattern = safe.patternToRegex(pattern);
|
||||||
const propNeedles = parsePropertiesToMatch(propsToMatch, 'url');
|
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, {
|
self.fetch = new Proxy(self.fetch, {
|
||||||
apply: function(target, thisArg, args) {
|
apply: function(target, thisArg, args) {
|
||||||
const fetchPromise = Reflect.apply(target, thisArg, args);
|
const fetchPromise = Reflect.apply(target, thisArg, args);
|
||||||
|
@ -1361,6 +1363,9 @@ function replaceFetchResponseFn(
|
||||||
return fetchPromise.then(responseBefore => {
|
return fetchPromise.then(responseBefore => {
|
||||||
const response = responseBefore.clone();
|
const response = responseBefore.clone();
|
||||||
return response.text().then(textBefore => {
|
return response.text().then(textBefore => {
|
||||||
|
if ( reIncludes && reIncludes.test(textBefore) === false ) {
|
||||||
|
return responseBefore;
|
||||||
|
}
|
||||||
const textAfter = textBefore.replace(rePattern, replacement);
|
const textAfter = textBefore.replace(rePattern, replacement);
|
||||||
const outcome = textAfter !== textBefore ? 'match' : 'nomatch';
|
const outcome = textAfter !== textBefore ? 'match' : 'nomatch';
|
||||||
if ( outcome === 'nomatch' ) { return responseBefore; }
|
if ( outcome === 'nomatch' ) { return responseBefore; }
|
||||||
|
@ -4365,6 +4370,8 @@ function trustedReplaceXhrResponse(
|
||||||
if ( pattern === '*' ) { pattern = '.*'; }
|
if ( pattern === '*' ) { pattern = '.*'; }
|
||||||
const rePattern = safe.patternToRegex(pattern);
|
const rePattern = safe.patternToRegex(pattern);
|
||||||
const propNeedles = parsePropertiesToMatch(propsToMatch, 'url');
|
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 {
|
self.XMLHttpRequest = class extends self.XMLHttpRequest {
|
||||||
open(method, url, ...args) {
|
open(method, url, ...args) {
|
||||||
const outerXhr = this;
|
const outerXhr = this;
|
||||||
|
@ -4402,6 +4409,9 @@ function trustedReplaceXhrResponse(
|
||||||
if ( typeof innerResponse !== 'string' ) {
|
if ( typeof innerResponse !== 'string' ) {
|
||||||
return (xhrDetails.response = innerResponse);
|
return (xhrDetails.response = innerResponse);
|
||||||
}
|
}
|
||||||
|
if ( reIncludes && reIncludes.test(innerResponse) === false ) {
|
||||||
|
return (xhrDetails.response = innerResponse);
|
||||||
|
}
|
||||||
const textBefore = innerResponse;
|
const textBefore = innerResponse;
|
||||||
const textAfter = textBefore.replace(rePattern, replacement);
|
const textAfter = textBefore.replace(rePattern, replacement);
|
||||||
if ( textAfter !== textBefore ) {
|
if ( textAfter !== textBefore ) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user