Skip to content

Commit

Permalink
improve content extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 21, 2022
1 parent 1a094c9 commit 42b271b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
return Reflect.apply(target, thisArg, args);
};

const sendWrapper = (target, thisArg, args) => {
const sendWrapper = async (target, thisArg, args) => {
if (!shouldReplace) {
return Reflect.apply(target, thisArg, args);
}
Expand All @@ -142,10 +142,15 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
statusText,
} = secretXhr;

// Extract content from response
const content = responseText || response;
if (typeof content !== 'string') {
return;
}

const parsedPattern = pattern === getWildcardSymbol()
? MATCH_ALL_CHARACTERS_REGEX
: pattern;
const content = response || responseText;

const modifiedContent = content.replace(parsedPattern, replacement);

Expand Down Expand Up @@ -188,7 +193,11 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
});
requestHeaders = [];

nativeSend.call(secretXhr, args);
try {
nativeSend.call(secretXhr, args);
} catch {
return Reflect.apply(target, thisArg, args);
}
return undefined;
};

Expand Down

0 comments on commit 42b271b

Please sign in to comment.