1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 15:33:38 +01:00

Further improve prevent-fetch scriptlet

This commit is contained in:
Raymond Hill 2024-09-05 11:17:36 -04:00
parent 1dc09b6217
commit 60a009c530
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -2129,6 +2129,7 @@ builtinScriptlets.push({
fn: noFetchIf, fn: noFetchIf,
dependencies: [ dependencies: [
'generate-content.fn', 'generate-content.fn',
'proxy-apply.fn',
'safe-self.fn', 'safe-self.fn',
], ],
}); });
@ -2175,61 +2176,58 @@ function noFetchIf(
responseProps.type = { value: responseType }; responseProps.type = { value: responseType };
} }
} }
self.fetch = new Proxy(self.fetch, { proxyApplyFn('fetch', function fetch(target, thisArg, args) {
apply: function(target, thisArg, args) { const details = args[0] instanceof self.Request
const details = args[0] instanceof self.Request ? args[0]
? args[0] : Object.assign({ url: args[0] }, args[1]);
: Object.assign({ url: args[0] }, args[1]); let proceed = true;
if ( safe.logLevel > 1 ) { try {
safe.uboLog(logPrefix, `apply:\n\t${Object.entries(details).map(a => `${a[0]}: ${a[1]}`).join('\n\t')}`); const props = new Map();
for ( const prop in details ) {
let v = details[prop];
if ( typeof v !== 'string' ) {
try { v = safe.JSON_stringify(v); }
catch(ex) { }
}
if ( typeof v !== 'string' ) { continue; }
props.set(prop, v);
} }
let proceed = true; if ( safe.logLevel > 1 || propsToMatch === '' && responseBody === '' ) {
try { const out = Array.from(props).map(a => `${a[0]}:${a[1]}`);
const props = new Map(); safe.uboLog(logPrefix, `Called: ${out.join('\n')}`);
for ( const prop in details ) {
let v = details[prop];
if ( typeof v !== 'string' ) {
try { v = safe.JSON_stringify(v); }
catch(ex) { }
}
if ( typeof v !== 'string' ) { continue; }
props.set(prop, v);
}
if ( propsToMatch === '' && responseBody === '' ) {
const out = Array.from(props).map(a => `${a[0]}:${a[1]}`);
safe.uboLog(logPrefix, `Called: ${out.join('\n')}`);
return Reflect.apply(target, thisArg, args);
}
proceed = needles.length === 0;
for ( const { key, pattern } of needles ) {
if (
pattern.expect && props.has(key) === false ||
safe.testPattern(pattern, props.get(key)) === false
) {
proceed = true;
break;
}
}
} catch(ex) {
} }
if ( proceed ) { if ( propsToMatch === '' && responseBody === '' ) {
return Reflect.apply(target, thisArg, args); return Reflect.apply(target, thisArg, args);
} }
return generateContentFn(responseBody).then(text => { proceed = needles.length === 0;
safe.uboLog(logPrefix, `Prevented with response "${text}"`); for ( const { key, pattern } of needles ) {
const response = new Response(text, { if (
headers: { pattern.expect && props.has(key) === false ||
'Content-Length': text.length, safe.testPattern(pattern, props.get(key)) === false
} ) {
}); proceed = true;
const props = Object.assign( break;
{ url: { value: details.url } }, }
responseProps }
); } catch(ex) {
safe.Object_defineProperties(response, props);
return response;
});
} }
if ( proceed ) {
return Reflect.apply(target, thisArg, args);
}
return generateContentFn(responseBody).then(text => {
safe.uboLog(logPrefix, `Prevented with response "${text}"`);
const response = new Response(text, {
headers: {
'Content-Length': text.length,
}
});
const props = Object.assign(
{ url: { value: details.url } },
responseProps
);
safe.Object_defineProperties(response, props);
return response;
});
}); });
} }