Skip to content

Commit

Permalink
Merge branch 'fix/AG-17113' into fix/AG-17113_01
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Oct 25, 2022
2 parents d435aac + e05fefc commit ac00fb1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { nativeIsNaN } from './number-utils';
* Prepares cookie string if given parameters are ok
* @param {string} name cookie name to set
* @param {string} value cookie value to set
* @param {string} path cookie path to set, no set for 'none'
* @returns {string|null} cookie string if ok OR null if not
*/
export const prepareCookie = (name, value) => {
if (!name || !value) {
export const prepareCookie = (name, value, path) => {
if (!name || !value || !path) {
return null;
}

Expand Down Expand Up @@ -43,7 +44,9 @@ export const prepareCookie = (name, value) => {
return null;
}

const pathToSet = 'path=/;';
const pathToSet = path === 'none'
? ''
: `path=${path}`;
// eslint-disable-next-line max-len
const cookieData = `${encodeURIComponent(name)}=${encodeURIComponent(valueToSet)}; ${pathToSet}`;

Expand Down
14 changes: 10 additions & 4 deletions src/scriptlets/set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
* @scriptlet set-cookie-reload
*
* @description
* Sets a cookie with the specified name and value, and then reloads the current page.
* Sets a cookie with the specified name and value, and path,
* and reloads the current page after the cookie setting.
* If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet.
*
* **Syntax**
* ```
* example.org#%#//scriptlet('set-cookie-reload', name, value)
* example.org#%#//scriptlet('set-cookie-reload', name, value[, path])
* ```
*
* - `name` - required, cookie name to be set
Expand All @@ -25,15 +26,20 @@ import {
* - `yes` / `Yes` / `Y`
* - `no`
* - `ok` / `OK`
* - `path` - optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
*
* **Examples**
* ```
* example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok')
*
* example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1')
*
* example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none')
* ```
*/
export function setCookieReload(source, name, value) {
export function setCookieReload(source, name, value, path = '/') {
const isCookieSetWithValue = (name, value) => {
return document.cookie.split(';')
.some((cookieStr) => {
Expand All @@ -52,7 +58,7 @@ export function setCookieReload(source, name, value) {
return;
}

const cookieData = prepareCookie(name, value);
const cookieData = prepareCookie(name, value, path);

if (cookieData) {
document.cookie = cookieData;
Expand Down
15 changes: 10 additions & 5 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index';
* @scriptlet set-cookie
*
* @description
* Sets a cookie with the specified name and value. Cookie path defaults to root.
* Sets a cookie with the specified name, value, and path.
*
* **Syntax**
* ```
* example.org#%#//scriptlet('set-cookie', name, value)
* example.org#%#//scriptlet('set-cookie', name, value[, path])
* ```
*
* - `name` - required, cookie name to be set
Expand All @@ -21,17 +21,22 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index';
* - `yes` / `Yes` / `Y`
* - `no`
* - `ok` / `OK`
* - `path` - optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
*
* **Examples**
* ```
* example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1')
* example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1')
*
* example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true')
*
* example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none')
* ```
*/
/* eslint-enable max-len */
export function setCookie(source, name, value) {
const cookieData = prepareCookie(name, value);
export function setCookie(source, name, value, path = '/') {
const cookieData = prepareCookie(name, value, path);

if (cookieData) {
hit(source);
Expand Down
21 changes: 16 additions & 5 deletions wiki/about-scriptlets.md
Original file line number Diff line number Diff line change
Expand Up @@ -1557,12 +1557,13 @@ example.org#%#//scriptlet('set-constant', 'document.third', 'trueFunc', 'checkin
### <a id="set-cookie-reload"></a> ⚡️ set-cookie-reload
Sets a cookie with the specified name and value, and then reloads the current page.
Sets a cookie with the specified name and value, and path,
and reloads the current page after the cookie setting.
If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet.
**Syntax**
```
example.org#%#//scriptlet('set-cookie-reload', name, value)
example.org#%#//scriptlet('set-cookie-reload', name, value[, path])
```
- `name` - required, cookie name to be set
Expand All @@ -1574,24 +1575,29 @@ example.org#%#//scriptlet('set-cookie-reload', name, value)
- `yes` / `Yes` / `Y`
- `no`
- `ok` / `OK`
- `path` - optional, cookie path, defaults to `/`; possible values:
- `/` — root path
- `none` — to set no path at all
**Examples**
```
example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok')
example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1')
example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none')
```
[Scriptlet source](../src/scriptlets/set-cookie-reload.js)
* * *
### <a id="set-cookie"></a> ⚡️ set-cookie
Sets a cookie with the specified name and value. Cookie path defaults to root.
Sets a cookie with the specified name, value, and path.
**Syntax**
```
example.org#%#//scriptlet('set-cookie', name, value)
example.org#%#//scriptlet('set-cookie', name, value[, path])
```
- `name` - required, cookie name to be set
Expand All @@ -1603,12 +1609,17 @@ example.org#%#//scriptlet('set-cookie', name, value)
- `yes` / `Yes` / `Y`
- `no`
- `ok` / `OK`
- `path` - optional, cookie path, defaults to `/`; possible values:
- `/` — root path
- `none` — to set no path at all
**Examples**
```
example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1')
example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1')
example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true')
example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none')
```
[Scriptlet source](../src/scriptlets/set-cookie.js)
Expand Down

0 comments on commit ac00fb1

Please sign in to comment.