diff --git a/CHANGELOG.md b/CHANGELOG.md index 632750e1e..7842575dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,10 +11,13 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic ### Added +- `$currentISODate$` as a new possible value to `set-cookie`, `set-cookie-reload`, + `set-local-storage-item` and `set-session-storage-item` scriptlets [#435] - new values to `set-cookie` and `set-cookie-reload` scriptlets: `essential`, `nonessential` [#436] - `trusted-set-session-storage-item` scriptlet [#426] [Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.11.6...HEAD +[#435]: https://github.com/AdguardTeam/Scriptlets/issues/435 [#436]: https://github.com/AdguardTeam/Scriptlets/issues/436 [#426]: https://github.com/AdguardTeam/Scriptlets/issues/426 diff --git a/src/helpers/parse-keyword-value.ts b/src/helpers/parse-keyword-value.ts index fdac30e97..9bc57e35f 100644 --- a/src/helpers/parse-keyword-value.ts +++ b/src/helpers/parse-keyword-value.ts @@ -12,6 +12,7 @@ export const parseKeywordValue = (rawValue: string): string => { const NOW_VALUE_KEYWORD = '$now$'; const CURRENT_DATE_KEYWORD = '$currentDate$'; + const CURRENT_ISO_DATE_KEYWORD = '$currentISODate$'; let parsedValue = rawValue; @@ -21,6 +22,9 @@ export const parseKeywordValue = (rawValue: string): string => { } else if (rawValue === CURRENT_DATE_KEYWORD) { // Set to current date e.g 'Tue Nov 08 2022 13:53:19 GMT+0300' parsedValue = Date(); + } else if (rawValue === CURRENT_ISO_DATE_KEYWORD) { + // Set to current date e.g '2022-11-08T13:53:19.650Z' + parsedValue = new Date().toISOString(); } return parsedValue; diff --git a/src/scriptlets/trusted-set-cookie-reload.js b/src/scriptlets/trusted-set-cookie-reload.js index 153a063a6..a2cb9d530 100644 --- a/src/scriptlets/trusted-set-cookie-reload.js +++ b/src/scriptlets/trusted-set-cookie-reload.js @@ -36,6 +36,8 @@ import { * - empty string for no value * - `$now$` keyword for setting current time in ms, e.g 1667915146503 * - `$currentDate$` keyword for setting current time as string, e.g 'Tue Nov 08 2022 13:53:19 GMT+0300' + * - `$currentISODate$` keyword for setting current date in the date time string format, + * e.g '2022-11-08T13:53:19.650Z' * - `offsetExpiresSec` — optional, offset from current time in seconds, after which cookie should expire; * defaults to no offset. Possible values: * - positive integer in seconds diff --git a/src/scriptlets/trusted-set-cookie.js b/src/scriptlets/trusted-set-cookie.js index 591e5cee2..bbb5a9659 100644 --- a/src/scriptlets/trusted-set-cookie.js +++ b/src/scriptlets/trusted-set-cookie.js @@ -33,6 +33,8 @@ import { * - empty string for no value * - `$now$` keyword for setting current time in ms, e.g 1667915146503 * - `$currentDate$` keyword for setting current time as string, e.g 'Tue Nov 08 2022 13:53:19 GMT+0300' + * - `$currentISODate$` keyword for setting current date in the date time string format, + * e.g '2022-11-08T13:53:19.650Z' * - `offsetExpiresSec` — optional, offset from current time in seconds, after which cookie should expire; * defaults to no offset. Possible values: * - positive integer in seconds diff --git a/src/scriptlets/trusted-set-local-storage-item.js b/src/scriptlets/trusted-set-local-storage-item.js index 18b8b1265..633fb5ff3 100644 --- a/src/scriptlets/trusted-set-local-storage-item.js +++ b/src/scriptlets/trusted-set-local-storage-item.js @@ -26,6 +26,8 @@ import { * - `$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 + * - `$currentISODate$` keyword for setting current date in the date time string format, + * corresponds to `(new Date).toISOString()` call, e.g '2022-11-08T13:53:19.650Z' * * ### Examples * diff --git a/src/scriptlets/trusted-set-session-storage-item.ts b/src/scriptlets/trusted-set-session-storage-item.ts index bd9753af6..6fd4a7dd8 100644 --- a/src/scriptlets/trusted-set-session-storage-item.ts +++ b/src/scriptlets/trusted-set-session-storage-item.ts @@ -26,6 +26,8 @@ import { * - `$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 + * - `$currentISODate$` keyword for setting current date in the date time string format, + * corresponds to `(new Date).toISOString()` call, e.g '2022-11-08T13:53:19.650Z' * * ### Examples * diff --git a/tests/scriptlets/trusted-set-cookie.test.js b/tests/scriptlets/trusted-set-cookie.test.js index b03eade5a..f18884a59 100644 --- a/tests/scriptlets/trusted-set-cookie.test.js +++ b/tests/scriptlets/trusted-set-cookie.test.js @@ -77,6 +77,42 @@ test('Set cookie with current time value', (assert) => { clearCookie(cName); }); +test('Set cookie with current date value', (assert) => { + const cName = '__test-cookie_current_date'; + const cValue = '$currentDate$'; + + runScriptlet(name, [cName, cValue]); + + assert.strictEqual(window.hit, 'FIRED', 'Hit was fired'); + assert.strictEqual(document.cookie.includes(cName), true, 'Cookie name has been set'); + + const cookieValue = parseCookieString(document.cookie)[cName]; + const currentDate = Date(); + // Check only first 4 parts of the date (e.g. 'Tue Nov 08 2022') + const dateToCheck = currentDate.split(' ', 4).join(' '); + + assert.ok(cookieValue.startsWith(dateToCheck), 'Cookie value has been set to current date'); + clearCookie(cName); +}); + +test('Set cookie with current ISO time value', (assert) => { + const cName = '__test-cookie_current_iso_date'; + const cValue = '$currentISODate$'; + + runScriptlet(name, [cName, cValue]); + + assert.strictEqual(window.hit, 'FIRED', 'Hit was fired'); + assert.strictEqual(document.cookie.includes(cName), true, 'Cookie name has been set'); + + const cookieValue = parseCookieString(document.cookie)[cName]; + const currentIsoTime = new Date().toISOString(); + // Check only the date part of the ISO time (e.g. '2022-11-08') + const isoTimeToCheck = currentIsoTime.split('T')[0]; + + assert.ok(cookieValue.startsWith(isoTimeToCheck), 'Cookie value has been set to current ISO time'); + clearCookie(cName); +}); + test('Set cookie with expires', (assert) => { const cName = '__test-cookie_expires'; const cValue = 'expires'; diff --git a/tests/scriptlets/trusted-set-local-storage-item.test.js b/tests/scriptlets/trusted-set-local-storage-item.test.js index e6c67aa7a..cd0442965 100644 --- a/tests/scriptlets/trusted-set-local-storage-item.test.js +++ b/tests/scriptlets/trusted-set-local-storage-item.test.js @@ -137,4 +137,21 @@ if (isSafariBrowser()) { clearStorageItem(iName); }); + + test('Set localStorage item with $currentISODate$ keyword value', (assert) => { + const iName = '__test-item_current_iso_date'; + const iValue = '$currentISODate$'; + + runScriptlet(name, [iName, iValue]); + assert.strictEqual(window.hit, 'FIRED', 'Hit was fired'); + + const value = localStorage.getItem(iName); + const currentIsoTime = new Date().toISOString(); + // Check only the date part of the ISO time (e.g. '2022-11-08') + const isoTimeToCheck = currentIsoTime.split('T')[0]; + + assert.ok(value.startsWith(isoTimeToCheck), 'Item value has been set to current ISO time'); + + clearStorageItem(iName); + }); } diff --git a/tests/scriptlets/trusted-set-session-storage-item.test.js b/tests/scriptlets/trusted-set-session-storage-item.test.js index 855a6ec7a..7d1f8c9cb 100644 --- a/tests/scriptlets/trusted-set-session-storage-item.test.js +++ b/tests/scriptlets/trusted-set-session-storage-item.test.js @@ -137,4 +137,21 @@ if (isSafariBrowser()) { clearStorageItem(iName); }); + + test('Set sessionStorage item with $currentISODate$ keyword value', (assert) => { + const iName = '__test-item_current_iso_date'; + const iValue = '$currentISODate$'; + + runScriptlet(name, [iName, iValue]); + assert.strictEqual(window.hit, 'FIRED', 'Hit was fired'); + + const value = sessionStorage.getItem(iName); + const currentIsoTime = new Date().toISOString(); + // Check only the date part of the ISO time (e.g. '2022-11-08') + const isoTimeToCheck = currentIsoTime.split('T')[0]; + + assert.ok(value.startsWith(isoTimeToCheck), 'Item value has been set to current ISO time'); + + clearStorageItem(iName); + }); }