Skip to content

Commit e2cae03

Browse files
committed
Accessibility checks are now made before trying to access local storage. Fixes #174.
1 parent c60ec7c commit e2cae03

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

src/web/App.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ App.prototype.initialiseSplitter = function() {
240240
App.prototype.loadLocalStorage = function() {
241241
// Load options
242242
let lOptions;
243-
if (localStorage.options !== undefined) {
243+
if (this.isLocalStorageAvailable() && localStorage.options !== undefined) {
244244
lOptions = JSON.parse(localStorage.options);
245245
}
246246
this.manager.options.load(lOptions);
@@ -256,13 +256,17 @@ App.prototype.loadLocalStorage = function() {
256256
* If the user currently has no saved favourites, the defaults from the view constructor are used.
257257
*/
258258
App.prototype.loadFavourites = function() {
259-
let favourites = localStorage.favourites &&
260-
localStorage.favourites.length > 2 ?
261-
JSON.parse(localStorage.favourites) :
262-
this.dfavourites;
263-
264-
favourites = this.validFavourites(favourites);
265-
this.saveFavourites(favourites);
259+
let favourites;
260+
261+
if (this.isLocalStorageAvailable()) {
262+
favourites = localStorage.favourites && localStorage.favourites.length > 2 ?
263+
JSON.parse(localStorage.favourites) :
264+
this.dfavourites;
265+
favourites = this.validFavourites(favourites);
266+
this.saveFavourites(favourites);
267+
} else {
268+
favourites = this.dfavourites;
269+
}
266270

267271
const favCat = this.categories.filter(function(c) {
268272
return c.name === "Favourites";
@@ -306,6 +310,15 @@ App.prototype.validFavourites = function(favourites) {
306310
* @param {string[]} favourites - A list of the user's favourite operations
307311
*/
308312
App.prototype.saveFavourites = function(favourites) {
313+
if (!this.isLocalStorageAvailable()) {
314+
this.alert(
315+
"Your security settings do not allow access to local storage so your favourites cannot be saved.",
316+
"danger",
317+
5000
318+
);
319+
return false;
320+
}
321+
309322
localStorage.setItem("favourites", JSON.stringify(this.validFavourites(favourites)));
310323
};
311324

@@ -503,6 +516,22 @@ App.prototype.setCompileMessage = function() {
503516
};
504517

505518

519+
/**
520+
* Determines whether the browser supports Local Storage and if it is accessible.
521+
*
522+
* @returns {boolean}
523+
*/
524+
App.prototype.isLocalStorageAvailable = function() {
525+
try {
526+
if (!localStorage) return false;
527+
return true;
528+
} catch (err) {
529+
// Access to LocalStorage is denied
530+
return false;
531+
}
532+
};
533+
534+
506535
/**
507536
* Pops up a message to the user and writes it to the console log.
508537
*

src/web/ControlsWaiter.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ ControlsWaiter.prototype.loadClick = function() {
254254
* Saves the recipe specified in the save textarea to local storage.
255255
*/
256256
ControlsWaiter.prototype.saveButtonClick = function() {
257+
if (!this.app.isLocalStorageAvailable()) {
258+
this.app.alert(
259+
"Your security settings do not allow access to local storage so your recipe cannot be saved.",
260+
"danger",
261+
5000
262+
);
263+
return false;
264+
}
265+
257266
const recipeName = Utils.escapeHtml(document.getElementById("save-name").value);
258267
const recipeStr = document.querySelector("#save-texts .tab-pane.active textarea").value;
259268

@@ -283,6 +292,8 @@ ControlsWaiter.prototype.saveButtonClick = function() {
283292
* Populates the list of saved recipes in the load dialog box from local storage.
284293
*/
285294
ControlsWaiter.prototype.populateLoadRecipesList = function() {
295+
if (!this.app.isLocalStorageAvailable()) return false;
296+
286297
const loadNameEl = document.getElementById("load-name");
287298

288299
// Remove current recipes from select
@@ -313,6 +324,8 @@ ControlsWaiter.prototype.populateLoadRecipesList = function() {
313324
* Removes the currently selected recipe from local storage.
314325
*/
315326
ControlsWaiter.prototype.loadDeleteClick = function() {
327+
if (!this.app.isLocalStorageAvailable()) return false;
328+
316329
const id = parseInt(document.getElementById("load-name").value, 10);
317330
const rawSavedRecipes = localStorage.savedRecipes ?
318331
JSON.parse(localStorage.savedRecipes) : [];
@@ -328,6 +341,8 @@ ControlsWaiter.prototype.loadDeleteClick = function() {
328341
* Displays the selected recipe in the load text box.
329342
*/
330343
ControlsWaiter.prototype.loadNameChange = function(e) {
344+
if (!this.app.isLocalStorageAvailable()) return false;
345+
331346
const el = e.target;
332347
const savedRecipes = localStorage.savedRecipes ?
333348
JSON.parse(localStorage.savedRecipes) : [];

src/web/OperationsWaiter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ OperationsWaiter.prototype.editFavouritesClick = function(e) {
229229
filter: ".remove-icon",
230230
onFilter: function (evt) {
231231
const el = editableList.closest(evt.item);
232-
if (el) {
232+
if (el && el.parentNode) {
233233
$(el).popover("destroy");
234234
el.parentNode.removeChild(el);
235235
}

src/web/OptionsWaiter.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ OptionsWaiter.prototype.switchChange = function(e, state) {
8787
const option = el.getAttribute("option");
8888

8989
this.app.options[option] = state;
90-
localStorage.setItem("options", JSON.stringify(this.app.options));
90+
91+
if (this.app.isLocalStorageAvailable())
92+
localStorage.setItem("options", JSON.stringify(this.app.options));
9193
};
9294

9395

@@ -102,7 +104,9 @@ OptionsWaiter.prototype.numberChange = function(e) {
102104
const option = el.getAttribute("option");
103105

104106
this.app.options[option] = parseInt(el.value, 10);
105-
localStorage.setItem("options", JSON.stringify(this.app.options));
107+
108+
if (this.app.isLocalStorageAvailable())
109+
localStorage.setItem("options", JSON.stringify(this.app.options));
106110
};
107111

108112

@@ -117,7 +121,9 @@ OptionsWaiter.prototype.selectChange = function(e) {
117121
const option = el.getAttribute("option");
118122

119123
this.app.options[option] = el.value;
120-
localStorage.setItem("options", JSON.stringify(this.app.options));
124+
125+
if (this.app.isLocalStorageAvailable())
126+
localStorage.setItem("options", JSON.stringify(this.app.options));
121127
};
122128

123129

src/web/html/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
"use strict";
3636

3737
// Load theme before the preloader is shown
38-
document.querySelector(":root").className = (JSON.parse(localStorage.getItem("options")) || {}).theme;
38+
try {
39+
document.querySelector(":root").className = (JSON.parse(localStorage.getItem("options")) || {}).theme;
40+
} catch (err) {
41+
// LocalStorage access is denied by security settings
42+
}
3943

4044
// Define loading messages
4145
const loadingMsgs = [

0 commit comments

Comments
 (0)