From 6c54731a728b06f89a66ddc851ebae3e19e45448 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 27 Jan 2024 18:17:28 -0500 Subject: [PATCH] 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 --- assets/resources/scriptlets.js | 54 +++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 2da1fa2772fb4..d29fda5cd5458 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -1678,7 +1678,7 @@ function jsonPrune( if ( objAfter === undefined ) { return objBefore; } safe.uboLog(logPrefix, 'Pruned'); 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; }, @@ -3941,6 +3941,58 @@ function multiup() { 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}`); + }); + } + }); + }); + } + }); +} + /******************************************************************************* *