Fix/improve `xml-prune` scriptlet

Related issue:
https://github.com/uBlockOrigin/uAssets/issues/14849#issuecomment-1257094491
This commit is contained in:
Raymond Hill 2023-08-20 08:36:16 -04:00
parent b1f0c5b773
commit 4b83101ab9
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 21 additions and 10 deletions

View File

@ -2580,18 +2580,29 @@ function xmlPrune(
}; };
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);
if ( reUrl.test(urlFromArg(args[0])) === false ) { if ( reUrl.test(urlFromArg(args[0])) === false ) {
return Reflect.apply(target, thisArg, args); return fetchPromise;
} }
return safe.fetch(...args).then(realResponse => return fetchPromise.then(responseBefore => {
realResponse.text().then(text => const response = responseBefore.clone();
new Response(pruneFromText(text), { return response.text().then(text => {
status: realResponse.status, const responseAfter = new Response(pruneFromText(text), {
statusText: realResponse.statusText, status: responseBefore.status,
headers: realResponse.headers, statusText: responseBefore.statusText,
}) headers: responseBefore.headers,
) });
); Object.defineProperties(responseAfter, {
ok: { value: responseBefore.ok },
redirected: { value: responseBefore.redirected },
type: { value: responseBefore.type },
url: { value: responseBefore.url },
});
return responseAfter;
}).catch(( ) =>
responseBefore
);
});
} }
}); });
self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, { self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, {