Skip to content

Commit

Permalink
AG-33049 Improve 'set-local-storage-item' — add more supported values.
Browse files Browse the repository at this point in the history
…#429

Squashed commit of the following:

commit 123ff4e
Merge: 13256c0 69ec2e4
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Jul 8 12:20:58 2024 +0200

    Merge branch 'master' into fix/AG-33049

commit 13256c0
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Jul 5 11:48:50 2024 +0200

    Add new values to `set-local-storage-item` and `set-session-storage-item` scriptlets: `accept`, `accepted`, `reject`, `rejected`
  • Loading branch information
AdamWr committed Jul 8, 2024
1 parent 69ec2e4 commit c1c392b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

### Added

- new values to `set-local-storage-item` and `set-session-storage-item` scriptlets:
`accept`, `accepted`, `reject`, `rejected` [#429]
- ability to log original and modified content in `trusted-replace-node-text`, `xml-prune`, `m3u-prune`,
`trusted-replace-fetch-response` and `trusted-replace-xhr-response` scriptlets [#411]

Expand All @@ -19,6 +21,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
- Log message format [CoreLibs#180]

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.11.1...HEAD
[#429]: https://github.com/AdguardTeam/Scriptlets/issues/429
[#411]: https://github.com/AdguardTeam/Scriptlets/issues/411
[CoreLibs#180]: https://github.com/AdguardTeam/CoreLibs/issues/180

Expand Down
12 changes: 9 additions & 3 deletions src/helpers/storage-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export const setStorageItem = (source: Source, storage: Storage, key: string, va
*/
export const removeStorageItem = (source: Source, storage: Storage, key: string): void => {
try {
if (key.startsWith('/')
&& (key.endsWith('/') || key.endsWith('/i'))
&& isValidStrPattern(key)) {
if (
key.startsWith('/')
&& (key.endsWith('/') || key.endsWith('/i'))
&& isValidStrPattern(key)
) {
const regExpKey = toRegExp(key);
const storageKeys = Object.keys(storage);
storageKeys.forEach((storageKey) => {
Expand Down Expand Up @@ -70,6 +72,10 @@ export const getLimitedStorageItemValue = (value: string): StorageItemValue | nu
'no',
'on',
'off',
'accept',
'accepted',
'reject',
'rejected',
]);

let validValue;
Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import {
* - `no`
* - `on`
* - `off`
* - `accept`
* - `accepted`
* - `reject`
* - `rejected`
* - `$remove$` — remove specific item from localStorage
*
* ### Examples
Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/set-session-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import {
* - `no`
* - `on`
* - `off`
* - `accept`
* - `accepted`
* - `reject`
* - `rejected`
* - `$remove$` — remove specific item from sessionStorage
*
* ### Examples
Expand Down
28 changes: 28 additions & 0 deletions tests/scriptlets/set-local-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ if (isSafariBrowser()) {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'off', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_accept';
iValue = 'accept';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'accept', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_accepted';
iValue = 'accepted';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'accepted', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_reject';
iValue = 'reject';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'reject', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_rejected';
iValue = 'rejected';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'rejected', 'localStorage item has been set');
clearStorageItem(iName);
});

test('Set localStorage key with invalid value', (assert) => {
Expand Down
28 changes: 28 additions & 0 deletions tests/scriptlets/set-session-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ if (isSafariBrowser()) {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'off', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_accept';
cValue = 'accept';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'accept', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_accepted';
cValue = 'accepted';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'accepted', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_reject';
cValue = 'reject';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'reject', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_rejected';
cValue = 'rejected';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'rejected', 'sessionStorage item has been set');
clearStorageItem(cName);
});

test('Set sessionStorage key with invalid value', (assert) => {
Expand Down

0 comments on commit c1c392b

Please sign in to comment.