Skip to content

Commit

Permalink
Add support for $currentISODate$ value in set-cookie, `set-cookie…
Browse files Browse the repository at this point in the history
…-reload`, `set-local-storage-item` and `set-session-storage-item` scriptlets
  • Loading branch information
AdamWr committed Jul 12, 2024
1 parent be12111 commit 1874bea
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/helpers/parse-keyword-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/trusted-set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/trusted-set-session-storage-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
36 changes: 36 additions & 0 deletions tests/scriptlets/trusted-set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
17 changes: 17 additions & 0 deletions tests/scriptlets/trusted-set-local-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
17 changes: 17 additions & 0 deletions tests/scriptlets/trusted-set-session-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 1874bea

Please sign in to comment.