From aa899d35f86b6972c430f71f9dbf992c264f185a Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Tue, 8 Jan 2013 23:15:45 +0100 Subject: [PATCH] fixed problem where localStorage wouldn't work like OS X 10.7 --- WebKit/scripts/helper/CacheStorage.js | 57 +++++++++++++++++---------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/WebKit/scripts/helper/CacheStorage.js b/WebKit/scripts/helper/CacheStorage.js index 6af7534..1f9343e 100644 --- a/WebKit/scripts/helper/CacheStorage.js +++ b/WebKit/scripts/helper/CacheStorage.js @@ -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;