Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure localStorage cannot fail because of quota or Safari private browsing #625

Merged
merged 1 commit into from Sep 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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