From ad1800fbcaeea30a860871f3179def5c4af1bc45 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 22 Feb 2022 08:44:09 -0500 Subject: [PATCH] Add command to toggle cosmetic filtering Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/2000 --- platform/chromium/manifest.json | 3 +++ platform/firefox/manifest.json | 3 +++ platform/opera/manifest.json | 3 +++ src/_locales/en/messages.json | 8 ++++---- src/js/commands.js | 32 +++++++++++++++++++------------- src/js/ublock.js | 11 +++++++---- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/platform/chromium/manifest.json b/platform/chromium/manifest.json index 47e9d1e48..b492b9a31 100644 --- a/platform/chromium/manifest.json +++ b/platform/chromium/manifest.json @@ -26,6 +26,9 @@ }, "relax-blocking-mode": { "description": "__MSG_relaxBlockingMode__" + }, + "toggle-cosmetic-filtering": { + "description": "__MSG_toggleCosmeticFiltering__" } }, "content_scripts": [ diff --git a/platform/firefox/manifest.json b/platform/firefox/manifest.json index 3c214d975..43deb2c6e 100644 --- a/platform/firefox/manifest.json +++ b/platform/firefox/manifest.json @@ -35,6 +35,9 @@ }, "relax-blocking-mode": { "description": "__MSG_relaxBlockingMode__" + }, + "toggle-cosmetic-filtering": { + "description": "__MSG_toggleCosmeticFiltering__" } }, "content_scripts": [ diff --git a/platform/opera/manifest.json b/platform/opera/manifest.json index 5c8b59c4a..d61215ff3 100644 --- a/platform/opera/manifest.json +++ b/platform/opera/manifest.json @@ -26,6 +26,9 @@ }, "relax-blocking-mode": { "description": "__MSG_relaxBlockingMode__" + }, + "toggle-cosmetic-filtering": { + "description": "__MSG_toggleCosmeticFiltering__" } }, "content_scripts": [ diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index f0cbcae9b..fc479aadc 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -1221,13 +1221,13 @@ "message": "Select all", "description": "Label for buttons used to select all text in editor" }, - "toggleBlockingProfile": { - "message": "Toggle blocking profile", - "description": "Label for keyboard shortcut used to toggle blocking profile" + "toggleCosmeticFiltering": { + "message": "Toggle cosmetic filtering", + "description": "Label for keyboard shortcut used to toggle cosmetic filtering" }, "relaxBlockingMode": { "message": "Relax blocking mode", - "description": "Label for keyboard shortcut used to relax blocking mode (meant to replace 'Toggle blocking profile')" + "description": "Label for keyboard shortcut used to relax blocking mode" }, "storageUsed": { "message": "Storage used: {{value}} {{unit}}", diff --git a/src/js/commands.js b/src/js/commands.js index 63585df29..c70b6ce28 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -160,11 +160,21 @@ const relaxBlockingMode = (( ) => { })(); vAPI.commands.onCommand.addListener(async command => { + // Generic commands + if ( command === 'open-dashboard' ) { + µb.openNewTab({ + url: 'dashboard.html', + select: true, + index: -1, + }); + return; + } + // Tab-specific commands + const tab = await vAPI.tabs.getCurrent(); + if ( tab instanceof Object === false ) { return; } switch ( command ) { case 'launch-element-picker': case 'launch-element-zapper': { - const tab = await vAPI.tabs.getCurrent(); - if ( tab instanceof Object === false ) { return; } µb.epickerArgs.mouse = false; µb.elementPickerExec( tab.id, @@ -175,8 +185,6 @@ vAPI.commands.onCommand.addListener(async command => { break; } case 'launch-logger': { - const tab = await vAPI.tabs.getCurrent(); - if ( tab instanceof Object === false ) { return; } const hash = tab.url.startsWith(vAPI.getURL('')) ? '' : `#_+${tab.id}`; @@ -187,16 +195,14 @@ vAPI.commands.onCommand.addListener(async command => { }); break; } - case 'open-dashboard': { - µb.openNewTab({ - url: 'dashboard.html', - select: true, - index: -1, - }); - break; - } case 'relax-blocking-mode': - relaxBlockingMode(await vAPI.tabs.getCurrent()); + relaxBlockingMode(tab); + break; + case 'toggle-cosmetic-filtering': + µb.toggleHostnameSwitch({ + name: 'no-cosmetic-filtering', + hostname: hostnameFromURI(µb.normalizeTabURL(tab.id, tab.url)), + }); break; default: break; diff --git a/src/js/ublock.js b/src/js/ublock.js index a772a4197..06c4a5fb0 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -566,11 +566,14 @@ const matchBucket = function(url, hostname, bucket, start) { /******************************************************************************/ µb.toggleHostnameSwitch = function(details) { + const newState = typeof details.state === 'boolean' + ? details.state + : sessionSwitches.evaluateZ(details.name, details.hostname) === false; let changed = sessionSwitches.toggleZ( details.name, details.hostname, !!details.deep, - details.state + newState ); if ( changed === false ) { return; } @@ -580,7 +583,7 @@ const matchBucket = function(url, hostname, bucket, start) { this.updateToolbarIcon(details.tabId, 0b100); break; case 'no-cosmetic-filtering': { - const scriptlet = details.state ? 'cosmetic-off' : 'cosmetic-on'; + const scriptlet = newState ? 'cosmetic-off' : 'cosmetic-on'; vAPI.tabs.executeScript(details.tabId, { file: `/js/scriptlets/${scriptlet}.js`, allFrames: true, @@ -590,7 +593,7 @@ const matchBucket = function(url, hostname, bucket, start) { case 'no-large-media': const pageStore = this.pageStoreFromTabId(details.tabId); if ( pageStore !== null ) { - pageStore.temporarilyAllowLargeMediaElements(!details.state); + pageStore.temporarilyAllowLargeMediaElements(!newState); } break; } @@ -601,7 +604,7 @@ const matchBucket = function(url, hostname, bucket, start) { details.name, details.hostname, !!details.deep, - details.state + newState ); if ( changed ) { this.saveHostnameSwitches();