Skip to content

Commit e1ae17e

Browse files
committed
Improve prevent-fetch scriptlet
Related issue: AdguardTeam/AdguardFilters#153796
1 parent 00fb5f1 commit e1ae17e

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

assets/resources/scriptlets.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ function generateContentFn(directive) {
274274
if ( directive === 'true' ) {
275275
return Promise.resolve(randomize(10));
276276
}
277+
if ( directive === 'emptyObj' ) {
278+
return Promise.resolve('{}');
279+
}
280+
if ( directive === 'emptyArr' ) {
281+
return Promise.resolve('[]');
282+
}
283+
if ( directive === 'emptyStr' ) {
284+
return Promise.resolve('');
285+
}
277286
if ( directive.startsWith('length:') ) {
278287
const match = /^length:(\d+)(?:-(\d+))?$/.exec(directive);
279288
if ( match ) {
@@ -1829,7 +1838,7 @@ builtinScriptlets.push({
18291838
});
18301839
function noFetchIf(
18311840
propsToMatch = '',
1832-
directive = ''
1841+
responseBody = ''
18331842
) {
18341843
if ( typeof propsToMatch !== 'string' ) { return; }
18351844
const safe = safeSelf();
@@ -1886,7 +1895,17 @@ function noFetchIf(
18861895
if ( proceed ) {
18871896
return Reflect.apply(target, thisArg, args);
18881897
}
1889-
return generateContentFn(directive).then(text => {
1898+
let responseType = '';
1899+
if ( details.mode === undefined || details.mode === 'cors' ) {
1900+
try {
1901+
const desURL = new URL(details.url);
1902+
responseType = desURL.origin !== document.location.origin
1903+
? 'cors'
1904+
: 'basic';
1905+
} catch(_) {
1906+
}
1907+
}
1908+
return generateContentFn(responseBody).then(text => {
18901909
const response = new Response(text, {
18911910
statusText: 'OK',
18921911
headers: {
@@ -1896,6 +1915,11 @@ function noFetchIf(
18961915
Object.defineProperty(response, 'url', {
18971916
value: details.url
18981917
});
1918+
if ( responseType !== '' ) {
1919+
Object.defineProperty(response, 'type', {
1920+
value: responseType
1921+
});
1922+
}
18991923
return response;
19001924
});
19011925
}

0 commit comments

Comments
 (0)