From e1ae17ed0059f5be488f2d6b7617feeda63e9096 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 1 Jan 2024 10:24:47 -0500 Subject: [PATCH] Improve `prevent-fetch` scriptlet Related issue: https://github.com/AdguardTeam/AdguardFilters/issues/153796 --- assets/resources/scriptlets.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 2015b1b2f..6e2654fc1 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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; }); }