diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cc629329..fb15af68b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] @@ -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 diff --git a/src/helpers/storage-utils.ts b/src/helpers/storage-utils.ts index 017538d71..d30cdd0cb 100644 --- a/src/helpers/storage-utils.ts +++ b/src/helpers/storage-utils.ts @@ -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) => { @@ -70,6 +72,10 @@ export const getLimitedStorageItemValue = (value: string): StorageItemValue | nu 'no', 'on', 'off', + 'accept', + 'accepted', + 'reject', + 'rejected', ]); let validValue; diff --git a/src/scriptlets/set-local-storage-item.js b/src/scriptlets/set-local-storage-item.js index b788cfca8..8ac45217d 100644 --- a/src/scriptlets/set-local-storage-item.js +++ b/src/scriptlets/set-local-storage-item.js @@ -46,6 +46,10 @@ import { * - `no` * - `on` * - `off` + * - `accept` + * - `accepted` + * - `reject` + * - `rejected` * - `$remove$` — remove specific item from localStorage * * ### Examples diff --git a/src/scriptlets/set-session-storage-item.js b/src/scriptlets/set-session-storage-item.js index 1c6f65892..546ee79cf 100644 --- a/src/scriptlets/set-session-storage-item.js +++ b/src/scriptlets/set-session-storage-item.js @@ -46,6 +46,10 @@ import { * - `no` * - `on` * - `off` + * - `accept` + * - `accepted` + * - `reject` + * - `rejected` * - `$remove$` — remove specific item from sessionStorage * * ### Examples diff --git a/tests/scriptlets/set-local-storage-item.test.js b/tests/scriptlets/set-local-storage-item.test.js index 151e1b594..cd2d797bb 100644 --- a/tests/scriptlets/set-local-storage-item.test.js +++ b/tests/scriptlets/set-local-storage-item.test.js @@ -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) => { diff --git a/tests/scriptlets/set-session-storage-item.test.js b/tests/scriptlets/set-session-storage-item.test.js index 244de4f45..48d172644 100644 --- a/tests/scriptlets/set-session-storage-item.test.js +++ b/tests/scriptlets/set-session-storage-item.test.js @@ -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) => {