Skip to content

Commit

Permalink
Add new values to set-local-storage-item and `set-session-storage-i…
Browse files Browse the repository at this point in the history
…tem` scriptlets: `accept`, `accepted`, `reject`, `rejected`
  • Loading branch information
AdamWr committed Jul 5, 2024
1 parent b17b619 commit 13256c0
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,10 +11,13 @@ 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]

[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

## [v1.11.1] - 2024-06-13
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 13256c0

Please sign in to comment.