-
Notifications
You must be signed in to change notification settings - Fork 30
/
set-session-storage-item.js
121 lines (114 loc) · 3.41 KB
/
set-session-storage-item.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
import {
hit,
logMessage,
nativeIsNaN,
setStorageItem,
removeStorageItem,
getLimitedStorageItemValue,
isValidStrPattern,
toRegExp,
escapeRegExp,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet set-session-storage-item
*
* @description
* Adds specified key and its value to sessionStorage object, or updates the value of the key if it already exists.
* Scriptlet won't set item if storage is full.
*
* To remove item from sessionStorage use `$remove$` as a value.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#set-session-storage-itemjs-
*
* ### Syntax
*
* ```text
* example.com#%#//scriptlet('set-session-storage-item', 'key', 'value')
* ```
*
* - `key` — required, key name to be set. Should be a string for setting,
* but it also can be a regular expression for removing items from localStorage.
* - `value` — required, key value; possible values:
* - positive decimal integer `<= 32767`
* - one of the predefined constants in any case variation:
* - `undefined`
* - `false`
* - `true`
* - `null`
* - `emptyObj` — empty object
* - `emptyArr` — empty array
* - `''` — empty string
* - `yes`
* - `no`
* - `on`
* - `off`
* - `accept`
* - `accepted`
* - `reject`
* - `rejected`
* - `allowed`
* - `denied`
* - `$remove$` — remove specific item from sessionStorage
* - `forbidden`
* - `forever`
*
* ### Examples
*
* ```adblock
* example.org#%#//scriptlet('set-session-storage-item', 'player.live.current.mute', 'false')
*
* example.org#%#//scriptlet('set-session-storage-item', 'exit-intent-marketing', '1')
*
* ! Removes the item with key 'foo' from session storage
* example.org#%#//scriptlet('set-session-storage-item', 'foo', '$remove$')
*
* ! Removes from session storage all items whose key matches the regular expression `/mp_.*_mixpanel/`
* example.org#%#//scriptlet('set-session-storage-item', '/mp_.*_mixpanel/', '$remove$')
* ```
*
* @added v1.4.3.
*/
/* eslint-enable max-len */
export function setSessionStorageItem(source, key, value) {
if (typeof key === 'undefined') {
logMessage(source, 'Item key should be specified.');
return;
}
let validValue;
try {
validValue = getLimitedStorageItemValue(value);
} catch {
logMessage(source, `Invalid storage item value: '${value}'`);
return;
}
const { sessionStorage } = window;
if (validValue === '$remove$') {
removeStorageItem(source, sessionStorage, key);
} else {
setStorageItem(source, sessionStorage, key, validValue);
}
hit(source);
}
export const setSessionStorageItemNames = [
'set-session-storage-item',
// aliases are needed for matching the related scriptlet converted into our syntax
'set-session-storage-item.js',
'ubo-set-session-storage-item.js',
'ubo-set-session-storage-item',
];
// eslint-disable-next-line prefer-destructuring
setSessionStorageItem.primaryName = setSessionStorageItemNames[0];
setSessionStorageItem.injections = [
hit,
logMessage,
nativeIsNaN,
setStorageItem,
removeStorageItem,
getLimitedStorageItemValue,
// following helpers are needed for helpers above
isValidStrPattern,
toRegExp,
escapeRegExp,
];