Skip to content

Commit

Permalink
add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 21, 2022
1 parent 42b271b commit ed2a348
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
shouldReplace = matchRequestProps(propsToMatch, xhrData);
}

// Trap setRequestHeader of target xhr object to mimic request headers later
// Trap setRequestHeader of target xhr object to mimic request headers later
if (shouldReplace) {
const setRequestHeaderWrapper = (target, thisArg, args) => {
// Collect headers
requestHeaders.push(args);
return Reflect.apply(target, thisArg, args);
};
Expand All @@ -115,6 +116,8 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
apply: setRequestHeaderWrapper,
};

// setRequestHeader can only be called on open xhr object,
// so we can safely proxy it here
thisArg.setRequestHeader = new Proxy(thisArg.setRequestHeader, setRequestHeaderHandler);
}

Expand All @@ -126,6 +129,11 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
return Reflect.apply(target, thisArg, args);
}

/**
* Create separate XHR request with original request's input
* to be able to collect response data without triggering
* listeners on original XHR object
*/
const secretXhr = new XMLHttpRequest();
secretXhr.addEventListener('readystatechange', () => {
if (secretXhr.readyState !== 4) {
Expand Down

0 comments on commit ed2a348

Please sign in to comment.