From 41d91ed0809db8fe2208b7811c9dc7d11f06f889 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 20 Oct 2023 20:10:35 -0400 Subject: [PATCH] Fine tune `set-local-storage-item` as per feedback Related feedback: - https://github.com/uBlockOrigin/uAssets/discussions/20194 - https://github.com/gorhill/uBlock/pull/3898 --- assets/resources/scriptlets.js | 42 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 1c2e63afd..0ea0163c1 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -106,10 +106,7 @@ function safeSelf() { const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern); if ( match === null ) { const reStr = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - if ( verbatim ) { - return new RegExp(`^${reStr}$`, flags); - } - return new RegExp(reStr, flags); + return new RegExp(verbatim ? `^${reStr}$` : reStr, flags); } try { return new RegExp(match[1], match[2] || flags); @@ -784,10 +781,13 @@ function setCookieHelper( /******************************************************************************/ builtinScriptlets.push({ - name: 'set-local-storage-item-core.fn', - fn: setLocalStorageItemCore, + name: 'set-local-storage-item.fn', + fn: setLocalStorageItemFn, + dependencies: [ + 'safe-self.fn', + ], }); -function setLocalStorageItemCore( +function setLocalStorageItemFn( which = 'local', trusted = false, key = '', @@ -799,6 +799,7 @@ function setLocalStorageItemCore( '', 'undefined', 'null', 'false', 'true', + 'on', 'off', 'yes', 'no', '{}', '[]', '""', '$remove$', @@ -821,11 +822,20 @@ function setLocalStorageItemCore( } try { - const storage = `${which}Storage`; + const storage = self[`${which}Storage`]; if ( value === '$remove$' ) { - self[storage].removeItem(key); + const safe = safeSelf(); + const pattern = safe.patternToRegex(key, undefined, true ); + const toRemove = []; + for ( let i = 0, n = storage.length; i < n; i++ ) { + const key = storage.key(i); + if ( pattern.test(key) ) { toRemove.push(key); } + } + for ( const key of toRemove ) { + storage.removeItem(key); + } } else { - self[storage].setItem(key, `${value}`); + storage.setItem(key, `${value}`); } } catch(ex) { } @@ -3426,11 +3436,11 @@ builtinScriptlets.push({ fn: setLocalStorageItem, world: 'ISOLATED', dependencies: [ - 'set-local-storage-item-core.fn', + 'set-local-storage-item.fn', ], }); function setLocalStorageItem(key = '', value = '') { - setLocalStorageItemCore('local', false, key, value); + setLocalStorageItemFn('local', false, key, value); } builtinScriptlets.push({ @@ -3438,11 +3448,11 @@ builtinScriptlets.push({ fn: setSessionStorageItem, world: 'ISOLATED', dependencies: [ - 'set-local-storage-item-core.fn', + 'set-local-storage-item.fn', ], }); function setSessionStorageItem(key = '', value = '') { - setLocalStorageItemCore('session', false, key, value); + setLocalStorageItemFn('session', false, key, value); } /******************************************************************************* @@ -3817,11 +3827,11 @@ builtinScriptlets.push({ fn: trustedSetLocalStorageItem, world: 'ISOLATED', dependencies: [ - 'set-local-storage-item-core.fn', + 'set-local-storage-item.fn', ], }); function trustedSetLocalStorageItem(key = '', value = '') { - setLocalStorageItemCore('local', true, key, value); + setLocalStorageItemFn('local', true, key, value); } /*******************************************************************************