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

View file

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