Accessibility checks are now made before trying to access local storage. Fixes #174.

This commit is contained in:
n1474335 2017-09-20 22:26:47 +01:00
parent c60ec7c170
commit e2cae035f2
5 changed files with 66 additions and 12 deletions

View File

@ -240,7 +240,7 @@ App.prototype.initialiseSplitter = function() {
App.prototype.loadLocalStorage = function() {
// Load options
let lOptions;
if (localStorage.options !== undefined) {
if (this.isLocalStorageAvailable() && localStorage.options !== undefined) {
lOptions = JSON.parse(localStorage.options);
}
this.manager.options.load(lOptions);
@ -256,13 +256,17 @@ App.prototype.loadLocalStorage = function() {
* If the user currently has no saved favourites, the defaults from the view constructor are used.
*/
App.prototype.loadFavourites = function() {
let favourites = localStorage.favourites &&
localStorage.favourites.length > 2 ?
JSON.parse(localStorage.favourites) :
this.dfavourites;
let favourites;
favourites = this.validFavourites(favourites);
this.saveFavourites(favourites);
if (this.isLocalStorageAvailable()) {
favourites = localStorage.favourites && localStorage.favourites.length > 2 ?
JSON.parse(localStorage.favourites) :
this.dfavourites;
favourites = this.validFavourites(favourites);
this.saveFavourites(favourites);
} else {
favourites = this.dfavourites;
}
const favCat = this.categories.filter(function(c) {
return c.name === "Favourites";
@ -306,6 +310,15 @@ App.prototype.validFavourites = function(favourites) {
* @param {string[]} favourites - A list of the user's favourite operations
*/
App.prototype.saveFavourites = function(favourites) {
if (!this.isLocalStorageAvailable()) {
this.alert(
"Your security settings do not allow access to local storage so your favourites cannot be saved.",
"danger",
5000
);
return false;
}
localStorage.setItem("favourites", JSON.stringify(this.validFavourites(favourites)));
};
@ -503,6 +516,22 @@ App.prototype.setCompileMessage = function() {
};
/**
* Determines whether the browser supports Local Storage and if it is accessible.
*
* @returns {boolean}
*/
App.prototype.isLocalStorageAvailable = function() {
try {
if (!localStorage) return false;
return true;
} catch (err) {
// Access to LocalStorage is denied
return false;
}
};
/**
* Pops up a message to the user and writes it to the console log.
*

View File

@ -254,6 +254,15 @@ ControlsWaiter.prototype.loadClick = function() {
* Saves the recipe specified in the save textarea to local storage.
*/
ControlsWaiter.prototype.saveButtonClick = function() {
if (!this.app.isLocalStorageAvailable()) {
this.app.alert(
"Your security settings do not allow access to local storage so your recipe cannot be saved.",
"danger",
5000
);
return false;
}
const recipeName = Utils.escapeHtml(document.getElementById("save-name").value);
const recipeStr = document.querySelector("#save-texts .tab-pane.active textarea").value;
@ -283,6 +292,8 @@ ControlsWaiter.prototype.saveButtonClick = function() {
* Populates the list of saved recipes in the load dialog box from local storage.
*/
ControlsWaiter.prototype.populateLoadRecipesList = function() {
if (!this.app.isLocalStorageAvailable()) return false;
const loadNameEl = document.getElementById("load-name");
// Remove current recipes from select
@ -313,6 +324,8 @@ ControlsWaiter.prototype.populateLoadRecipesList = function() {
* Removes the currently selected recipe from local storage.
*/
ControlsWaiter.prototype.loadDeleteClick = function() {
if (!this.app.isLocalStorageAvailable()) return false;
const id = parseInt(document.getElementById("load-name").value, 10);
const rawSavedRecipes = localStorage.savedRecipes ?
JSON.parse(localStorage.savedRecipes) : [];
@ -328,6 +341,8 @@ ControlsWaiter.prototype.loadDeleteClick = function() {
* Displays the selected recipe in the load text box.
*/
ControlsWaiter.prototype.loadNameChange = function(e) {
if (!this.app.isLocalStorageAvailable()) return false;
const el = e.target;
const savedRecipes = localStorage.savedRecipes ?
JSON.parse(localStorage.savedRecipes) : [];

View File

@ -229,7 +229,7 @@ OperationsWaiter.prototype.editFavouritesClick = function(e) {
filter: ".remove-icon",
onFilter: function (evt) {
const el = editableList.closest(evt.item);
if (el) {
if (el && el.parentNode) {
$(el).popover("destroy");
el.parentNode.removeChild(el);
}

View File

@ -87,7 +87,9 @@ OptionsWaiter.prototype.switchChange = function(e, state) {
const option = el.getAttribute("option");
this.app.options[option] = state;
localStorage.setItem("options", JSON.stringify(this.app.options));
if (this.app.isLocalStorageAvailable())
localStorage.setItem("options", JSON.stringify(this.app.options));
};
@ -102,7 +104,9 @@ OptionsWaiter.prototype.numberChange = function(e) {
const option = el.getAttribute("option");
this.app.options[option] = parseInt(el.value, 10);
localStorage.setItem("options", JSON.stringify(this.app.options));
if (this.app.isLocalStorageAvailable())
localStorage.setItem("options", JSON.stringify(this.app.options));
};
@ -117,7 +121,9 @@ OptionsWaiter.prototype.selectChange = function(e) {
const option = el.getAttribute("option");
this.app.options[option] = el.value;
localStorage.setItem("options", JSON.stringify(this.app.options));
if (this.app.isLocalStorageAvailable())
localStorage.setItem("options", JSON.stringify(this.app.options));
};

View File

@ -35,7 +35,11 @@
"use strict";
// Load theme before the preloader is shown
document.querySelector(":root").className = (JSON.parse(localStorage.getItem("options")) || {}).theme;
try {
document.querySelector(":root").className = (JSON.parse(localStorage.getItem("options")) || {}).theme;
} catch (err) {
// LocalStorage access is denied by security settings
}
// Define loading messages
const loadingMsgs = [