Improve prevent-fetch scriptlet

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/3828

Ability to set header values for the trusted version of the
scriptlet. Example:

..##+js(trusted-prevent-fetch, target, length:100, '{"headers":{"content-type": "image/png"}}')
This commit is contained in:
Raymond Hill 2025-10-24 12:26:37 -04:00
parent 2e416f51b7
commit 60e15cb6e1
No known key found for this signature in database
GPG key ID: F5630CAE62A14316

View file

@ -65,9 +65,14 @@ function preventFetchFn(
const responseProps = {
statusText: { value: 'OK' },
};
const responseHeaders = {};
if ( /^\{.*\}$/.test(responseType) ) {
try {
Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => {
if ( p === 'headers' && trusted ) {
Object.assign(responseHeaders, v);
return;
}
if ( validResponseProps[p] === undefined ) { return; }
if ( validResponseProps[p].includes(v) === false ) { return; }
responseProps[p] = { value: v };
@ -120,11 +125,11 @@ function preventFetchFn(
}
return Promise.resolve(generateContentFn(trusted, responseBody)).then(text => {
safe.uboLog(logPrefix, `Prevented with response "${text}"`);
const response = new Response(text, {
headers: {
'Content-Length': text.length,
}
});
const headers = Object.assign({}, responseHeaders);
if ( headers['content-length'] === undefined ) {
headers['content-length'] = text.length;
}
const response = new Response(text, { headers });
const props = Object.assign(
{ url: { value: details.url } },
responseProps