Skip to content

Commit ad1800f

Browse files
committed
Add command to toggle cosmetic filtering
Related issue: - uBlockOrigin/uBlock-issues#2000
1 parent db5d598 commit ad1800f

File tree

6 files changed

+39
-21
lines changed

6 files changed

+39
-21
lines changed

platform/chromium/manifest.json

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
},
2727
"relax-blocking-mode": {
2828
"description": "__MSG_relaxBlockingMode__"
29+
},
30+
"toggle-cosmetic-filtering": {
31+
"description": "__MSG_toggleCosmeticFiltering__"
2932
}
3033
},
3134
"content_scripts": [

platform/firefox/manifest.json

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
},
3636
"relax-blocking-mode": {
3737
"description": "__MSG_relaxBlockingMode__"
38+
},
39+
"toggle-cosmetic-filtering": {
40+
"description": "__MSG_toggleCosmeticFiltering__"
3841
}
3942
},
4043
"content_scripts": [

platform/opera/manifest.json

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
},
2727
"relax-blocking-mode": {
2828
"description": "__MSG_relaxBlockingMode__"
29+
},
30+
"toggle-cosmetic-filtering": {
31+
"description": "__MSG_toggleCosmeticFiltering__"
2932
}
3033
},
3134
"content_scripts": [

src/_locales/en/messages.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1221,13 +1221,13 @@
12211221
"message": "Select all",
12221222
"description": "Label for buttons used to select all text in editor"
12231223
},
1224-
"toggleBlockingProfile": {
1225-
"message": "Toggle blocking profile",
1226-
"description": "Label for keyboard shortcut used to toggle blocking profile"
1224+
"toggleCosmeticFiltering": {
1225+
"message": "Toggle cosmetic filtering",
1226+
"description": "Label for keyboard shortcut used to toggle cosmetic filtering"
12271227
},
12281228
"relaxBlockingMode": {
12291229
"message": "Relax blocking mode",
1230-
"description": "Label for keyboard shortcut used to relax blocking mode (meant to replace 'Toggle blocking profile')"
1230+
"description": "Label for keyboard shortcut used to relax blocking mode"
12311231
},
12321232
"storageUsed": {
12331233
"message": "Storage used: {{value}} {{unit}}",

src/js/commands.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,21 @@ const relaxBlockingMode = (( ) => {
160160
})();
161161

162162
vAPI.commands.onCommand.addListener(async command => {
163+
// Generic commands
164+
if ( command === 'open-dashboard' ) {
165+
µb.openNewTab({
166+
url: 'dashboard.html',
167+
select: true,
168+
index: -1,
169+
});
170+
return;
171+
}
172+
// Tab-specific commands
173+
const tab = await vAPI.tabs.getCurrent();
174+
if ( tab instanceof Object === false ) { return; }
163175
switch ( command ) {
164176
case 'launch-element-picker':
165177
case 'launch-element-zapper': {
166-
const tab = await vAPI.tabs.getCurrent();
167-
if ( tab instanceof Object === false ) { return; }
168178
µb.epickerArgs.mouse = false;
169179
µb.elementPickerExec(
170180
tab.id,
@@ -175,8 +185,6 @@ vAPI.commands.onCommand.addListener(async command => {
175185
break;
176186
}
177187
case 'launch-logger': {
178-
const tab = await vAPI.tabs.getCurrent();
179-
if ( tab instanceof Object === false ) { return; }
180188
const hash = tab.url.startsWith(vAPI.getURL(''))
181189
? ''
182190
: `#_+${tab.id}`;
@@ -187,16 +195,14 @@ vAPI.commands.onCommand.addListener(async command => {
187195
});
188196
break;
189197
}
190-
case 'open-dashboard': {
191-
µb.openNewTab({
192-
url: 'dashboard.html',
193-
select: true,
194-
index: -1,
195-
});
196-
break;
197-
}
198198
case 'relax-blocking-mode':
199-
relaxBlockingMode(await vAPI.tabs.getCurrent());
199+
relaxBlockingMode(tab);
200+
break;
201+
case 'toggle-cosmetic-filtering':
202+
µb.toggleHostnameSwitch({
203+
name: 'no-cosmetic-filtering',
204+
hostname: hostnameFromURI(µb.normalizeTabURL(tab.id, tab.url)),
205+
});
200206
break;
201207
default:
202208
break;

src/js/ublock.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,14 @@ const matchBucket = function(url, hostname, bucket, start) {
566566
/******************************************************************************/
567567

568568
µb.toggleHostnameSwitch = function(details) {
569+
const newState = typeof details.state === 'boolean'
570+
? details.state
571+
: sessionSwitches.evaluateZ(details.name, details.hostname) === false;
569572
let changed = sessionSwitches.toggleZ(
570573
details.name,
571574
details.hostname,
572575
!!details.deep,
573-
details.state
576+
newState
574577
);
575578
if ( changed === false ) { return; }
576579

@@ -580,7 +583,7 @@ const matchBucket = function(url, hostname, bucket, start) {
580583
this.updateToolbarIcon(details.tabId, 0b100);
581584
break;
582585
case 'no-cosmetic-filtering': {
583-
const scriptlet = details.state ? 'cosmetic-off' : 'cosmetic-on';
586+
const scriptlet = newState ? 'cosmetic-off' : 'cosmetic-on';
584587
vAPI.tabs.executeScript(details.tabId, {
585588
file: `/js/scriptlets/${scriptlet}.js`,
586589
allFrames: true,
@@ -590,7 +593,7 @@ const matchBucket = function(url, hostname, bucket, start) {
590593
case 'no-large-media':
591594
const pageStore = this.pageStoreFromTabId(details.tabId);
592595
if ( pageStore !== null ) {
593-
pageStore.temporarilyAllowLargeMediaElements(!details.state);
596+
pageStore.temporarilyAllowLargeMediaElements(!newState);
594597
}
595598
break;
596599
}
@@ -601,7 +604,7 @@ const matchBucket = function(url, hostname, bucket, start) {
601604
details.name,
602605
details.hostname,
603606
!!details.deep,
604-
details.state
607+
newState
605608
);
606609
if ( changed ) {
607610
this.saveHostnameSwitches();

0 commit comments

Comments
 (0)