add fallback localStorage implementation if not functional, #134

This commit is contained in:
Martin Zimmermann 2014-11-09 22:13:26 +01:00
parent f06be982e7
commit f0a0f40223
2 changed files with 26 additions and 2 deletions

View File

@ -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"))

View File

@ -68,11 +68,34 @@ define(["app/i18n"], function(i18n) {
.replace(/\n/gi, '<br>');
};
// 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
};
});