diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index ab814424f..007313dea 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -97,31 +97,39 @@ vAPI.closePopup = function() { // This storage is optional, but it is nice to have, for a more polished user // experience. -// This can throw in some contexts (like in devtool). -try { - vAPI.localStorage = window.localStorage; -} catch (ex) { -} - // https://github.com/gorhill/uBlock/issues/2824 // Use a dummy localStorage if for some reasons it's not available. -if ( vAPI.localStorage instanceof Object === false ) { - vAPI.localStorage = { - length: 0, - clear: function() { - }, - getItem: function() { - return null; - }, - key: function() { - throw new RangeError(); - }, - removeItem: function() { - }, - setItem: function() { + +// https://github.com/gorhill/uMatrix/issues/840 +// Always use a wrapper to seamlessly handle exceptions + +vAPI.localStorage = { + clear: function() { + try { + window.localStorage.clear(); + } catch(ex) { } - }; -} + }, + getItem: function(key) { + try { + return window.localStorage.getItem(key); + } catch(ex) { + } + return null; + }, + removeItem: function(key) { + try { + window.localStorage.removeItem(key); + } catch(ex) { + } + }, + setItem: function(key, value) { + try { + window.localStorage.setItem(key, value); + } catch(ex) { + } + } +}; /******************************************************************************/