Skip to content

Commit

Permalink
Add support to remove values in set-local-storage-item scriptlet
Browse files Browse the repository at this point in the history
Related feedback:
- uBlockOrigin/uBlock-issues#2697 (comment)

When using the special value `$remove$`, the scriptlet will remove
the item from the local storage.
  • Loading branch information
gorhill committed Jun 30, 2023
1 parent d54fad2 commit b283d6a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions assets/resources/scriptlets.js
Expand Up @@ -2838,7 +2838,8 @@ function setLocalStorageItem(
'undefined', 'null',
'false', 'true',
'yes', 'no',
'{}', '[]', "''",
'{}', '[]', '""',
'$remove$',
];
let actualValue;
if ( validValues.includes(value) ) {
Expand All @@ -2851,10 +2852,10 @@ function setLocalStorageItem(
}

try {
if ( actualValue !== undefined ) {
self.localStorage.setItem(key, `${actualValue}`);
} else {
if ( actualValue === '$remove$' ) {
self.localStorage.removeItem(key);
} else {
self.localStorage.setItem(key, `${actualValue}`);
}
} catch(ex) {
}
Expand Down Expand Up @@ -3024,7 +3025,6 @@ function trustedSetLocalStorageItem(
value = ''
) {
if ( key === '' ) { return; }
if ( value === '' ) { return; }

let actualValue = value;
if ( value === '$now$' ) {
Expand All @@ -3034,7 +3034,11 @@ function trustedSetLocalStorageItem(
}

try {
self.localStorage.setItem(key, `${actualValue}`);
if ( actualValue === '$remove$' ) {
self.localStorage.removeItem(key);
} else {
self.localStorage.setItem(key, `${actualValue}`);
}
} catch(ex) {
}
}
Expand Down

0 comments on commit b283d6a

Please sign in to comment.