Skip to content

Commit c6b1575

Browse files
committed
AG-41928 Add support for 'emptyArr' and 'emptyObj' in 'set-cookie' and 'set-cookie-reload' scriptlets. #497
Squashed commit of the following: commit 1e6f1c9 Author: Adam Wróblewski <[email protected]> Date: Fri Apr 25 16:37:30 2025 +0200 Add support for 'emptyArr' and 'emptyObj' in `set-cookie` and `set-cookie-reload` scriptlets
1 parent 70ec3c0 commit c6b1575

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
1414

1515
### Added
1616

17+
- New values to `set-cookie` and `set-cookie-reload` scriptlets: `emptyArr`, `emptyObj` [#497].
1718
- Ability to set random response content in `prevent-fetch` scriptlet [#416].
1819
- Ability to choose CSS injection method in `inject-css-in-shadow-dom` scriptlet [#477].
1920
- TypeScript types for CoreLibs provided [`ContentScriptApi`](./README.md#scriptlets-api--content-script-api).
@@ -33,6 +34,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
3334
[#416]: https://github.com/AdguardTeam/Scriptlets/issues/416
3435
[#477]: https://github.com/AdguardTeam/Scriptlets/issues/477
3536
[#496]: https://github.com/AdguardTeam/Scriptlets/issues/496
37+
[#497]: https://github.com/AdguardTeam/Scriptlets/issues/497
3638

3739
## [v2.1.7] - 2025-04-03
3840

src/helpers/cookie-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export const getLimitedCookieValue = (value: string): string | number | null =>
138138
let validValue;
139139
if (allowedCookieValues.has(value.toLowerCase())) {
140140
validValue = value;
141+
} else if (value === 'emptyArr') {
142+
validValue = '[]';
143+
} else if (value === 'emptyObj') {
144+
validValue = '{}';
141145
} else if (/^\d+$/.test(value)) {
142146
validValue = parseFloat(value);
143147
if (nativeIsNaN(validValue)) {

src/scriptlets/set-cookie-reload.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
* - `essential` / `nonessential`
4545
* - `checked` / `unchecked`
4646
* - `forbidden` / `forever`
47+
* - `emptyArr` to set an empty array `[]`
48+
* - `emptyObj` to set an empty object `{}`
4749
* - `path` — optional, cookie path, defaults to `/`; possible values:
4850
* - `/` — root path
4951
* - `none` — to set no path at all
@@ -90,7 +92,7 @@ export function setCookieReload(source, name, value, path = '/', domain = '') {
9092
return;
9193
}
9294

93-
const cookieToSet = serializeCookie(name, validValue, path, domain);
95+
const cookieToSet = serializeCookie(name, validValue, path, domain, false);
9496
if (!cookieToSet) {
9597
logMessage(source, 'Invalid cookie name or value');
9698
return;

src/scriptlets/set-cookie.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import {
4646
* - `essential` / `nonessential`
4747
* - `checked` / `unchecked`
4848
* - `forbidden` / `forever`
49+
* - `emptyArr` to set an empty array `[]`
50+
* - `emptyObj` to set an empty object `{}`
4951
* - `path` — optional, cookie path, defaults to `/`; possible values:
5052
* - `/` — root path
5153
* - `none` — to set no path at all
@@ -89,7 +91,7 @@ export function setCookie(source, name, value, path = '/', domain = '') {
8991
return;
9092
}
9193

92-
const cookieToSet = serializeCookie(name, validValue, path, domain);
94+
const cookieToSet = serializeCookie(name, validValue, path, domain, false);
9395
if (!cookieToSet) {
9496
logMessage(source, 'Invalid cookie name or value');
9597
return;

tests/scriptlets/set-cookie.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,40 @@ const cookies = [
7070
['__test-cookie_unchecked', 'unchecked'],
7171
['__test-cookie_forbidden', 'forbidden'],
7272
['__test-cookie_forever', 'forever'],
73+
['__test-cookie_emptyArray', 'emptyArr'],
74+
['__test-cookie_emptyObject', 'emptyObj'],
7375
];
7476

7577
test.each('Set cookie with valid value', cookies, (assert, [cName, cValue]) => {
78+
const emptyArrValue = '[]';
79+
const emptyObjValue = '{}';
80+
7681
runScriptlet(name, [cName, cValue]);
7782
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
78-
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
83+
switch (cValue) {
84+
case 'emptyArr':
85+
assert.strictEqual(
86+
document.cookie.includes(cName)
87+
&& document.cookie.includes(emptyArrValue),
88+
true,
89+
'Cookie is set',
90+
);
91+
break;
92+
case 'emptyObj':
93+
assert.strictEqual(
94+
document.cookie.includes(cName)
95+
&& document.cookie.includes(emptyObjValue),
96+
true,
97+
'Cookie is set');
98+
break;
99+
default:
100+
assert.strictEqual(
101+
document.cookie.includes(cName)
102+
&& document.cookie.includes(cValue),
103+
true,
104+
'Cookie is set',
105+
);
106+
}
79107
clearCookie(cName);
80108
});
81109

0 commit comments

Comments
 (0)