Catch errors on localStorage setting failure

Fixes https://github.com/rust-lang/rust/issues/81928

“Ask forgiveness not permission”  : this makes the code both simpler and more robust
This commit is contained in:
Ophir LOJKINE 2021-02-10 14:53:22 +01:00 committed by GitHub
parent 218bf8d765
commit f13bbea124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,35 +89,20 @@ function hasOwnProperty(obj, property) {
return Object.prototype.hasOwnProperty.call(obj, property);
}
function usableLocalStorage() {
// Check if the browser supports localStorage at all:
if (typeof Storage === "undefined") {
return false;
}
// Check if we can access it; this access will fail if the browser
// preferences deny access to localStorage, e.g., to prevent storage of
// "cookies" (or cookie-likes, as is the case here).
try {
return window.localStorage !== null && window.localStorage !== undefined;
} catch(err) {
// Storage is supported, but browser preferences deny access to it.
return false;
}
}
function updateLocalStorage(name, value) {
if (usableLocalStorage()) {
localStorage[name] = value;
} else {
// No Web Storage support so we do nothing
try {
window.localStorage.setItem(name, value);
} catch(e) {
// localStorage is not accessible, do nothing
}
}
function getCurrentValue(name) {
if (usableLocalStorage() && localStorage[name] !== undefined) {
return localStorage[name];
try {
window.localStorage.getItem(name);
} catch(e) {
return null;
}
return null;
}
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {