Skip to content

Commit

Permalink
Merge branch 'feature/AG-16687_01' of ssh://bit.adguard.com:7999/adgu…
Browse files Browse the repository at this point in the history
…ard-filters/scriptlets into feature/AG-16687_01
  • Loading branch information
slavaleleka committed Oct 17, 2022
2 parents 93efce6 + 0976bb9 commit 18a5a61
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/scriptlets/xml-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch) {
}

let shouldPruneResponse = true;
let shouldLogResponse = false;
// eslint-disable-next-line no-console
const log = console.log.bind(console);
if (!propsToRemove) {
// If "propsToRemove" is not defined, then response shouldn't be pruned
// but it should be logged in browser console
shouldPruneResponse = false;
shouldLogResponse = true;
}

const urlMatchRegexp = toRegExp(urlToMatch);
Expand Down Expand Up @@ -125,15 +123,12 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch) {
if (thisArg.readyState === 4) {
const { response } = thisArg;
thisArg.removeEventListener('readystatechange', pruneResponse);
if (!shouldPruneResponse && shouldLogResponse) {
if (!shouldPruneResponse) {
if (isXML(response)) {
log(`XMLHttpRequest.open() URL: ${xhrURL}\nresponse: ${response}`);
}
} else {
shouldPruneResponse = true;
const prunedResponseContent = pruneXML(response);
// pruneXML set shouldPruneResponse to false
// in case if response shouldn't be pruned
if (shouldPruneResponse) {
Object.defineProperty(thisArg, 'response', {
value: prunedResponseContent,
Expand All @@ -143,6 +138,11 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch) {
});
hit(source);
}
// In case if response shouldn't be pruned
// pruneXML sets shouldPruneResponse to false
// so it's necessary to set it to true again
// otherwise response will be only logged
shouldPruneResponse = true;
}
}
});
Expand All @@ -167,15 +167,12 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch) {
if (urlMatchRegexp.test(fetchURL)) {
return nativeFetch.apply(this, args).then((response) => {
return response.text().then((text) => {
if (!shouldPruneResponse && shouldLogResponse) {
if (!shouldPruneResponse) {
if (isXML(text)) {
log(`fetch URL: ${fetchURL}\nresponse text: ${text}`);
}
return Reflect.apply(target, thisArg, args);
}
shouldPruneResponse = true;
// pruneXML set shouldPruneResponse to false
// in case if response shouldn't be pruned
const prunedText = pruneXML(text);
if (shouldPruneResponse) {
hit(source);
Expand All @@ -185,6 +182,11 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch) {
headers: response.headers,
});
}
// In case if response shouldn't be pruned
// pruneXML sets shouldPruneResponse to false
// so it's necessary to set it to true again
// otherwise response will be only logged
shouldPruneResponse = true;
return Reflect.apply(target, thisArg, args);
});
});
Expand Down

0 comments on commit 18a5a61

Please sign in to comment.