forked from Macil/webstorage-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (103 loc) · 3.32 KB
/
index.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
(function() {
'use strict';
try {
// Test webstorage existence.
if (!window.localStorage || !window.sessionStorage) throw "exception";
// Test webstorage accessibility - Needed for Safari private browsing.
window.localStorage.setItem('storage_test', 1);
window.localStorage.removeItem('storage_test');
} catch (e) {
(function() {
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var results = document.cookie.match('(^|;) ?'+encodeURIComponent(name)+'=([^;]*)(;|$)');
return results ? decodeURIComponent(results[2]) : null;
}
function Storage(type) {
function setData(data) {
data = JSON.stringify(data);
if (type === 'session') {
createCookie(getSessionName(), data);
} else {
createCookie('localStorage', data, 365);
}
}
function clearData() {
if (type === 'session') {
createCookie(getSessionName(), '');
} else {
createCookie('localStorage', '', 365);
}
}
function getData() {
var data = readCookie(type === 'session' ? getSessionName() : 'localStorage');
return data ? JSON.parse(data) : {};
}
function getSessionName() {
if (!window.name) {
window.name = Math.random()+'-'+(new Date().getTime());
}
return 'sessionStorage-' + window.name;
}
// Initialize if there's already data.
var data = getData();
var obj = {
POLYFILLED: true,
length: 0,
clear: function() {
data = {};
this.length = 0;
clearData();
},
getItem: function(key) {
return Object.prototype.hasOwnProperty.call(data, key) ? data[key] : null;
},
key: function(i) {
var ctr = 0;
for (var k in data) {
if (ctr++ == i)
return k;
}
return null;
},
removeItem: function(key) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
delete data[key];
this.length--;
setData(data);
}
},
setItem: function(key, value) {
if (!Object.prototype.hasOwnProperty.call(data, key)) {
this.length++;
}
data[key] = ''+value;
setData(data);
}
};
return obj;
}
var localStorage = new Storage('local');
var sessionStorage = new Storage('session');
try {
window.localStorage = localStorage;
window.sessionStorage = sessionStorage;
} catch(e) {}
try {
// For Safari private browsing need to also set the proto value.
window.localStorage.__proto__ = localStorage;
window.sessionStorage.__proto__ = sessionStorage;
} catch(e) {}
})();
}
})();