From 07c950f1e5794a7cfc6d091c2f29a76557adc717 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 11 Aug 2019 13:55:39 -0400 Subject: [PATCH] Review icon badge color management Related commit & feedback: - https://github.com/gorhill/uBlock/commit/7ff750eaf6007bdea4e843d3314fc7275b1ce945 The color value for the icon badge is now "attached" to the blocking profile value. Additionally, as per feedback, `3p` rules will be relaxing before master JavaScript switch rules. --- src/js/background.js | 5 +++-- src/js/commands.js | 36 +++++++++++++++--------------------- src/js/tab.js | 34 ++++++++-------------------------- src/js/ublock.js | 24 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 008566121..1fc46649b 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -42,8 +42,7 @@ const µBlock = (function() { // jshint ignore:line autoUpdateAssetFetchPeriod: 120, autoUpdateDelayAfterLaunch: 180, autoUpdatePeriod: 7, - blockingProfiles: '11101 11001 00001', - blockingProfileColors: '#666666 #E7552C #F69454 #008DCB', + blockingProfiles: '11111/#F00 11011/#C0F 11001/#00F 00001', cacheStorageAPI: 'unset', cacheStorageCompression: true, cacheControlForFirefox1376932: 'no-cache, no-store, must-revalidate', @@ -191,6 +190,8 @@ const µBlock = (function() { // jshint ignore:line cspNoInlineScript: "script-src 'unsafe-eval' * blob: data:", cspNoScripting: 'script-src http: https:', cspNoInlineFont: 'font-src *', + + liveBlockingProfiles: [], }; })(); diff --git a/src/js/commands.js b/src/js/commands.js index e94a3fdf0..70f73fdfb 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -47,27 +47,21 @@ const relaxBlockingMode = function(tab) { if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; } const hn = µb.URI.hostnameFromURI(normalURL); - const currentProfile = µb.blockingModeFromHostname(hn); - const profiles = []; - for ( const s of µb.hiddenSettings.blockingProfiles.split(/\s+/) ) { - const v = parseInt(s, 2); - if ( isNaN(v) ) { continue; } - profiles.push(v); - } - let newProfile; - for ( const profile of profiles ) { - if ( (currentProfile & profile & 0b11111110) !== currentProfile ) { - newProfile = profile; + const curProfileBits = µb.blockingModeFromHostname(hn); + let newProfileBits; + for ( const profile of µb.liveBlockingProfiles ) { + if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) { + newProfileBits = profile.bits; break; } } // TODO: Reset to original blocking profile? - if ( newProfile === undefined ) { return; } + if ( newProfileBits === undefined ) { return; } if ( - (currentProfile & 0b00000010) !== 0 && - (newProfile & 0b00000010) === 0 + (curProfileBits & 0b00000010) !== 0 && + (newProfileBits & 0b00000010) === 0 ) { µb.toggleHostnameSwitch({ name: 'no-scripting', @@ -77,8 +71,8 @@ const relaxBlockingMode = function(tab) { } if ( µb.userSettings.advancedUserEnabled ) { if ( - (currentProfile & 0b00000100) !== 0 && - (newProfile & 0b00000100) === 0 + (curProfileBits & 0b00000100) !== 0 && + (newProfileBits & 0b00000100) === 0 ) { µb.toggleFirewallRule({ srcHostname: hn, @@ -88,8 +82,8 @@ const relaxBlockingMode = function(tab) { }); } if ( - (currentProfile & 0b00001000) !== 0 && - (newProfile & 0b00001000) === 0 + (curProfileBits & 0b00001000) !== 0 && + (newProfileBits & 0b00001000) === 0 ) { µb.toggleFirewallRule({ srcHostname: hn, @@ -99,8 +93,8 @@ const relaxBlockingMode = function(tab) { }); } if ( - (currentProfile & 0b00010000) !== 0 && - (newProfile & 0b00010000) === 0 + (curProfileBits & 0b00010000) !== 0 && + (newProfileBits & 0b00010000) === 0 ) { µb.toggleFirewallRule({ srcHostname: hn, @@ -111,7 +105,7 @@ const relaxBlockingMode = function(tab) { } } - if ( newProfile & 0b00000001 ) { + if ( newProfileBits & 0b00000001 ) { vAPI.tabs.reload(tab.id); } }; diff --git a/src/js/tab.js b/src/js/tab.js index 9e4a4304e..e0de96eca 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -954,24 +954,6 @@ vAPI.tabs = new vAPI.Tabs(); µBlock.updateToolbarIcon = (( ) => { const tabIdToDetails = new Map(); - const blockingProfileColors = [ - '#666666', - '#E7552C', - '#F69454', - '#008DCB', - ]; - - self.addEventListener( - 'hiddenSettingsChanged', - ( ) => { - const colors = µBlock.hiddenSettings.blockingProfileColors; - if ( /^#[0-9a-f]{6}(\s#[0-9a-f]{6}){3}$/i.test(colors) === false ) { - return; - } - blockingProfileColors.length = 0; - blockingProfileColors.push(...colors.split(/\s+/)); - } - ); const updateBadge = function(tabId) { const µb = µBlock; @@ -980,7 +962,7 @@ vAPI.tabs = new vAPI.Tabs(); let state = 0; let badge = ''; - let color = blockingProfileColors[0]; + let color = '#666'; let pageStore = µb.pageStoreFromTabId(tabId); if ( pageStore !== null ) { @@ -993,13 +975,13 @@ vAPI.tabs = new vAPI.Tabs(); badge = µb.formatCount(pageStore.perLoadBlockedRequestCount); } if ( (parts & 0b100) !== 0 ) { - let profile = µb.blockingModeFromHostname(pageStore.tabHostname); - if ( (profile & 0b00000010) !== 0 ) { - color = blockingProfileColors[3]; - } else if ( (profile & 0b00000100) !== 0 ) { - color = blockingProfileColors[2]; - } else if ( (profile & 0b00011000) !== 0 ) { - color = blockingProfileColors[1]; + const currentBits = µb.blockingModeFromHostname(pageStore.tabHostname); + let max = 0; + for ( const profile of µb.liveBlockingProfiles ) { + const v = currentBits & (profile.bits & ~1); + if ( v < max ) { break; } + color = profile.color; + max = v; } } } diff --git a/src/js/ublock.js b/src/js/ublock.js index 034898c56..d8adf7a18 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -605,6 +605,30 @@ const matchBucket = function(url, hostname, bucket, start) { return bits; }; +µBlock.parseBlockingProfiles = (( ) => { + const parse = function() { + const s = µBlock.hiddenSettings.blockingProfiles; + const profiles = []; + s.split(/\s+/).forEach(s => { + let pos = s.indexOf('/'); + if ( pos === -1 ) { + pos = s.length; + } + const bits = parseInt(s.slice(0, pos), 2); + if ( isNaN(bits) ) { return; } + const color = s.slice(pos + 1); + profiles.push({ bits, color: color !== '' ? color : '#666' }); + }); + µBlock.liveBlockingProfiles = profiles; + }; + + parse(); + + self.addEventListener('hiddenSettingsChanged', ( ) => { parse(); }); + + return parse; +})(); + /******************************************************************************/ // https://github.com/NanoMeow/QuickReports/issues/6#issuecomment-414516623