-
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.
Merge branch 'release/v1.7' into fix/AG-16962
- Loading branch information
Showing
11 changed files
with
386 additions
and
126 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,22 @@ | ||
/** | ||
* Modifies passed keyword value according to its purpose. | ||
* Returns initial value if it's not a keyword. | ||
* @param {string} rawValue | ||
* @returns {string} | ||
*/ | ||
export const parseKeywordValue = (rawValue) => { | ||
const NOW_VALUE_KEYWORD = '$now$'; | ||
const CURRENT_DATE_KEYWORD = '$currentDate$'; | ||
|
||
let parsedValue = rawValue; | ||
|
||
if (rawValue === NOW_VALUE_KEYWORD) { | ||
// Set to current time in ms, e.g 1667915146503 | ||
parsedValue = Date.now().toString(); | ||
} else if (rawValue === CURRENT_DATE_KEYWORD) { | ||
// Set to current date e.g 'Tue Nov 08 2022 13:53:19 GMT+0300' | ||
parsedValue = Date(); | ||
} | ||
|
||
return parsedValue; | ||
}; |
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,19 @@ | ||
/** | ||
* Sets item to a specified storage, if storage isn't full. | ||
* @param {Storage} storage storage instance to set item into | ||
* @param {string} key | ||
* @param {string} value | ||
* @param {boolean} shouldLog determines if helper should log on a failed set attempt | ||
*/ | ||
export const setStorageItem = (storage, key, value, shouldLog) => { | ||
// eslint-disable-next-line no-console | ||
const log = console.log.bind(console); | ||
// setItem() may throw an exception if the storage is full. | ||
try { | ||
storage.setItem(key, value); | ||
} catch (e) { | ||
if (shouldLog) { | ||
log(`Unable to set sessionStorage item due to: ${e.message}`); | ||
} | ||
} | ||
}; |
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
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,87 @@ | ||
import { | ||
hit, | ||
nativeIsNaN, | ||
setStorageItem, | ||
parseKeywordValue, | ||
} from '../helpers/index'; | ||
|
||
/* eslint-disable max-len */ | ||
/** | ||
* @scriptlet trusted-set-local-storage-item | ||
* | ||
* @description | ||
* Adds item with arbitrary key and value to localStorage object, or updates the value of the key if it already exists. | ||
* Scriptlet won't set item if storage is full. | ||
* | ||
* **Syntax** | ||
* ``` | ||
* example.com#%#//scriptlet('trusted-set-local-storage-item', 'key', 'value') | ||
* ``` | ||
* | ||
* - `key` — required, key name to be set. | ||
* - `value` - required, key value; possible values: | ||
* - arbitrary value | ||
* - `$now$` keyword for setting current time in ms, corresponds to `Date.now()` and `(new Date).getTime()` calls | ||
* - `$currentDate$` keyword for setting string representation of the current date and time, corresponds to `Date()` and `(new Date).toString()` calls | ||
* | ||
* **Examples** | ||
* 1. Set local storage item | ||
* ``` | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.mute', 'false') | ||
* | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'COOKIE_CONSENTS', '{"preferences":3,"marketing":false}') | ||
* | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'providers', '[16364,88364]') | ||
* | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'providers', '{"providers":[16364,88364],"consent":"all"}') | ||
* ``` | ||
* | ||
* 2. Set item with current time since unix epoch in ms | ||
* ``` | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.play', '$now$') | ||
* ``` | ||
* | ||
* 3. Set item with current date, e.g 'Tue Nov 08 2022 13:53:19 GMT+0300' | ||
* ``` | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.play', '$currentDate$') | ||
* ``` | ||
* | ||
* 4. Set item without value | ||
* ``` | ||
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'ppu_main_none', '') | ||
* ``` | ||
*/ | ||
/* eslint-enable max-len */ | ||
|
||
export function trustedSetLocalStorageItem(source, key, value) { | ||
// eslint-disable-next-line no-console | ||
const log = console.log.bind(console); | ||
|
||
if (typeof key === 'undefined') { | ||
log('Item key should be specified.'); | ||
return; | ||
} | ||
|
||
if (typeof value === 'undefined') { | ||
log('Item value should be specified.'); | ||
return; | ||
} | ||
|
||
const parsedValue = parseKeywordValue(value); | ||
|
||
const { localStorage } = window; | ||
setStorageItem(localStorage, key, parsedValue, source.verbose); | ||
hit(source); | ||
} | ||
|
||
trustedSetLocalStorageItem.names = [ | ||
'trusted-set-local-storage-item', | ||
// trusted scriptlets support no aliases | ||
]; | ||
|
||
trustedSetLocalStorageItem.injections = [ | ||
hit, | ||
nativeIsNaN, | ||
setStorageItem, | ||
parseKeywordValue, | ||
]; |
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
Oops, something went wrong.