-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathstorage.js
More file actions
70 lines (60 loc) · 1.95 KB
/
storage.js
File metadata and controls
70 lines (60 loc) · 1.95 KB
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
export const overrideLocalStorage = function(callback) {
const originSetItem = localStorage.setItem.bind(localStorage);
localStorage.setItem = function (key, value) {
// if (!isStr(key) || !isStr(value)) return;
originSetItem(key, value);
callback({type: 'setItem'})
};
const originRemoveItem = localStorage.removeItem.bind(localStorage);
localStorage.removeItem = function (key) {
originRemoveItem(key);
callback({type: 'removeItem'})
};
const originClear = localStorage.clear.bind(localStorage);
localStorage.clear = function (key) {
originClear();
callback({type: 'clear'})
};
}
export const overrideSessionStorage = function(callback) {
const originSetItem = sessionStorage.setItem.bind(sessionStorage);
sessionStorage.setItem = function (key, value) {
// if (!isStr(key) || !isStr(value)) return;
originSetItem(key, value);
callback({type: 'setItem'})
};
const originRemoveItem = sessionStorage.removeItem.bind(sessionStorage);
sessionStorage.removeItem = function (key) {
originRemoveItem(key);
callback({type: 'removeItem'})
};
const originClear = sessionStorage.clear.bind(sessionStorage);
sessionStorage.clear = function (key) {
originClear();
callback({type: 'clear'})
};
}
export const clearCookie = function () {
let cookieMap = getCookieMap()
for (const key in cookieMap) {
if (cookieMap.hasOwnProperty.call(cookieMap, key)) {
removeCookieItem(key)
}
}
}
export const removeCookieItem = function (key) {
document.cookie = encodeURIComponent(key) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
export const getCookieMap = function (params) {
const cookieMap = Object.create({})
const cookie = document.cookie;
if (cookie.trim() !== '') {
cookie.split(';').forEach(ele => {
ele = ele.split('=');
const key = ele.shift().trim();
ele = decodeURIComponent(ele.join('='));
cookieMap[key] = ele
})
}
return cookieMap;
}