fixed problem where localStorage wouldn't work like OS X 10.7
This commit is contained in:
parent
4b8c157601
commit
aa899d35f8
1 changed files with 36 additions and 21 deletions
|
@ -16,47 +16,62 @@ function() {
|
|||
};
|
||||
|
||||
CacheStorage.prototype.getItem = function(key) {
|
||||
return JSON.parse(localStorage.getItem(this.mkPath(key)));
|
||||
var item = null;
|
||||
|
||||
try { // If localStorage doesn't work then just leave it empty
|
||||
item = JSON.parse(localStorage.getItem(this.mkPath(key)));
|
||||
} catch(e) {}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
CacheStorage.prototype.setItem = function(key, value) {
|
||||
var item = this.getItem(key);
|
||||
|
||||
localStorage.setItem(this.mkPath(key), JSON.stringify(value));
|
||||
try {
|
||||
localStorage.setItem(this.mkPath(key), JSON.stringify(value));
|
||||
|
||||
if (!item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) + 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
if (!item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) + 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
} catch(e) {}
|
||||
}
|
||||
|
||||
CacheStorage.prototype.removeItem = function(key) {
|
||||
|
||||
var item = this.getItem(key);
|
||||
|
||||
localStorage.removeItem(this.mkPath(key));
|
||||
try {
|
||||
localStorage.removeItem(this.mkPath(key));
|
||||
|
||||
if (item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) - 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
if (item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) - 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
} catch(e) {}
|
||||
};
|
||||
|
||||
CacheStorage.prototype.clear = function() {
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
if (key.startsWith(this.mkPath(""))) {
|
||||
localStorage.removeItem(key);
|
||||
try {
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
if (key.startsWith(this.mkPath(""))) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem(this.mkInternalPath("_length"), 0);
|
||||
localStorage.setItem(this.mkInternalPath("_length"), 0);
|
||||
} catch(e) {}
|
||||
}
|
||||
|
||||
CacheStorage.prototype.length = function() {
|
||||
return parseInt(localStorage.getItem(this.mkInternalPath("_length")), 10);
|
||||
var l = 0;
|
||||
try {
|
||||
l = parseInt(localStorage.getItem(this.mkInternalPath("_length")), 10);
|
||||
} catch(e) {}
|
||||
return l;
|
||||
}
|
||||
|
||||
return CacheStorage;
|
||||
|
|
Reference in a new issue