mirror of https://github.com/gorhill/uBlock.git
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:
parent
324102cb65
commit
9ce3056361
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue