From f0a0f4022380c122f20ac378af012af3514ce447 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Sun, 9 Nov 2014 22:13:26 +0100 Subject: [PATCH] add fallback localStorage implementation if not functional, #134 --- isso/js/app/isso.js | 3 ++- isso/js/app/utils.js | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/isso/js/app/isso.js b/isso/js/app/isso.js index ec97d63..21aec7c 100644 --- a/isso/js/app/isso.js +++ b/isso/js/app/isso.js @@ -7,7 +7,8 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n", var Postbox = function(parent) { - var el = $.htmlify(jade.render("postbox", { + var localStorage = utils.localStorageImpl, + el = $.htmlify(jade.render("postbox", { "author": JSON.parse(localStorage.getItem("author")), "email": JSON.parse(localStorage.getItem("email")), "website": JSON.parse(localStorage.getItem("website")) diff --git a/isso/js/app/utils.js b/isso/js/app/utils.js index d7bddf4..f971770 100644 --- a/isso/js/app/utils.js +++ b/isso/js/app/utils.js @@ -68,11 +68,34 @@ define(["app/i18n"], function(i18n) { .replace(/\n/gi, '
'); }; + // Safari private browsing mode supports localStorage, but throws QUOTA_EXCEEDED_ERR + var localStorageImpl; + try { + localStorage.setItem("x", "y"); + localStorage.removeItem("x"); + localStorageImpl = localStorage; + } catch (ex) { + localStorageImpl = (function(storage) { + return { + setItem: function(key, val) { + storage[key] = val; + }, + getItem: function(key) { + return typeof(storage[key]) !== 'undefined' ? storage[key] : null; + }, + removeItem: function(key) { + delete storage[key]; + } + }; + })({}); + } + return { cookie: cookie, pad: pad, ago: ago, text: text, - detext: detext + detext: detext, + localStorageImpl: localStorageImpl }; });