-
Notifications
You must be signed in to change notification settings - Fork 2
/
unistorage.js
215 lines (196 loc) · 4.11 KB
/
unistorage.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function LS()
{
try
{
if (localStorage)
{
const test = "test" + new Date().valueOf();
try
{
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
}
catch(e)
{
return false;
}
}
else
{
return false;
}
}
catch (X)
{
return false;
}
}
function DataExists(name)
{
if (LS())
{
if (localStorage[name])
{
return true;
}
else
{
return false;
}
}
else
{
if (document.cookie !== "")
{
const cookies = document.cookie.split(new RegExp("; *", ""));
for (let i=0; i<cookies.length; i++)
{
const cookieName = cookies[i].split("=")[0];
const cookieVal = cookies[i].split("=")[1];
if (cookieName === decodeURIComponent(name))
{
return true;
}
}
}
return false;
}
}
function DataGet(name)
{
if (LS())
{
return(localStorage.getItem(name));
}
else
{
if (document.cookie !== "")
{
const cookies = document.cookie.split(new RegExp("; *", ""));
for (let i=0; i<cookies.length; i++)
{
const cookieName = cookies[i].split("=")[0];
const cookieVal = cookies[i].split("=")[1];
if (cookieName === decodeURIComponent(name))
{
return decodeURIComponent(cookieVal);
}
}
}
}
}
function DataSet(name, val)
{
if (LS())
{
localStorage.setItem(name, val);
}
else
{
const cookieName = encodeURIComponent(name);
const cookieVal = encodeURIComponent(val);
let cookieText = cookieName + "=" + cookieVal;
const data = new Date(2099, 1, 1, 0, 0, 0, 0);
cookieText += "; expires=" + data.toGMTString();
document.cookie = cookieText;
}
}
function DataDelete(name)
{
if (DataExists(name))
{
if (LS())
{
localStorage.removeItem(name)
}
else
{
const cookieName = encodeURIComponent(name);
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT;';
}
}
}
function DataRaw()
{
if (LS())
{
var raw = "";
for (var item in localStorage)
{
raw += (item + "=" + localStorage.getItem(item) + "\n" );
}
return raw;
}
else
{
return document.cookie;
}
}
function DataClear()
{
if (LS())
{
localStorage.clear();
}
else
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
}
function DataSetI(name, val)
{
DataSet(name, val);
}
function DataGetI(name)
{
return parseInt(DataGet(name));
}
function DataSetB(name, val)
{
DataSet(name, val ? "1" : "0");
}
function DataGetB(name)
{
return (DataGet(name) == "1");
}
function DataGetDefault(name, val)
{
if (DataExists(name))
{
return DataGet(name);
}
else
{
return val;
}
}
function DataGetIDefault(name, val)
{
if (DataExists(name))
{
return DataGetI(name);
}
else
{
return val;
}
}
function DataGetBDefault(name, val)
{
if (DataExists(name))
{
return DataGetB(name);
}
else
{
return val;
}
}