From 76c840fe4a63732d748f96811f9209b677bc2774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wr=C3=B3blewski?= Date: Fri, 12 Jul 2024 17:13:06 +0300 Subject: [PATCH] =?UTF-8?q?AG-34186=20Improve=20'trusted-set-cookie'=20and?= =?UTF-8?q?=20'trusted-set-cookie-reload'=20=E2=80=94=20add=20'$currentISO?= =?UTF-8?q?Date$'.=20#435?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed commit of the following: commit 1874bea40dc5782b7f950f7ca3ad68bdffc8f4ef Author: Adam Wróblewski Date: Fri Jul 12 10:37:49 2024 +0200 Add support for `$currentISODate$` value in `set-cookie`, `set-cookie-reload`, `set-local-storage-item` and `set-session-storage-item` scriptlets --- CHANGELOG.md | 3 ++ src/helpers/parse-keyword-value.ts | 4 +++ src/scriptlets/trusted-set-cookie-reload.js | 2 ++ src/scriptlets/trusted-set-cookie.js | 2 ++ .../trusted-set-local-storage-item.js | 2 ++ .../trusted-set-session-storage-item.ts | 2 ++ tests/scriptlets/trusted-set-cookie.test.js | 36 +++++++++++++++++++ .../trusted-set-local-storage-item.test.js | 17 +++++++++ .../trusted-set-session-storage-item.test.js | 17 +++++++++ 9 files changed, 85 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 632750e1..7842575d 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 fdac30e9..9bc57e35 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 153a063a..a2cb9d53 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 591e5cee..bbb5a965 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 18b8b126..633fb5ff 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 bd9753af..6fd4a7dd 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 b03eade5..f18884a5 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 e6c67aa7..cd044296 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 855a6ec7..7d1f8c9c 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); + }); }