Skip to content

Commit

Permalink
Fine tune set-local-storage-item as per feedback
Browse files Browse the repository at this point in the history
Related feedback:
- uBlockOrigin/uAssets#20194
- #3898
  • Loading branch information
gorhill committed Oct 21, 2023
1 parent f0cd933 commit 41d91ed
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = '',
Expand All @@ -799,6 +799,7 @@ function setLocalStorageItemCore(
'',
'undefined', 'null',
'false', 'true',
'on', 'off',
'yes', 'no',
'{}', '[]', '""',
'$remove$',
Expand All @@ -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) {
}
Expand Down Expand Up @@ -3426,23 +3436,23 @@ 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({
name: 'set-session-storage-item.js',
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);
}

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

/*******************************************************************************
Expand Down

1 comment on commit 41d91ed

@peace2000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill My PR was about adding "on" and "off" values to set-cookie scriptlet, not to set local storage item.

Though, it doesn't hurt if those values are also accepted in set local storage item :)

Please sign in to comment.