catch SecurityError with localStorage, fixes #47

This commit is contained in:
Norbert Renner 2016-10-05 11:49:54 +02:00
parent b108efae5c
commit 514cc705ef
3 changed files with 26 additions and 4 deletions

View file

@ -60,7 +60,7 @@ BR.Map = {
map = new L.Map('map', {
worldCopyJump: true
});
if (!map.restoreView()) {
if (!BR.Util.localStorageAvailable() || !map.restoreView()) {
map.setView([50.99, 9.86], 6);
}
map.attributionControl.addAttribution(

View file

@ -29,6 +29,26 @@ BR.Util = {
msg = xhr.status + ': ' + xhr.statusText;
}
return new Error(msg);
}
},
// check if localStorage is available, especially for catching SecurityError
// when cookie settings are blocking access (Chrome, Pale Moon, older Firefox)
//
// see also https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js
//
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Testing_for_support_vs_availability
// by Mozilla Contributors, with modifications;
// Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
localStorageAvailable: function() {
try {
var storage = window.localStorage,
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
}
};

View file

@ -7,7 +7,7 @@ BR.OpacitySlider = L.Control.extend({
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-bar control-slider'),
input = $('<input id="slider" type="text"/>'),
item = localStorage.opacitySliderValue,
item = BR.Util.localStorageAvailable() ? localStorage.opacitySliderValue : null,
value = item ? parseInt(item) : BR.conf.defaultOpacity * 100,
minOpacity = (BR.conf.minOpacity || 0) * 100;
@ -39,7 +39,9 @@ BR.OpacitySlider = L.Control.extend({
}).on('slide slideStop', { self: this }, function (evt) {
evt.data.self.options.callback(evt.value / 100);
}).on('slideStop', function (evt) {
if (BR.Util.localStorageAvailable()) {
localStorage.opacitySliderValue = evt.value;
}
// When dragging outside slider and over map, click event after mouseup
// adds marker when active on Chromium. So disable click (not needed)