-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add trusted-set-local-storage-item scriptelt
- Loading branch information
1 parent
788f73a
commit 8dd819d
Showing
5 changed files
with
202 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |