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

Add remove-cache-storage-item scriptlet

Usage:

...##+js(remove-cache-storage-item, cacheNamePattern[, urlPattern])

`cacheNamePattern`: the name of the cache to target. Plain string
  or regex.

`urlPattern`: the URL of the resource to remove. Plain string
  or regex. If no pattern is provided, the whole cache is removed.

Reference:
https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage
This commit is contained in:
Raymond Hill 2024-01-27 18:17:28 -05:00
parent fa162e2a31
commit 6c54731a72
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -1678,7 +1678,7 @@ function jsonPrune(
if ( objAfter === undefined ) { return objBefore; } if ( objAfter === undefined ) { return objBefore; }
safe.uboLog(logPrefix, 'Pruned'); safe.uboLog(logPrefix, 'Pruned');
if ( safe.logLevel > 1 ) { if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, JSON.stringify(objAfter, null, 1)); safe.uboLog(logPrefix, `After pruning:\n${JSON.stringify(objAfter, null, 1)}`);
} }
return objAfter; return objAfter;
}, },
@ -3941,6 +3941,58 @@ function multiup() {
document.addEventListener('click', handler, { capture: true }); document.addEventListener('click', handler, { capture: true });
} }
/******************************************************************************/
builtinScriptlets.push({
name: 'remove-cache-storage-item.js',
fn: removeCacheStorageItem,
world: 'ISOLATED',
dependencies: [
'safe-self.fn',
],
});
function removeCacheStorageItem(
cacheNamePattern = '',
requestPattern = ''
) {
if ( cacheNamePattern === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('remove-cache-storage-item', cacheNamePattern, requestPattern);
const cacheStorage = self.caches;
if ( cacheStorage instanceof Object === false ) { return; }
const reCache = safe.patternToRegex(cacheNamePattern, undefined, true);
const reRequest = safe.patternToRegex(requestPattern, undefined, true);
cacheStorage.keys().then(cacheNames => {
for ( const cacheName of cacheNames ) {
if ( reCache.test(cacheName) === false ) { continue; }
if ( requestPattern === '' ) {
cacheStorage.delete(cacheName).then(result => {
if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Deleting ${cacheName}`);
}
if ( result !== true ) { return; }
safe.uboLog(logPrefix, `Deleted ${cacheName}: ${result}`);
});
continue;
}
cacheStorage.open(cacheName).then(cache => {
cache.keys().then(requests => {
for ( const request of requests ) {
if ( reRequest.test(request.url) === false ) { continue; }
if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Deleting ${cacheName}/${request.url}`);
}
cache.delete(request).then(result => {
if ( result !== true ) { return; }
safe.uboLog(logPrefix, `Deleted ${cacheName}/${request.url}: ${result}`);
});
}
});
});
}
});
}
/******************************************************************************* /*******************************************************************************
* *