-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssw-old.js
357 lines (284 loc) · 7.81 KB
/
ssw-old.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Storage - Cross Document Transport of JavaScript Data
*
* Extensible Wrapper for HTML 5's DOM Storage, Microsoft's UserData,
* Session Cookies and window.name using JSON serialization
*
* Author: Mathias Schaefer <molily@mailbox.org>
* License: Public Domain
*
*/
/* Namespace */
var storage = {};
/* Helper Functions */
storage.helper = {};
storage.helper.forEach = function(obj, func){
if (obj.constructor == Array) {
for (var i = 0, length = obj.length; i < length; i++) {
var result = func.call(obj, obj[i], i);
if (result != undefined) return result;
}
} else {
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
var result = func.call(obj, obj[name], name);
if (result != undefined) return result;
}
}
}
return null;
};
storage.helper.mixin = function (source, target) {
storage.helper.forEach(source, function (value, name) {
target[name] = value;
});
return target;
};
storage.helper.loadScript = function (uri) {
var head = document.getElementsByTagName("head")[0],
el = document.createElement("script");
el.type = "text/javascript";
el.src = uri;
head.appendChild(el);
};
/* Initialization */
storage.init = function () {
storage.implementation.detect();
};
/* Serializers */
storage.serializers = {};
/* JSON */
storage.serializers.json = {
scriptUri : "json2.js",
init : function () {
/* Load additional script if there's no native JSON */
if (!window.JSON) {
storage.helper.loadScript(this.scriptUri);
}
},
serialize : function (object) {
if (!(window.JSON && JSON.stringify)) return false;
return JSON.stringify(object);
},
unserialize : function (string) {
if (!(window.JSON || JSON.parse)) return false;
return JSON.parse(string);
}
};
/* Implementation Object */
storage.implementation = {};
/* Implementation List */
storage.implementation.list = [];
/* Add an implementation (with possibiliy to mixin a template) */
storage.implementation.add = function (obj1, obj2) {
var imp = arguments.length == 2 ? storage.helper.mixin(obj1, obj2) : obj1;
if (imp) {
/* Push object into the array *and* save it as a property */
this.list.push(imp);
this.list[imp.name] = imp;
}
};
/* Get an implementation by name or index */
storage.implementation.get = function (param) {
if (typeof param == "string") {
return storage.helper.forEach(this.list, function (imp) {
if (imp.name == param) return imp;
});
} else if (typeof param == "number") {
return this.list[param];
} else {
return false;
}
};
/* Auto-Detect available implementation */
storage.implementation.detect = function () {
var imp = storage.helper.forEach(this.list, function (imp) {
if (imp.isAvailable()) return imp;
});
if (!imp) return;
this.setup(imp);
};
/* Force an implementation (override auto-detect) */
storage.implementation.force = function (implementationName) {
this.setup(this.get(implementationName));
};
/* Setup and initialize an implementation */
storage.implementation.setup = function (imp) {
/* Setup public members */
storage.helper.forEach(["get", "add", "remove", "clear"], function (methodName) {
/* Wrap function to call the method with the right context ("this" points to implementation, not storage object) */
storage[methodName] = function () {
return imp[methodName].apply(imp, arguments);
};
});
/* Init Implementation */
imp.init();
/* Save active implementation as storage.implementation.active */
this.active = imp;
};
/* Mixins (Implementation Templates) */
storage.implementation.mixins = {};
storage.implementation.mixins.serialized = {
/* private members */
init : function () {
this.serializer.init();
this.serialize = this.serializer.serialize;
this.unserialize = this.serializer.unserialize;
if (this.specificInit) {
this.specificInit();
}
},
serializer : storage.serializers.json,
box : null,
/*
Implementation specific private methods:
isAvailable()
read()
save()
*/
/* public members */
get : function () {
if (this.box) return this.box;
var string = this.read(), obj = {};
if (string && string.charAt(0) == "{") {
obj = this.unserialize(string);
}
return this.box = obj;
},
add : function (obj) {
if (!this.box) {
this.box = {};
}
storage.helper.mixin(obj, this.box);
this.save(this.serialize(this.box));
},
remove : function (name) {
if (this.box && name in this.box) {
delete this.box[name];
this.save(this.serialize(this.box));
}
},
clear : function () {
this.box = {};
this.save();
if (this.specificClear) {
this.specificClear();
}
}
}
/* Implementations */
/* DOM Storage */
storage.implementation.add({
name : "domstorage",
/* private members */
init : function () {},
isAvailable : function () {
return Boolean(window.sessionStorage);
},
/* public members */
get : function () {
return sessionStorage;
},
add : function (obj) {
storage.helper.mixin(obj, sessionStorage);
},
remove : function (name) {
sessionStorage.removeItem(name);
},
clear : function () {
sessionStorage.clear();
}
});
/* userData */
storage.implementation.add(storage.implementation.mixins.serialized, {
name : "userdata",
/* necessary methods */
isAvailable : function () {
return Boolean(document.documentElement && document.documentElement.addBehavior);
},
read : function () {
this.element.load(this.storeName);
return this.element.getAttribute(this.storeName);
},
save : function (serializedString) {
this.element.setAttribute(this.storeName, serializedString);
this.element.save(this.storeName);
},
/* internal members */
storeName : "storage",
specificInit : function () {
this.element = document.documentElement;
this.element.addBehavior("#default#userData");
},
specificClear : function () {
this.element.expires = new Date(0).toUTCString();
}
});
/* Cookie */
storage.implementation.add(storage.implementation.mixins.serialized, {
name : "cookie",
/* necessary methods */
isAvailable : function () {
var v = "storage-test";
this.setCookie(v, v);
return this.getCookie(v) == v ? (this.deleteCookie(v), true) : false;
},
read : function () {
return this.getCookie(this.cookieName);
},
save : function (serializedString) {
this.setCookie(this.cookieName, serializedString);
},
/* internal members */
cookieName : "storage",
specificClear : function () {
this.deleteCookie(this.cookieName);
},
escapeCookie : function (value) {
/* Just escape semicolon and double quotes (the latter escpecially for Opera),
that is more economical than encodeURIComponent/decodeURIComponent */
return value.toString().replace(/%/g, "%%").replace(/;/g, '%S').replace(/"/g, '%Q');
},
unescapeCookie : function (value) {
return value.replace(/%Q/g, '"').replace(/%S/g, ";").replace(/%%/g, "%");
},
getCookie : function (name) {
var pairs = document.cookie.split(/;\s*/),
value = storage.helper.forEach(pairs, function (str) {
if (!str) return;
var separatorPosition = str.indexOf("="),
testName = str.substring(0, separatorPosition);
if (testName == name) {
return str.substring(separatorPosition + 1);
}
});
if (!value) return false;
return this.unescapeCookie(value);
},
setCookie : function (name, value) {
var cookieString = name + "=" + this.escapeCookie(value);
if (cookieString.length > 4096) {
throw new Error("Storage: Cookie size exceeds 4096 byte limit");
}
document.cookie = cookieString;
},
deleteCookie : function (name) {
document.cookie = name + '=;expires=' + new Date(0).toUTCString();
}
});
/* window.name */
storage.implementation.add(storage.implementation.mixins.serialized, {
name : "windowname",
/* necessary methods */
isAvailable : function () {
return (typeof window.name != "undefined");
},
read : function () {
return window.name;
},
save : function (serializedString) {
window.name = serializedString;
}
});
/* Call Init */
storage.init();