mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
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:
parent
2e416f51b7
commit
60e15cb6e1
1 changed files with 10 additions and 5 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue