1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

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
This commit is contained in:
Raymond Hill 2023-10-20 20:10:35 -04:00
parent f0cd93335e
commit 41d91ed080
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -106,10 +106,7 @@ function safeSelf() {
const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern); const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern);
if ( match === null ) { if ( match === null ) {
const reStr = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const reStr = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
if ( verbatim ) { return new RegExp(verbatim ? `^${reStr}$` : reStr, flags);
return new RegExp(`^${reStr}$`, flags);
}
return new RegExp(reStr, flags);
} }
try { try {
return new RegExp(match[1], match[2] || flags); return new RegExp(match[1], match[2] || flags);
@ -784,10 +781,13 @@ function setCookieHelper(
/******************************************************************************/ /******************************************************************************/
builtinScriptlets.push({ builtinScriptlets.push({
name: 'set-local-storage-item-core.fn', name: 'set-local-storage-item.fn',
fn: setLocalStorageItemCore, fn: setLocalStorageItemFn,
dependencies: [
'safe-self.fn',
],
}); });
function setLocalStorageItemCore( function setLocalStorageItemFn(
which = 'local', which = 'local',
trusted = false, trusted = false,
key = '', key = '',
@ -799,6 +799,7 @@ function setLocalStorageItemCore(
'', '',
'undefined', 'null', 'undefined', 'null',
'false', 'true', 'false', 'true',
'on', 'off',
'yes', 'no', 'yes', 'no',
'{}', '[]', '""', '{}', '[]', '""',
'$remove$', '$remove$',
@ -821,11 +822,20 @@ function setLocalStorageItemCore(
} }
try { try {
const storage = `${which}Storage`; const storage = self[`${which}Storage`];
if ( value === '$remove$' ) { 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 { } else {
self[storage].setItem(key, `${value}`); storage.setItem(key, `${value}`);
} }
} catch(ex) { } catch(ex) {
} }
@ -3426,11 +3436,11 @@ builtinScriptlets.push({
fn: setLocalStorageItem, fn: setLocalStorageItem,
world: 'ISOLATED', world: 'ISOLATED',
dependencies: [ dependencies: [
'set-local-storage-item-core.fn', 'set-local-storage-item.fn',
], ],
}); });
function setLocalStorageItem(key = '', value = '') { function setLocalStorageItem(key = '', value = '') {
setLocalStorageItemCore('local', false, key, value); setLocalStorageItemFn('local', false, key, value);
} }
builtinScriptlets.push({ builtinScriptlets.push({
@ -3438,11 +3448,11 @@ builtinScriptlets.push({
fn: setSessionStorageItem, fn: setSessionStorageItem,
world: 'ISOLATED', world: 'ISOLATED',
dependencies: [ dependencies: [
'set-local-storage-item-core.fn', 'set-local-storage-item.fn',
], ],
}); });
function setSessionStorageItem(key = '', value = '') { function setSessionStorageItem(key = '', value = '') {
setLocalStorageItemCore('session', false, key, value); setLocalStorageItemFn('session', false, key, value);
} }
/******************************************************************************* /*******************************************************************************
@ -3817,11 +3827,11 @@ builtinScriptlets.push({
fn: trustedSetLocalStorageItem, fn: trustedSetLocalStorageItem,
world: 'ISOLATED', world: 'ISOLATED',
dependencies: [ dependencies: [
'set-local-storage-item-core.fn', 'set-local-storage-item.fn',
], ],
}); });
function trustedSetLocalStorageItem(key = '', value = '') { function trustedSetLocalStorageItem(key = '', value = '') {
setLocalStorageItemCore('local', true, key, value); setLocalStorageItemFn('local', true, key, value);
} }
/******************************************************************************* /*******************************************************************************