diff --git a/src/js/messaging-handlers.js b/src/js/messaging-handlers.js index 7673529..a6560a6 100644 --- a/src/js/messaging-handlers.js +++ b/src/js/messaging-handlers.js @@ -439,6 +439,54 @@ var onMessage = function(request, sender, callback) { /******************************************************************************/ /******************************************************************************/ +// privacy.js + +(function() { + +var onMessage = function(request, sender, callback) { + var µm = µMatrix; + + // Async + switch ( request.what ) { + default: + break; + } + + // Sync + var response; + + switch ( request.what ) { + case 'getPrivacySettings': + response = { + userSettings: µm.userSettings, + matrixSwitches: { + 'https-strict': µm.pMatrix.evaluateSwitch('https-strict', '*') === 1, + 'ua-spoof': µm.pMatrix.evaluateSwitch('ua-spoof', '*') === 1, + } + }; + break; + + case 'setMatrixSwitch': + µm.tMatrix.setSwitch(request.switchName, '*', request.state); + if ( µm.pMatrix.setSwitch(request.switchName, '*', request.state) ) { + µm.saveMatrix(); + } + break; + + default: + return µm.messaging.defaultHandler(request, sender, callback); + } + + callback(response); +}; + +µMatrix.messaging.listen('privacy.js', onMessage); + +})(); + +/******************************************************************************/ +/******************************************************************************/ + // user-rules.js (function() { diff --git a/src/js/privacy.js b/src/js/privacy.js index fdc18d3..4ffd6c5 100644 --- a/src/js/privacy.js +++ b/src/js/privacy.js @@ -19,6 +19,8 @@ Home: https://github.com/gorhill/uMatrix */ +/* global messaging, uDom */ + /******************************************************************************/ (function() { @@ -27,7 +29,7 @@ messaging.start('privacy.js'); -var cachedUserSettings = {}; +var cachedPrivacySettings = {}; /******************************************************************************/ @@ -41,8 +43,18 @@ function changeUserSettings(name, value) { /******************************************************************************/ +function changeMatrixSwitch(name, state) { + messaging.tell({ + what: 'setMatrixSwitch', + switchName: name, + state: state + }); +} + +/******************************************************************************/ + function onChangeValueHandler(uelem, setting, min, max) { - var oldVal = cachedUserSettings[setting]; + var oldVal = cachedPrivacySettings.userSettings[setting]; var newVal = Math.round(parseFloat(uelem.val())); if ( typeof newVal !== 'number' ) { newVal = oldVal; @@ -67,33 +79,26 @@ function prepareToDie() { /******************************************************************************/ var installEventHandlers = function() { - uDom('#delete-unused-session-cookies').on('change', function(){ - changeUserSettings('deleteUnusedSessionCookies', this.checked); + uDom('[data-setting-bool]').on('change', function(){ + var settingName = this.getAttribute('data-setting-bool'); + if ( typeof settingName === 'string' && settingName !== '' ) { + changeUserSettings(settingName, this.checked); + } }); + + uDom('[data-matrix-switch]').on('change', function(){ + var switchName = this.getAttribute('data-matrix-switch'); + if ( typeof switchName === 'string' && switchName !== '' ) { + changeMatrixSwitch(switchName, this.checked); + } + }); + uDom('#delete-unused-session-cookies-after').on('change', function(){ onChangeValueHandler(uDom(this), 'deleteUnusedSessionCookiesAfter', 15, 1440); }); - uDom('#delete-blacklisted-cookies').on('change', function(){ - changeUserSettings('deleteCookies', this.checked); - }); - uDom('#delete-blacklisted-localstorage').on('change', function(){ - changeUserSettings('deleteLocalStorage', this.checked); - }); - uDom('#clear-browser-cache').on('change', function(){ - changeUserSettings('clearBrowserCache', this.checked); - }); uDom('#clear-browser-cache-after').on('change', function(){ onChangeValueHandler(uDom(this), 'clearBrowserCacheAfter', 15, 1440); }); - uDom('#process-referer').on('change', function(){ - changeUserSettings('processReferer', this.checked); - }); - uDom('#process-hyperlink-auditing').on('change', function(){ - changeUserSettings('processHyperlinkAuditing', this.checked); - }); - uDom('#spoof-user-agent').on('change', function(){ - changeUserSettings('spoofUserAgent', this.checked); - }); uDom('#spoof-user-agent-every').on('change', function(){ onChangeValueHandler(uDom(this), 'spoofUserAgentEvery', 2, 999); }); @@ -108,25 +113,35 @@ var installEventHandlers = function() { /******************************************************************************/ uDom.onLoad(function() { - var onUserSettingsReceived = function(userSettings) { + var onSettingsReceived = function(privacySettings) { // Cache copy - cachedUserSettings = userSettings; + cachedPrivacySettings = privacySettings; + + var userSettings = privacySettings.userSettings; + var matrixSwitches = privacySettings.matrixSwitches; + + uDom('[data-setting-bool]').forEach(function(elem){ + var settingName = elem.attr('data-setting-bool'); + if ( typeof settingName === 'string' && settingName !== '' ) { + elem.prop('checked', userSettings[settingName] === true); + } + }); + + uDom('[data-matrix-switch]').forEach(function(elem){ + var switchName = elem.attr('data-matrix-switch'); + if ( typeof switchName === 'string' && switchName !== '' ) { + elem.prop('checked', matrixSwitches[switchName] === true); + } + }); - uDom('#delete-unused-session-cookies').prop('checked', userSettings.deleteUnusedSessionCookies === true); uDom('#delete-unused-session-cookies-after').val(userSettings.deleteUnusedSessionCookiesAfter); - uDom('#delete-blacklisted-cookies').prop('checked', userSettings.deleteCookies === true); - uDom('#delete-blacklisted-localstorage').prop('checked', userSettings.deleteLocalStorage); - uDom('#clear-browser-cache').prop('checked', userSettings.clearBrowserCache === true); uDom('#clear-browser-cache-after').val(userSettings.clearBrowserCacheAfter); - uDom('#process-referer').prop('checked', userSettings.processReferer); - uDom('#process-hyperlink-auditing').prop('checked', userSettings.processHyperlinkAuditing); - uDom('#spoof-user-agent').prop('checked', userSettings.spoofUserAgent); uDom('#spoof-user-agent-every').val(userSettings.spoofUserAgentEvery); uDom('#spoof-user-agent-with').val(userSettings.spoofUserAgentWith); installEventHandlers(); }; - messaging.ask({ what: 'getUserSettings' }, onUserSettingsReceived); + messaging.ask({ what: 'getPrivacySettings' }, onSettingsReceived); }); /******************************************************************************/ diff --git a/src/js/storage.js b/src/js/storage.js index 07c36cd..dfe8943 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -83,7 +83,10 @@ /******************************************************************************/ -µMatrix.loadMatrix = function() { +µMatrix.loadMatrix = function(callback) { + if ( typeof callback !== 'function' ) { + callback = this.noopFunc; + } var µm = this; var onLoaded = function(bin) { if ( bin.hasOwnProperty('userMatrix') ) { @@ -95,6 +98,7 @@ µm.pMatrix.setCell('*', '*', 'doc', µm.Matrix.Green); µm.saveMatrix(); µm.tMatrix.assign(µm.pMatrix); + callback(); } }; this.XAL.keyvalGetOne('userMatrix', onLoaded); @@ -396,9 +400,28 @@ } var µm = this; + var settingsReady = false; + var matrixReady = false; - // User settings are in memory - var onUserSettingsReady = function(settings) { + // TODO: to remove when everybody (and their backup file) has their + // ua-spoof setting converted into a matrix switch. + var onSettingsAndMatrixReady = function() { + if ( !settingsReady || !matrixReady ) { + return; + } + if ( µm.userSettings.hasOwnProperty('spoofUserAgent') === false ) { + return; + } + if ( µm.userSettings.spoofUserAgent ) { + µm.tMatrix.setSwitch('ua-spoof', '*', 1); + µm.pMatrix.setSwitch('ua-spoof', '*', 1); + µm.saveMatrix(); + } + delete µm.userSettings.spoofUserAgent; + µm.saveUserSettings(); + }; + + var onSettingsReady = function(settings) { // Never auto-update at boot time µm.loadUpdatableAssets(false, callback); @@ -406,9 +429,16 @@ if ( settings.autoUpdate ) { µm.updater.restart(µm.firstUpdateAfter); } + settingsReady = true; + onSettingsAndMatrixReady(); }; - this.loadUserSettings(onUserSettingsReady); - this.loadMatrix(); + var onMatrixReady = function() { + matrixReady = true; + onSettingsAndMatrixReady(); + }; + + this.loadUserSettings(onSettingsReady); + this.loadMatrix(onMatrixReady); this.getBytesInUse(); }; diff --git a/src/popup.html b/src/popup.html index 7a6d4ed..8c3cb9a 100644 --- a/src/popup.html +++ b/src/popup.html @@ -33,8 +33,8 @@ diff --git a/src/privacy.html b/src/privacy.html index 1df9ebe..6628d17 100644 --- a/src/privacy.html +++ b/src/privacy.html @@ -46,11 +46,11 @@ html.rtl #spoof-user-agent-with {