Skip to content

Commit

Permalink
add trusted-set-local-storage-item scriptelt
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 7, 2022
1 parent 788f73a commit 8dd819d
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export * from './no-topics';
export * from './trusted-replace-xhr-response';
export * from './xml-prune';
export * from './trusted-replace-fetch-response';
export * from './trusted-set-local-storage-item';
71 changes: 71 additions & 0 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
hit,
nativeIsNaN,
} from '../helpers/index';

/* eslint-disable max-len */
/**
* @scriptlet trusted-set-local-storage-item
*
* @description
* Adds specified key and its value to localStorage object, or updates the value of the key if it already exists.
*
* **Syntax**
* ```
* example.com#%#//scriptlet('trusted-set-local-storage-item', 'key', 'value')
* ```
*
* - `key` — required, key name to be set.
* - `value` - required, key value; possible values:
* - positive decimal integer `<= 32767`
* - one of the predefined constants:
* - `undefined`
* - `false`
* - `true`
* - `null`
* - `emptyObj` - empty object
* - `emptyArr` - empty array
* - `''` - empty string
* - `yes`
* - `no`
*
* **Examples**
* ```
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.mute', 'false')
*
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'exit-intent-marketing', '1')
* ```
*/
/* eslint-enable max-len */

export function trustedSetLocalStorageItem(source, key, value) {
if (!key || (!value && value !== '')) {
return;
}

const setItem = (key, value) => {
const { localStorage } = window;
// setItem() may throw an exception if the storage is full.
try {
localStorage.setItem(key, value);
hit(source);
} catch (e) {
if (source.verbose) {
// eslint-disable-next-line no-console
console.log(`Was unable to set localStorage item due to: ${e.message}`);
}
}
};

setItem(key, value);
}

trustedSetLocalStorageItem.names = [
'trusted-set-local-storage-item',
// trusted scriptlets support no aliases
];

trustedSetLocalStorageItem.injections = [
hit,
nativeIsNaN,
];
1 change: 1 addition & 0 deletions tests/scriptlets/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ import './trusted-replace-xhr-response.test';
import './xml-prune.test';
import './trusted-click-element.test';
import './trusted-replace-fetch-response.test';
import './trusted-set-local-storage-item.test';
60 changes: 30 additions & 30 deletions tests/scriptlets/set-local-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,34 @@ if (isSafariBrowser()) {
assert.strictEqual(window.localStorage.getItem(cName), 'no', 'localStorage item has been set');
clearStorageItem(cName);
});
}

test('Set localStorage key with invalid value', (assert) => {
let cName = '__test-item_arrayItem';
let cValue = '["item"]';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_object';
cValue = '{"key":value"}';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_str';
cValue = 'test_string';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_bigInt';
cValue = '999999';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);
});
test('Set localStorage key with invalid value', (assert) => {
let cName = '__test-item_arrayItem';
let cValue = '["item"]';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_object';
cValue = '{"key":value"}';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_str';
cValue = 'test_string';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);

cName = '__test-item_bigInt';
cValue = '999999';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, undefined, 'Hit was not fired');
assert.strictEqual(window.localStorage.getItem(cName), null, 'localStorage item has not been set');
clearStorageItem(cName);
});
}
99 changes: 99 additions & 0 deletions tests/scriptlets/trusted-set-local-storage-item.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable no-underscore-dangle */
import { runScriptlet, clearGlobalProps, isSafariBrowser } from '../helpers';

const { test, module } = QUnit;
const name = 'trusted-set-local-storage-item';

const beforeEach = () => {
window.__debug = () => {
window.hit = 'FIRED';
};
};

const afterEach = () => {
clearGlobalProps('hit', '__debug');
};

module(name, { beforeEach, afterEach });

const clearStorageItem = (cName) => {
window.localStorage.removeItem(cName);
};

if (isSafariBrowser()) {
test('unsupported', (assert) => {
assert.ok(true, 'does not work in Safari 10 while browserstack auto tests run');
});
} else {
test('Set localStorage key with valid value', (assert) => {
let cName = '__test-item_true';
let cValue = 'true';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(cName), 'true', 'localStorage item has been set');
clearStorageItem(cName);

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

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

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

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

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

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

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

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

cName = '__test-item_object';
cValue = '{"key":value"}';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(cName), cValue, 'localStorage item has not been set');
clearStorageItem(cName);
});
}

0 comments on commit 8dd819d

Please sign in to comment.