Interactive interface for browsing global, full-resolution satellite imagery
Check localStorage safely. Fixes #474
Josiah Spraguefc8c415f6534
diff --git a/test/proj/wv.proj.change-test.js b/test/proj/wv.proj.change-test.jsindex 7ebd0f916..d5255fa04 100644--- a/test/proj/wv.proj.change-test.js+++ b/test/proj/wv.proj.change-test.js@@ -158,7 +158,7 @@ buster.testCase("wv.proj.change", {},"Not notified if 'Dont show again' selected": function() {- this.stub(wv.util, "localStorage")+ this.stub(window.localStorage, 'getItem').withArgs("arcticProjectionChangeNotification").returns("true");this.models.date.select(new Date(Date.UTC(2013, 0, 1)));diff --git a/web/js/notifications/wv.notifications.ui.js b/web/js/notifications/wv.notifications.ui.jsindex a5fc02850..d26ebe40f 100644--- a/web/js/notifications/wv.notifications.ui.js+++ b/web/js/notifications/wv.notifications.ui.js@@ -151,7 +151,7 @@ wv.notifications.ui = wv.notifications.ui || function(models, config) {* in LocalStorage*/var getNumberOfTypeNotseen = function(type, arra) {- var storageItem = wv.util.isInLocalStorage(type);+ var storageItem = wv.util.browser.localStorage?!!localStorage.getItem(type):false;var count, len;len = arra.length;@@ -185,7 +185,7 @@ wv.notifications.ui = wv.notifications.ui || function(models, config) {type = obj.notification_type;idString = obj.created_at.toString();- fieldExists = wv.util.isInLocalStorage(type);+ fieldExists = wv.util.browser.localStorage?!!localStorage.getItem(type):false;fieldValueMatches = false;if (fieldExists) {@@ -344,7 +344,7 @@ wv.notifications.ui = wv.notifications.ui || function(models, config) {var messages;this.className = 'ui-icon fa fa-fw fa-gift';self.messageIconActive = false;- wv.util.localStorage('message', activeMessageId);+ if (wv.util.browser.localStorage) localStorage.setItem('message', activeMessageId);activeMessageId = null;messages = sortedNotifications.messages[0];@@ -373,11 +373,11 @@ wv.notifications.ui = wv.notifications.ui || function(models, config) {self.notifyIconActive = false;mainNotification = null;createNotifyDialog();- if (activeNotifications.outage) {- wv.util.localStorage('outage', activeNotifications.outage);+ if (activeNotifications.outage && wv.util.browser.localStorage) {+ localStorage.setItem('outage', activeNotifications.outage);}- if (activeNotifications.alert) {- wv.util.localStorage('alert', activeNotifications.alert);+ if (activeNotifications.alert && wv.util.browser.localStorage) {+ localStorage.setItem('alert', activeNotifications.alert);}activeNotifications = {};if (self.messageIconActive) {@@ -486,6 +486,7 @@ wv.notifications.ui = wv.notifications.ui || function(models, config) {* @returns {Boolean}*/var localStorageValueMatches = function(property, value) {+ if (!wv.util.browser.localStorage) return;var oldValue = localStorage.getItem(property);return new Date(value) <= new Date(oldValue);};diff --git a/web/js/proj/wv.proj.change.js b/web/js/proj/wv.proj.change.jsindex 6be1e3408..30eeec776 100644--- a/web/js/proj/wv.proj.change.js+++ b/web/js/proj/wv.proj.change.js@@ -78,7 +78,7 @@ wv.proj.change = wv.proj.change || function(models) {if (!wv.util.browser.localStorage) {return;}- if (wv.util.localStorage(DO_NOT_SHOW_AGAIN) === "true") {+ if (localStorage.getItem(DO_NOT_SHOW_AGAIN) === "true") {return;}if (notified) {@@ -128,8 +128,8 @@ wv.proj.change = wv.proj.change || function(models) {wv.ui.notify(message, "Notice", 400);var $check = $("#arcticChangeNoticeDontShowAgain");$check.on("click", function() {- if ($check.is(":checked")) {- wv.util.localStorage(DO_NOT_SHOW_AGAIN, "true");+ if ($check.is(":checked") && wv.util.browser.localStorage) {+ localStorage.setItem(DO_NOT_SHOW_AGAIN, "true");}});};diff --git a/web/js/util/wv.util.browser.js b/web/js/util/wv.util.browser.jsindex b6bb63b81..3d6e0dd66 100644--- a/web/js/util/wv.util.browser.js+++ b/web/js/util/wv.util.browser.js@@ -91,7 +91,7 @@ wv.util.browser = wv.util.browser || (function() {* @attribute localStorage* @type Boolean*/- self.localStorage = true;+ self.localStorage = false;self.history = true;self.touchDevice = false;@@ -138,8 +138,12 @@ wv.util.browser = wv.util.browser || (function() {};self.tests.window = function(property) {- return window[property];-+ // Some browsers throw an error when attempting to access restricted Window objects+ try {+ return window[property];+ } catch (error) {+ return false;+ }};self.tests.safari = function() {@@ -204,6 +208,8 @@ wv.util.browser = wv.util.browser || (function() {}try {localStorage.setItem("available", "true");+ localStorage.getItem('available');+ localStorage.removeItem('available');} catch (error) {return false;}@@ -473,4 +479,4 @@ if (!String.prototype.endsWith) {}}, false);-})();\ No newline at end of file+})();diff --git a/web/js/util/wv.util.js b/web/js/util/wv.util.jsindex add7062d6..d09f8b52b 100644--- a/web/js/util/wv.util.js+++ b/web/js/util/wv.util.js@@ -639,16 +639,6 @@ wv.util = (function(self) {};};- self.localStorage = function(property, value) {- if (_.isUndefined(value)) {- return localStorage.getItem(property);- } else {- localStorage.setItem(property, value);- }- };- self.isInLocalStorage = function(property) {- return localStorage.getItem(property);- };/*** Wraps a function in a try/catch block that invokes wv.util.error* if an exception is thrown.@@ -766,14 +756,16 @@ wv.util = (function(self) {};self.setCoordinateFormat = function(type) {+ if (!wv.util.browser.localStorage) return;if (type !== "latlon-dd" && type !== "latlon-dms") {throw new Error("Invalid coordinate format: " + type);}- self.localStorage("coordinateFormat", type);+ localStorage.setItem('coordinateFormat', type);};self.getCoordinateFormat = function() {- return self.localStorage("coordinateFormat") || "latlon-dd";+ if (!wv.util.browser.localStorage) return 'latlon-dd';+ return localStorage.getItem("coordinateFormat") || "latlon-dd";};self.formatCoordinate = function(coord, format) {diff --git a/web/js/wv.tour.js b/web/js/wv.tour.jsindex daefa6c22..d669e8be5 100644--- a/web/js/wv.tour.js+++ b/web/js/wv.tour.js@@ -47,7 +47,7 @@ wv.tour = wv.tour || function(models, ui, config) {}// Don't show tour if the user has opted out- if (wv.util.localStorage("hideSplash")) {+ if (localStorage.getItem("hideSplash")) {return;}@@ -213,8 +213,9 @@ wv.tour = wv.tour || function(models, ui, config) {* Toggle the value of the "hideSplash" flag.*/var setDoNotShow = function() {- var hideSplash = wv.util.localStorage('hideSplash');- wv.util.localStorage('hideSplash', !hideSplash);+ if (!wv.util.browser.localStorage) return;+ var hideSplash = localStorage.getItem('hideSplash');+ localStorage.setItem('hideSplash', !hideSplash);};// assign events and start