From 2936d73911813bfbcde57e6c5bb20f2400cb0dc3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 22 Dec 2019 11:03:14 -0500 Subject: [PATCH] Fix odds and ends from mega commit 9b292304d33a Related commit: - https://github.com/gorhill/uMatrix/commit/9b292304d33a --- src/css/popup.css | 1 + src/js/background.js | 2 +- src/js/start.js | 25 +++++-- src/js/storage.js | 156 +++++++++++++++---------------------------- src/js/utils.js | 12 ++++ src/popup.html | 2 +- 6 files changed, 89 insertions(+), 109 deletions(-) diff --git a/src/css/popup.css b/src/css/popup.css index 62edd2f..bdf3309 100644 --- a/src/css/popup.css +++ b/src/css/popup.css @@ -385,6 +385,7 @@ body.tabless .needtab { justify-content: flex-end; unicode-bidi: embed; width: 16em; + word-break: keep-all; } .matrix .matGroup.g4 .matRow.ro > .matCell:first-child { direction: inherit; diff --git a/src/js/background.js b/src/js/background.js index db50185..f5cb891 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -166,7 +166,7 @@ return { } }, - rawSettingsDefault: rawSettingsDefault, + rawSettingsDefault, rawSettings: (( ) => { const out = Object.assign({}, rawSettingsDefault); const json = vAPI.localStorage.getItem('immediateRawSettings'); diff --git a/src/js/start.js b/src/js/start.js index 05a9178..fa1d7a0 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -27,10 +27,22 @@ const µm = µMatrix; await Promise.all([ - µm.loadPublicSuffixList(), + µm.loadRawSettings(), µm.loadUserSettings(), ]); - log.info(`PSL and user settings ready ${Date.now()-vAPI.T0} ms after launch`); + log.info(`User settings ready ${Date.now()-vAPI.T0} ms after launch`); + + const shouldWASM = µm.rawSettings.disableWebAssembly !== true; + if ( shouldWASM ) { + await Promise.all([ + µm.HNTrieContainer.enableWASM(), + self.publicSuffixList.enableWASM(), + ]); + log.info(`WASM modules ready ${Date.now()-vAPI.T0} ms after launch`); + } + + await µm.loadPublicSuffixList(), + log.info(`PSL ready ${Date.now()-vAPI.T0} ms after launch`); { let trieDetails; @@ -41,16 +53,17 @@ } catch(ex) { } µm.ubiquitousBlacklist = new µm.HNTrieContainer(trieDetails); - µm.ubiquitousBlacklist.initWASM(); + if ( shouldWASM ) { + µm.ubiquitousBlacklist.initWASM(); + } } - log.info(`Ubiquitous block container ready ${Date.now()-vAPI.T0} ms after launch`); + log.info(`Ubiquitous block rules container ready ${Date.now()-vAPI.T0} ms after launch`); await Promise.all([ - µm.loadRawSettings(), µm.loadMatrix(), µm.loadHostsFiles(), ]); - log.info(`Ubiquitous block rules ready ${Date.now()-vAPI.T0} ms after launch`); + log.info(`All rules ready ${Date.now()-vAPI.T0} ms after launch`); { const pageStore = diff --git a/src/js/storage.js b/src/js/storage.js index 42241e3..78ababc 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -158,7 +158,7 @@ this.rawSettings.suspendTabsUntilReady ? 'yes' : 'unset'; } } - this.fireDOMEvent('hiddenSettingsChanged'); + this.fireDOMEvent('rawSettingsChanged'); }; // Note: Save only the settings which values differ from the default ones. @@ -175,11 +175,11 @@ bin.rawSettings[prop] = this.rawSettings[prop]; } } - vAPI.storage.set(bin); this.saveImmediateHiddenSettings(); + return vAPI.storage.set(bin); }; -self.addEventListener('hiddenSettingsChanged', ( ) => { +self.addEventListener('rawSettingsChanged', ( ) => { const µm = µMatrix; self.log.verbosity = µm.rawSettings.consoleLogLevel; vAPI.net.setOptions({ @@ -192,113 +192,56 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { }); }); -// These settings must be available immediately on startup, without delay -// through the vAPI.localStorage. Add/remove settings as needed. - -µMatrix.saveImmediateHiddenSettings = function() { - const props = [ - 'consoleLogLevel', - 'disableWebAssembly', - 'suspendTabsUntilReady', - ]; - const toSave = {}; - for ( const prop of props ) { - if ( this.rawSettings[prop] !== this.rawSettingsDefault[prop] ) { - toSave[prop] = this.rawSettings[prop]; - } - } - if ( Object.keys(toSave).length !== 0 ) { - vAPI.localStorage.setItem( - 'immediateHiddenSettings', - JSON.stringify(toSave) - ); - } else { - vAPI.localStorage.removeItem('immediateHiddenSettings'); - } -}; - - - - -µMatrix.loadRawSettings = async function() { - const bin = await vAPI.storage.get('rawSettings'); - if ( - bin instanceof Object === false || - bin.rawSettings instanceof Object === false - ) { - return; - } - for ( const key of Object.keys(bin.rawSettings) ) { - if ( - this.rawSettings.hasOwnProperty(key) === false || - typeof bin.rawSettings[key] !== typeof this.rawSettings[key] - ) { - continue; - } - this.rawSettings[key] = bin.rawSettings[key]; - } - this.rawSettingsWriteTime = Date.now(); -}; - -µMatrix.saveRawSettings = async function(rawSettings) { - const keys = Object.keys(rawSettings); - if ( keys.length === 0 ) { return; } - - for ( const key of keys ) { - if ( - this.rawSettingsDefault.hasOwnProperty(key) && - typeof rawSettings[key] === typeof this.rawSettingsDefault[key] - ) { - this.rawSettings[key] = rawSettings[key]; - } - } - this.saveImmediateHiddenSettings(); - this.rawSettingsWriteTime = Date.now(); +self.addEventListener('rawSettingsChanged', ( ) => { + const µm = µMatrix; + self.log.verbosity = µm.rawSettings.consoleLogLevel; + vAPI.net.setOptions({ + cnameIgnoreList: µm.rawSettings.cnameIgnoreList, + cnameIgnore1stParty: µm.rawSettings.cnameIgnore1stParty, + cnameIgnoreExceptions: µm.rawSettings.cnameIgnoreExceptions, + cnameIgnoreRootDocument: µm.rawSettings.cnameIgnoreRootDocument, + cnameMaxTTL: µm.rawSettings.cnameMaxTTL, + cnameReplayFullURL: µm.rawSettings.cnameReplayFullURL, + }); +}); - await vAPI.storage.set({ rawSettings: this.rawSettings }); -}; +/******************************************************************************/ µMatrix.rawSettingsFromString = function(raw) { - const result = {}; + const out = Object.assign({}, this.rawSettingsDefault); const lineIter = new this.LineIterator(raw); while ( lineIter.eot() === false ) { - const line = lineIter.next().trim(); - const matches = /^(\S+)(\s+(.+))?$/.exec(line); - if ( matches === null ) { continue; } + const line = lineIter.next(); + const matches = /^\s*(\S+)\s+(.+)$/.exec(line); + if ( matches === null || matches.length !== 3 ) { continue; } const name = matches[1]; - if ( this.rawSettingsDefault.hasOwnProperty(name) === false ) { - continue; - } - let value = (matches[2] || '').trim(); - switch ( typeof this.rawSettingsDefault[name] ) { + if ( out.hasOwnProperty(name) === false ) { continue; } + const value = matches[2].trim(); + switch ( typeof out[name] ) { case 'boolean': if ( value === 'true' ) { - value = true; + out[name] = true; } else if ( value === 'false' ) { - value = false; - } else { - value = this.rawSettingsDefault[name]; + out[name] = false; } break; case 'string': - if ( value === '' ) { - value = this.rawSettingsDefault[name]; - } + out[name] = value; break; - case 'number': - value = parseInt(value, 10); - if ( isNaN(value) ) { - value = this.rawSettingsDefault[name]; + case 'number': { + const i = parseInt(value, 10); + if ( isNaN(i) === false ) { + out[name] = i; } break; + } default: break; } - if ( this.rawSettings[name] !== value ) { - result[name] = value; - } } - this.saveRawSettings(result); + this.rawSettings = out; + this.saveRawSettings(); + this.fireDOMEvent('rawSettingsChanged'); }; µMatrix.stringFromRawSettings = function() { @@ -309,16 +252,30 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { return out.join('\n'); }; +/******************************************************************************/ + // These settings must be available immediately on startup, without delay // through the vAPI.localStorage. Add/remove settings as needed. µMatrix.saveImmediateHiddenSettings = function() { - vAPI.localStorage.setItem( - 'immediateRawSettings', - JSON.stringify({ - suspendTabsUntilReady: this.rawSettings.suspendTabsUntilReady, - }) - ); + const props = [ + 'consoleLogLevel', + 'suspendTabsUntilReady', + ]; + const toSave = {}; + for ( const prop of props ) { + if ( this.rawSettings[prop] !== this.rawSettingsDefault[prop] ) { + toSave[prop] = this.rawSettings[prop]; + } + } + if ( Object.keys(toSave).length !== 0 ) { + vAPI.localStorage.setItem( + 'immediateHiddenSettings', + JSON.stringify(toSave) + ); + } else { + vAPI.localStorage.removeItem('immediateHiddenSettings'); + } }; /******************************************************************************/ @@ -816,6 +773,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { ); µm.ubiquitousBlacklistRef = µm.ubiquitousBlacklist.createOne(bin.hostsFilesSelfie.trieref); + return true; }, cancel: function() { if ( timer !== undefined ) { @@ -832,10 +790,6 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { // TODO: remove once all users are way past 1.4.0. this.cacheStorage.remove('publicSuffixListSelfie'); - if ( this.rawSettings.disableWebAssembly === false ) { - publicSuffixList.enableWASM(); - } - try { const result = await this.assets.get(`compiled/${this.pslAssetKey}`); if ( publicSuffixList.fromSelfie(result.content, this.base64) ) { diff --git a/src/js/utils.js b/src/js/utils.js index 5a0e75f..df492c9 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -227,3 +227,15 @@ })(); /******************************************************************************/ + +µMatrix.fireDOMEvent = function(name) { + if ( + window instanceof Object && + window.dispatchEvent instanceof Function && + window.CustomEvent instanceof Function + ) { + window.dispatchEvent(new CustomEvent(name)); + } +}; + +/******************************************************************************/ diff --git a/src/popup.html b/src/popup.html index 0194103..3523495 100644 --- a/src/popup.html +++ b/src/popup.html @@ -72,7 +72,7 @@
  • info-circle
  • info-circle
  • info-circle -
  • info-circle +
  • info-circle