Improve `prevent-fetch` scriptlet

Add `statusText` as overridable property in response instance.

Supported values are: `""`, `"Not Found"`. `statusText` defaults
to `"OK"` when not overridden.
This commit is contained in:
Raymond Hill 2024-07-14 12:15:53 -04:00
parent 324102cb65
commit 9ce3056361
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 6 additions and 5 deletions

View File

@ -2089,11 +2089,13 @@ function noFetchIf(
} }
const validResponseProps = { const validResponseProps = {
ok: [ false, true ], ok: [ false, true ],
type: [ 'basic', 'cors', 'opaque' ], statusText: [ '', 'Not Found' ],
type: [ 'basic', 'cors', 'default', 'error', 'opaque' ],
};
const responseProps = {
statusText: { value: 'OK' },
}; };
let responseProps;
if ( /^\{.*\}$/.test(responseType) ) { if ( /^\{.*\}$/.test(responseType) ) {
responseProps = {};
try { try {
Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => { Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => {
if ( validResponseProps[p] === undefined ) { return; } if ( validResponseProps[p] === undefined ) { return; }
@ -2104,7 +2106,7 @@ function noFetchIf(
catch(ex) {} catch(ex) {}
} else if ( responseType !== '' ) { } else if ( responseType !== '' ) {
if ( validResponseProps.type.includes(responseType) ) { if ( validResponseProps.type.includes(responseType) ) {
responseProps = { type: { value: responseType } }; responseProps.type = { value: responseType };
} }
} }
self.fetch = new Proxy(self.fetch, { self.fetch = new Proxy(self.fetch, {
@ -2147,7 +2149,6 @@ function noFetchIf(
return generateContentFn(responseBody).then(text => { return generateContentFn(responseBody).then(text => {
safe.uboLog(logPrefix, `Prevented with response "${text}"`); safe.uboLog(logPrefix, `Prevented with response "${text}"`);
const response = new Response(text, { const response = new Response(text, {
statusText: 'OK',
headers: { headers: {
'Content-Length': text.length, 'Content-Length': text.length,
} }