Skip to content

Commit

Permalink
Merge pull request #625 from thelounge/astorije/safe-localstorage
Browse files Browse the repository at this point in the history
Ensure localStorage cannot fail because of quota or Safari private browsing
  • Loading branch information
astorije committed Sep 25, 2016
2 parents eaa05f2 + 813572d commit 937cc4e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions client/js/lounge.js
Expand Up @@ -54,6 +54,16 @@ $(function() {
return Handlebars.templates[name](data);
}

function setLocalStorageItem(key, value) {
try {
window.localStorage.setItem(key, value);
} catch (e) {
// Do nothing. If we end up here, web storage quota exceeded, or user is
// in Safari's private browsing where localStorage's setItem is not
// available. See http://stackoverflow.com/q/14555347/1935861.
}
}

Handlebars.registerHelper(
"partial", function(id) {
return new Handlebars.SafeString(render(id, this));
Expand Down Expand Up @@ -125,7 +135,7 @@ $(function() {
}

if (data.token && window.localStorage.getItem("token") !== null) {
window.localStorage.setItem("token", data.token);
setLocalStorageItem("token", data.token);
}

passwordForm
Expand All @@ -144,7 +154,7 @@ $(function() {
}

if (data.token && $("#sign-in-remember").is(":checked")) {
window.localStorage.setItem("token", data.token);
setLocalStorageItem("token", data.token);
} else {
window.localStorage.removeItem("token");
}
Expand Down Expand Up @@ -506,7 +516,7 @@ $(function() {
options[name] = self.val();
}

window.localStorage.setItem("settings", JSON.stringify(options));
setLocalStorageItem("settings", JSON.stringify(options));

if ([
"join",
Expand Down Expand Up @@ -1005,7 +1015,7 @@ $(function() {
}
});
if (values.user) {
window.localStorage.setItem("user", values.user);
setLocalStorageItem("user", values.user);
}
socket.emit(
event, values
Expand Down

0 comments on commit 937cc4e

Please sign in to comment.