-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-set-cookie-reload.js
177 lines (161 loc) · 5.8 KB
/
trusted-set-cookie-reload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
serializeCookie,
isValidCookiePath,
parseKeywordValue,
getTrustedCookieOffsetMs,
parseCookieString,
getCookiePath,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-set-cookie-reload
*
* @description
* Sets a cookie with arbitrary name and value,
* and with optional ability to offset cookie attribute 'expires', set path
* and set domain.
* Also reloads the current page after the cookie setting.
* If reloading option is not needed, use the [`trusted-set-cookie` scriptlet](#trusted-set-cookie).
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-set-cookie-reload', name, value[, offsetExpiresSec[, path[, domain]]])
* ```
*
* - `name` — required, cookie name to be set
* - `value` — required, cookie value. Possible values:
* - arbitrary value
* - 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
* - `1year` keyword for setting expiration date to one year
* - `1day` keyword for setting expiration date to one day
* - `path` — optional, argument for setting cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
* - `domain` — optional, cookie domain, if not set origin will be set as domain,
* if the domain does not match the origin, the cookie will not be set
*
* > Note that the scriptlet does not encode cookie names and values.
* > As a result, if a cookie's name or value includes `;`,
* > the scriptlet will not set the cookie since this may cause the cookie to break.
*
* ### Examples
*
* 1. Set cookie and reload the page after it
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', 'accept')
* ```
*
* 1. Set cookie with `new Date().getTime()` value and reload the page after it
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', '$now$')
* ```
*
* 1. Set cookie which will expire in 3 days and reload the page after it
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', 'accept', '259200')
* ```
*
* 1. Set cookie which will expire in one year and reload the page after it
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', 'accept', '1year')
* ```
*
* 1. Set cookie with no 'expire' and no path, reload the page after it
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', 'decline', '', 'none')
* ```
*
* 1. Set cookie with domain
*
* ```adblock
* example.org#%#//scriptlet('trusted-set-cookie-reload', 'cmpconsent', 'decline', '', 'none', 'example.org')
* ```
*
* @added v1.7.10.
*/
/* eslint-enable max-len */
export function trustedSetCookieReload(source, name, value, offsetExpiresSec = '', path = '/', domain = '') {
if (typeof name === 'undefined') {
logMessage(source, 'Cookie name should be specified');
return;
}
if (typeof value === 'undefined') {
logMessage(source, 'Cookie value should be specified');
return;
}
// Prevent infinite reloads if cookie was already set or blocked by the browser
// https://github.com/AdguardTeam/Scriptlets/issues/212
if (isCookieSetWithValue(document.cookie, name, value)) {
return;
}
const parsedValue = parseKeywordValue(value);
if (!isValidCookiePath(path)) {
logMessage(source, `Invalid cookie path: '${path}'`);
return;
}
if (!document.location.origin.includes(domain)) {
logMessage(source, `Cookie domain not matched by origin: '${domain}'`);
return;
}
let cookieToSet = serializeCookie(name, parsedValue, path, domain, false);
if (!cookieToSet) {
logMessage(source, 'Invalid cookie name or value');
return;
}
// TODO: Move this concat to serializeCookie
if (offsetExpiresSec) {
const parsedOffsetMs = getTrustedCookieOffsetMs(offsetExpiresSec);
if (!parsedOffsetMs) {
logMessage(source, `Invalid offsetExpiresSec value: ${offsetExpiresSec}`);
return;
}
const expires = Date.now() + parsedOffsetMs;
cookieToSet += `; expires=${new Date(expires).toUTCString()}`;
}
document.cookie = cookieToSet;
hit(source);
// Get cookie value, it's required for checking purpose
// in case if $now$ or $currentDate$ value is used
// https://github.com/AdguardTeam/Scriptlets/issues/291
const cookieValueToCheck = parseCookieString(document.cookie)[name];
// Only reload the page if cookie was set
// https://github.com/AdguardTeam/Scriptlets/issues/212
if (isCookieSetWithValue(document.cookie, name, cookieValueToCheck)) {
window.location.reload();
}
}
export const trustedSetCookieReloadNames = [
'trusted-set-cookie-reload',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedSetCookieReload.primaryName = trustedSetCookieReloadNames[0];
trustedSetCookieReload.injections = [
hit,
logMessage,
nativeIsNaN,
isCookieSetWithValue,
serializeCookie,
isValidCookiePath,
getTrustedCookieOffsetMs,
parseKeywordValue,
parseCookieString,
getCookiePath,
];