Improve prevent-fetch scriptlet

Related issue:
https://github.com/AdguardTeam/AdguardFilters/issues/153796
This commit is contained in:
Raymond Hill 2024-01-01 10:24:47 -05:00
parent 00fb5f18b2
commit e1ae17ed00
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -274,6 +274,15 @@ function generateContentFn(directive) {
if ( directive === 'true' ) {
return Promise.resolve(randomize(10));
}
if ( directive === 'emptyObj' ) {
return Promise.resolve('{}');
}
if ( directive === 'emptyArr' ) {
return Promise.resolve('[]');
}
if ( directive === 'emptyStr' ) {
return Promise.resolve('');
}
if ( directive.startsWith('length:') ) {
const match = /^length:(\d+)(?:-(\d+))?$/.exec(directive);
if ( match ) {
@ -1829,7 +1838,7 @@ builtinScriptlets.push({
});
function noFetchIf(
propsToMatch = '',
directive = ''
responseBody = ''
) {
if ( typeof propsToMatch !== 'string' ) { return; }
const safe = safeSelf();
@ -1886,7 +1895,17 @@ function noFetchIf(
if ( proceed ) {
return Reflect.apply(target, thisArg, args);
}
return generateContentFn(directive).then(text => {
let responseType = '';
if ( details.mode === undefined || details.mode === 'cors' ) {
try {
const desURL = new URL(details.url);
responseType = desURL.origin !== document.location.origin
? 'cors'
: 'basic';
} catch(_) {
}
}
return generateContentFn(responseBody).then(text => {
const response = new Response(text, {
statusText: 'OK',
headers: {
@ -1896,6 +1915,11 @@ function noFetchIf(
Object.defineProperty(response, 'url', {
value: details.url
});
if ( responseType !== '' ) {
Object.defineProperty(response, 'type', {
value: responseType
});
}
return response;
});
}