Skip to content

Commit

Permalink
Improve prevent-fetch scriptlet
Browse files Browse the repository at this point in the history
Add `statusText` as overridable property in response instance.

Supported values are: `""`, `"Not Found"`. `statusText` defaults
to `"OK"` when not overridden.
  • Loading branch information
gorhill committed Jul 14, 2024
1 parent 324102c commit 9ce3056
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2089,11 +2089,13 @@ function noFetchIf(
}
const validResponseProps = {
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) ) {
responseProps = {};
try {
Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => {
if ( validResponseProps[p] === undefined ) { return; }
Expand All @@ -2104,7 +2106,7 @@ function noFetchIf(
catch(ex) {}
} else if ( responseType !== '' ) {
if ( validResponseProps.type.includes(responseType) ) {
responseProps = { type: { value: responseType } };
responseProps.type = { value: responseType };
}
}
self.fetch = new Proxy(self.fetch, {
Expand Down Expand Up @@ -2147,7 +2149,6 @@ function noFetchIf(
return generateContentFn(responseBody).then(text => {
safe.uboLog(logPrefix, `Prevented with response "${text}"`);
const response = new Response(text, {
statusText: 'OK',
headers: {
'Content-Length': text.length,
}
Expand Down

0 comments on commit 9ce3056

Please sign in to comment.