fixed problem where localStorage wouldn't work like OS X 10.7

This commit is contained in:
Jeena Paradies 2013-01-08 23:15:45 +01:00
parent 4b8c157601
commit aa899d35f8

View file

@ -16,12 +16,19 @@ function() {
}; };
CacheStorage.prototype.getItem = function(key) { 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) { CacheStorage.prototype.setItem = function(key, value) {
var item = this.getItem(key); var item = this.getItem(key);
try {
localStorage.setItem(this.mkPath(key), JSON.stringify(value)); localStorage.setItem(this.mkPath(key), JSON.stringify(value));
if (!item) { if (!item) {
@ -29,12 +36,13 @@ function() {
var length = parseInt(localStorage.getItem(length_path), 10) + 1; var length = parseInt(localStorage.getItem(length_path), 10) + 1;
localStorage.setItem(length_path, length); localStorage.setItem(length_path, length);
} }
} catch(e) {}
} }
CacheStorage.prototype.removeItem = function(key) { CacheStorage.prototype.removeItem = function(key) {
var item = this.getItem(key); var item = this.getItem(key);
try {
localStorage.removeItem(this.mkPath(key)); localStorage.removeItem(this.mkPath(key));
if (item) { if (item) {
@ -42,9 +50,11 @@ function() {
var length = parseInt(localStorage.getItem(length_path), 10) - 1; var length = parseInt(localStorage.getItem(length_path), 10) - 1;
localStorage.setItem(length_path, length); localStorage.setItem(length_path, length);
} }
} catch(e) {}
}; };
CacheStorage.prototype.clear = function() { CacheStorage.prototype.clear = function() {
try {
for (var i = 0; i < localStorage.length; i++) { for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i); var key = localStorage.key(i);
if (key.startsWith(this.mkPath(""))) { if (key.startsWith(this.mkPath(""))) {
@ -53,10 +63,15 @@ function() {
} }
localStorage.setItem(this.mkInternalPath("_length"), 0); localStorage.setItem(this.mkInternalPath("_length"), 0);
} catch(e) {}
} }
CacheStorage.prototype.length = function() { 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; return CacheStorage;