Skip to content

Commit

Permalink
Merge branch 'master' into fix/AG-24527
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed Aug 4, 2023
2 parents 31d149d + 975ad11 commit 0e3b35c
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#348](https://github.com/AdguardTeam/Scriptlets/issues/348)
- issue with `obligatoryProps` in `json-prune` scriptlet
[#345](https://github.com/AdguardTeam/Scriptlets/issues/345)
- `prevent-xhr` closure bug on multiple requests
[#347](https://github.com/AdguardTeam/Scriptlets/issues/347)

## [v1.9.61] - 2023-08-01

Expand Down
2 changes: 1 addition & 1 deletion dist/build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.9.61
version=1.9.62
2 changes: 1 addition & 1 deletion dist/redirects.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# AdGuard Scriptlets (Redirects Source)
# Version 1.9.61
# Version 1.9.62
#
- title: 1x1-transparent.gif
added: v1.0.4
Expand Down
4 changes: 2 additions & 2 deletions dist/scriptlets.corelibs.json

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions dist/scriptlets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* AdGuard Scriptlets
* Version 1.9.61
* Version 1.9.62
*/

(function () {
Expand Down Expand Up @@ -6886,6 +6886,8 @@
}
var nativeOpen = window.XMLHttpRequest.prototype.open;
var nativeSend = window.XMLHttpRequest.prototype.send;
var nativeGetResponseHeader = window.XMLHttpRequest.prototype.getResponseHeader;
var nativeGetAllResponseHeaders = window.XMLHttpRequest.prototype.getAllResponseHeaders;
var xhrData;
var modifiedResponse = '';
var modifiedResponseText = '';
Expand All @@ -6899,6 +6901,9 @@
hit(source);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
// Add xhrData to thisArg to keep original values in case of multiple requests
// https://github.com/AdguardTeam/Scriptlets/issues/347
thisArg.xhrData = xhrData;
}

// Trap setRequestHeader of target xhr object to mimic request headers later;
Expand Down Expand Up @@ -6966,7 +6971,7 @@
},
// If the request is blocked, responseURL is an empty string
responseURL: {
value: responseURL || xhrData.url,
value: responseURL || thisArg.xhrData.url,
writable: false
},
responseXML: {
Expand Down Expand Up @@ -6999,7 +7004,7 @@
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
nativeOpen.apply(forgedRequest, [thisArg.xhrData.method, thisArg.xhrData.url]);

// Mimic request headers before sending
// setRequestHeader can only be called on open request objects
Expand All @@ -7026,6 +7031,9 @@
* @returns {string|null} Header value or null if header is not set.
*/
var getHeaderWrapper = function getHeaderWrapper(target, thisArg, args) {
if (!thisArg.shouldBePrevented) {
return nativeGetResponseHeader.apply(thisArg, args);
}
if (!thisArg.collectedHeaders.length) {
return null;
}
Expand All @@ -7048,6 +7056,9 @@
* @returns {string} All headers as a string. For no headers an empty string is returned.
*/
var getAllHeadersWrapper = function getAllHeadersWrapper(target, thisArg) {
if (!thisArg.shouldBePrevented) {
return nativeGetAllResponseHeaders.call(thisArg);
}
if (!thisArg.collectedHeaders.length) {
return '';
}
Expand Down Expand Up @@ -22736,6 +22747,8 @@
}
var nativeOpen = window.XMLHttpRequest.prototype.open;
var nativeSend = window.XMLHttpRequest.prototype.send;
var nativeGetResponseHeader = window.XMLHttpRequest.prototype.getResponseHeader;
var nativeGetAllResponseHeaders = window.XMLHttpRequest.prototype.getAllResponseHeaders;
var xhrData;
var modifiedResponse = "";
var modifiedResponseText = "";
Expand All @@ -22746,6 +22759,7 @@
hit(source);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
thisArg.xhrData = xhrData;
}
if (thisArg.shouldBePrevented) {
thisArg.collectedHeaders = [];
Expand Down Expand Up @@ -22797,7 +22811,7 @@
writable: false
},
responseURL: {
value: responseURL || xhrData.url,
value: responseURL || thisArg.xhrData.url,
writable: false
},
responseXML: {
Expand Down Expand Up @@ -22827,7 +22841,7 @@
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
nativeOpen.apply(forgedRequest, [thisArg.xhrData.method, thisArg.xhrData.url]);
thisArg.collectedHeaders.forEach(function (header) {
var name = header[0];
var value = header[1];
Expand All @@ -22841,6 +22855,9 @@
return undefined;
};
var getHeaderWrapper = function getHeaderWrapper(target, thisArg, args) {
if (!thisArg.shouldBePrevented) {
return nativeGetResponseHeader.apply(thisArg, args);
}
if (!thisArg.collectedHeaders.length) {
return null;
}
Expand All @@ -22852,6 +22869,9 @@
return matchedHeader ? matchedHeader[1] : null;
};
var getAllHeadersWrapper = function getAllHeadersWrapper(target, thisArg) {
if (!thisArg.shouldBePrevented) {
return nativeGetAllResponseHeaders.call(thisArg);
}
if (!thisArg.collectedHeaders.length) {
return "";
}
Expand Down
30 changes: 25 additions & 5 deletions dist/umd/scriptlets.umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* AdGuard Scriptlets
* Version 1.9.61
* Version 1.9.62
*/

(function (factory) {
Expand Down Expand Up @@ -6888,6 +6888,8 @@
}
var nativeOpen = window.XMLHttpRequest.prototype.open;
var nativeSend = window.XMLHttpRequest.prototype.send;
var nativeGetResponseHeader = window.XMLHttpRequest.prototype.getResponseHeader;
var nativeGetAllResponseHeaders = window.XMLHttpRequest.prototype.getAllResponseHeaders;
var xhrData;
var modifiedResponse = '';
var modifiedResponseText = '';
Expand All @@ -6901,6 +6903,9 @@
hit(source);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
// Add xhrData to thisArg to keep original values in case of multiple requests
// https://github.com/AdguardTeam/Scriptlets/issues/347
thisArg.xhrData = xhrData;
}

// Trap setRequestHeader of target xhr object to mimic request headers later;
Expand Down Expand Up @@ -6968,7 +6973,7 @@
},
// If the request is blocked, responseURL is an empty string
responseURL: {
value: responseURL || xhrData.url,
value: responseURL || thisArg.xhrData.url,
writable: false
},
responseXML: {
Expand Down Expand Up @@ -7001,7 +7006,7 @@
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
nativeOpen.apply(forgedRequest, [thisArg.xhrData.method, thisArg.xhrData.url]);

// Mimic request headers before sending
// setRequestHeader can only be called on open request objects
Expand All @@ -7028,6 +7033,9 @@
* @returns {string|null} Header value or null if header is not set.
*/
var getHeaderWrapper = function getHeaderWrapper(target, thisArg, args) {
if (!thisArg.shouldBePrevented) {
return nativeGetResponseHeader.apply(thisArg, args);
}
if (!thisArg.collectedHeaders.length) {
return null;
}
Expand All @@ -7050,6 +7058,9 @@
* @returns {string} All headers as a string. For no headers an empty string is returned.
*/
var getAllHeadersWrapper = function getAllHeadersWrapper(target, thisArg) {
if (!thisArg.shouldBePrevented) {
return nativeGetAllResponseHeaders.call(thisArg);
}
if (!thisArg.collectedHeaders.length) {
return '';
}
Expand Down Expand Up @@ -22738,6 +22749,8 @@
}
var nativeOpen = window.XMLHttpRequest.prototype.open;
var nativeSend = window.XMLHttpRequest.prototype.send;
var nativeGetResponseHeader = window.XMLHttpRequest.prototype.getResponseHeader;
var nativeGetAllResponseHeaders = window.XMLHttpRequest.prototype.getAllResponseHeaders;
var xhrData;
var modifiedResponse = "";
var modifiedResponseText = "";
Expand All @@ -22748,6 +22761,7 @@
hit(source);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
thisArg.xhrData = xhrData;
}
if (thisArg.shouldBePrevented) {
thisArg.collectedHeaders = [];
Expand Down Expand Up @@ -22799,7 +22813,7 @@
writable: false
},
responseURL: {
value: responseURL || xhrData.url,
value: responseURL || thisArg.xhrData.url,
writable: false
},
responseXML: {
Expand Down Expand Up @@ -22829,7 +22843,7 @@
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
nativeOpen.apply(forgedRequest, [thisArg.xhrData.method, thisArg.xhrData.url]);
thisArg.collectedHeaders.forEach(function (header) {
var name = header[0];
var value = header[1];
Expand All @@ -22843,6 +22857,9 @@
return undefined;
};
var getHeaderWrapper = function getHeaderWrapper(target, thisArg, args) {
if (!thisArg.shouldBePrevented) {
return nativeGetResponseHeader.apply(thisArg, args);
}
if (!thisArg.collectedHeaders.length) {
return null;
}
Expand All @@ -22854,6 +22871,9 @@
return matchedHeader ? matchedHeader[1] : null;
};
var getAllHeadersWrapper = function getAllHeadersWrapper(target, thisArg) {
if (!thisArg.shouldBePrevented) {
return nativeGetAllResponseHeaders.call(thisArg);
}
if (!thisArg.collectedHeaders.length) {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/scriptlets",
"version": "1.9.61",
"version": "1.9.62",
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
"scripts": {
"build": "babel-node -x .js,.ts scripts/build.js",
Expand Down
15 changes: 13 additions & 2 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export function preventXHR(source, propsToMatch, customResponseText) {

const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;
const nativeGetResponseHeader = window.XMLHttpRequest.prototype.getResponseHeader;
const nativeGetAllResponseHeaders = window.XMLHttpRequest.prototype.getAllResponseHeaders;

let xhrData;
let modifiedResponse = '';
Expand All @@ -128,6 +130,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
hit(source);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
// Add xhrData to thisArg to keep original values in case of multiple requests
// https://github.com/AdguardTeam/Scriptlets/issues/347
thisArg.xhrData = xhrData;
}

// Trap setRequestHeader of target xhr object to mimic request headers later;
Expand Down Expand Up @@ -194,7 +199,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
readyState: { value: readyState, writable: false },
statusText: { value: statusText, writable: false },
// If the request is blocked, responseURL is an empty string
responseURL: { value: responseURL || xhrData.url, writable: false },
responseURL: { value: responseURL || thisArg.xhrData.url, writable: false },
responseXML: { value: responseXML, writable: false },
// modified values
status: { value: 200, writable: false },
Expand All @@ -217,7 +222,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
hit(source);
});

nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
nativeOpen.apply(forgedRequest, [thisArg.xhrData.method, thisArg.xhrData.url]);

// Mimic request headers before sending
// setRequestHeader can only be called on open request objects
Expand Down Expand Up @@ -246,6 +251,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
* @returns {string|null} Header value or null if header is not set.
*/
const getHeaderWrapper = (target, thisArg, args) => {
if (!thisArg.shouldBePrevented) {
return nativeGetResponseHeader.apply(thisArg, args);
}
if (!thisArg.collectedHeaders.length) {
return null;
}
Expand All @@ -270,6 +278,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
* @returns {string} All headers as a string. For no headers an empty string is returned.
*/
const getAllHeadersWrapper = (target, thisArg) => {
if (!thisArg.shouldBePrevented) {
return nativeGetAllResponseHeaders.call(thisArg);
}
if (!thisArg.collectedHeaders.length) {
return '';
}
Expand Down
Loading

0 comments on commit 0e3b35c

Please sign in to comment.