Skip to content

Commit

Permalink
Merge branch 'release/v1.7' into fix/AG-16962
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 10, 2022
2 parents 9863ab2 + 356dd0d commit c252d85
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 126 deletions.
2 changes: 2 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export * from './random-response';
export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './match-request-props';
export * from './storage-utils';
export * from './parse-keyword-value';
22 changes: 22 additions & 0 deletions src/helpers/parse-keyword-value.js
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;
};
19 changes: 19 additions & 0 deletions src/helpers/storage-utils.js
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}`);
}
}
};
1 change: 1 addition & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ export * from './trusted-replace-xhr-response';
export * from './xml-prune';
export * from './trusted-set-cookie';
export * from './trusted-replace-fetch-response';
export * from './trusted-set-local-storage-item';
32 changes: 16 additions & 16 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
hit,
nativeIsNaN,
setStorageItem,
} from '../helpers/index';

/* eslint-disable max-len */
Expand All @@ -9,6 +10,7 @@ import {
*
* @description
* Adds specified key and its 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**
* ```
Expand Down Expand Up @@ -39,7 +41,16 @@ import {
/* eslint-enable max-len */

export function setLocalStorageItem(source, key, value) {
if (!key || (!value && 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;
}

Expand Down Expand Up @@ -74,21 +85,9 @@ export function setLocalStorageItem(source, key, 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, keyValue);
const { localStorage } = window;
setStorageItem(localStorage, key, keyValue, source.verbose);
hit(source);
}

setLocalStorageItem.names = [
Expand All @@ -98,4 +97,5 @@ setLocalStorageItem.names = [
setLocalStorageItem.injections = [
hit,
nativeIsNaN,
setStorageItem,
];
31 changes: 15 additions & 16 deletions src/scriptlets/set-session-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
hit,
nativeIsNaN,
setStorageItem,
} from '../helpers/index';

/* eslint-disable max-len */
Expand All @@ -9,6 +10,7 @@ import {
*
* @description
* Adds specified key and its value to sessionStorage object, or updates the value of the key if it already exists.
* Scriptlet won't set item if storage is full.
*
* **Syntax**
* ```
Expand Down Expand Up @@ -39,7 +41,16 @@ import {
/* eslint-enable max-len */

export function setSessionStorageItem(source, key, value) {
if (!key || (!value && 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;
}

Expand Down Expand Up @@ -74,21 +85,8 @@ export function setSessionStorageItem(source, key, value) {
return;
}

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

setItem(key, keyValue);
const { sessionStorage } = window;
setStorageItem(sessionStorage, key, keyValue, source.verbose);
}

setSessionStorageItem.names = [
Expand All @@ -98,4 +96,5 @@ setSessionStorageItem.names = [
setSessionStorageItem.injections = [
hit,
nativeIsNaN,
setStorageItem,
];
15 changes: 3 additions & 12 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
nativeIsNaN,
isCookieSetWithValue,
concatCookieNameValuePath,
parseKeywordValue,
// following helpers should be imported and injected
// because they are used by helpers above
isValidCookieRawPath,
Expand Down Expand Up @@ -90,21 +91,10 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel
return;
}

const NOW_VALUE_KEYWORD = '$now$';
const ONE_YEAR_EXPIRATION_KEYWORD = '1year';
const ONE_DAY_EXPIRATION_KEYWORD = '1day';

let parsedValue;

if (value === NOW_VALUE_KEYWORD) {
// Set cookie value to current time if corresponding keyword was passed
const date = new Date();
const currentTime = date.getTime();

parsedValue = currentTime.toString();
} else {
parsedValue = value;
}
const parsedValue = parseKeywordValue(value);

let cookieToSet = concatCookieNameValuePath(name, parsedValue, path);
if (!cookieToSet) {
Expand Down Expand Up @@ -161,5 +151,6 @@ trustedSetCookie.injections = [
isCookieSetWithValue,
concatCookieNameValuePath,
isValidCookieRawPath,
parseKeywordValue,
getCookiePath,
];
87 changes: 87 additions & 0 deletions src/scriptlets/trusted-set-local-storage-item.js
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,
];
1 change: 1 addition & 0 deletions tests/scriptlets/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ import './xml-prune.test';
import './trusted-click-element.test';
import './trusted-set-cookie.test';
import './trusted-replace-fetch-response.test';
import './trusted-set-local-storage-item.test';
Loading

0 comments on commit c252d85

Please sign in to comment.