From 3ff2926871e3c002b6eff329f1a50017547e4f8f Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 4 Dec 2017 16:48:59 -0500 Subject: [PATCH] fix #840 (need confirmation) --- platform/chromium/manifest.json | 2 +- platform/chromium/vapi-common.js | 52 ++++++++++++++++++-------------- src/js/messaging.js | 19 +++++++++--- src/js/popup.js | 8 ++--- src/js/start.js | 8 ++--- 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/platform/chromium/manifest.json b/platform/chromium/manifest.json index d76a1a5..b855fe6 100644 --- a/platform/chromium/manifest.json +++ b/platform/chromium/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "uMatrix", "short_name": "uMatrix", - "version": "1.1.14", + "version": "1.1.16", "description": "__MSG_extShortDesc__", "icons": { "16": "img/icon_16.png", diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index fa13031..3591c19 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -85,31 +85,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 = self.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) { + } + } +}; /******************************************************************************/ diff --git a/src/js/messaging.js b/src/js/messaging.js index 340ae0a..797abd0 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -489,13 +489,22 @@ var evaluateURLs = function(tabId, requests) { } if ( placeholders === null ) { - var bg = vAPI.localStorage.getItem('placeholderBackground'); placeholders = { - background: bg, - border: vAPI.localStorage.getItem('placeholderBorder'), - iframe: vAPI.localStorage.getItem('placeholderDocument').replace('{{bg}}', bg), - img: vAPI.localStorage.getItem('placeholderImage') + background: + vAPI.localStorage.getItem('placeholderBackground') || + µm.defaultLocalUserSettings.placeholderBackground, + border: + vAPI.localStorage.getItem('placeholderBorder') || + µm.defaultLocalUserSettings.placeholderBorder, + iframe: + vAPI.localStorage.getItem('placeholderDocument') || + µm.defaultLocalUserSettings.placeholderDocument, + img: + vAPI.localStorage.getItem('placeholderImage') || + µm.defaultLocalUserSettings.placeholderImage }; + placeholders.iframe = + placeholders.iframe.replace('{{bg}}', placeholders.background); } response.placeholders = placeholders; diff --git a/src/js/popup.js b/src/js/popup.js index e7f72c0..6410a2c 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -35,8 +35,8 @@ // Stuff which is good to do very early so as to avoid visual glitches. (function() { - var paneContentPaddingTop = localStorage.getItem('paneContentPaddingTop'), - touchDevice = localStorage.getItem('touchDevice'); + var paneContentPaddingTop = vAPI.localStorage.getItem('paneContentPaddingTop'), + touchDevice = vAPI.localStorage.getItem('touchDevice'); if ( typeof paneContentPaddingTop === 'string' ) { document.querySelector('.paneContent').style.setProperty( @@ -50,7 +50,7 @@ document.addEventListener('touchstart', function onTouched(ev) { document.removeEventListener(ev.type, onTouched); document.body.setAttribute('data-touch', 'true'); - localStorage.setItem('touchDevice', 'true'); + vAPI.localStorage.setItem('touchDevice', 'true'); resizePopup(); }); } @@ -71,7 +71,7 @@ var resizePopup = (function() { paneContent = doc.querySelector('.paneContent'); if ( paddingTop !== paneContent.style.paddingTop ) { paneContent.style.setProperty('padding-top', paddingTop); - localStorage.setItem('paneContentPaddingTop', paddingTop); + vAPI.localStorage.setItem('paneContentPaddingTop', paddingTop); } document.body.classList.toggle( 'hConstrained', diff --git a/src/js/start.js b/src/js/start.js index 25f5259..d19cc62 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -54,7 +54,7 @@ var µm = µMatrix; */ -var defaultLocalUserSettings = { +µm.defaultLocalUserSettings = { // data-URI background courtesy of https://github.com/dev-random // https://github.com/gorhill/uMatrix/issues/429#issuecomment-194548243 placeholderBackground: [ @@ -135,15 +135,15 @@ var onAllDone = function() { µm.assets.addObserver(µm.assetObserver.bind(µm)); µm.scheduleAssetUpdater(µm.userSettings.autoUpdate ? 7 * 60 * 1000 : 0); - for ( var key in defaultLocalUserSettings ) { - if ( defaultLocalUserSettings.hasOwnProperty(key) === false ) { + for ( var key in µm.defaultLocalUserSettings ) { + if (µm. defaultLocalUserSettings.hasOwnProperty(key) === false ) { continue; } if ( vAPI.localStorage.getItem(key) === null || rwLocalUserSettings.hasOwnProperty(key) === false ) { - vAPI.localStorage.setItem(key, defaultLocalUserSettings[key]); + vAPI.localStorage.setItem(key, µm.defaultLocalUserSettings[key]); } }