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
1 changed files with 26 additions and 16 deletions

View File

@ -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);
}
/*******************************************************************************