From 4f28301736d72353173ba30eff0856a3f26f66cb Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 5 Dec 2017 08:42:26 -0500 Subject: [PATCH] import fix to https://github.com/gorhill/uMatrix/issues/840 --- platform/chromium/vapi-common.js | 52 ++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 22 deletions(-) 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) { + } + } +}; /******************************************************************************/