Skip to content

Commit

Permalink
Add command to toggle cosmetic filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 22, 2022
1 parent db5d598 commit ad1800f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
3 changes: 3 additions & 0 deletions platform/chromium/manifest.json
Expand Up @@ -26,6 +26,9 @@
},
"relax-blocking-mode": {
"description": "__MSG_relaxBlockingMode__"
},
"toggle-cosmetic-filtering": {
"description": "__MSG_toggleCosmeticFiltering__"
}
},
"content_scripts": [
Expand Down
3 changes: 3 additions & 0 deletions platform/firefox/manifest.json
Expand Up @@ -35,6 +35,9 @@
},
"relax-blocking-mode": {
"description": "__MSG_relaxBlockingMode__"
},
"toggle-cosmetic-filtering": {
"description": "__MSG_toggleCosmeticFiltering__"
}
},
"content_scripts": [
Expand Down
3 changes: 3 additions & 0 deletions platform/opera/manifest.json
Expand Up @@ -26,6 +26,9 @@
},
"relax-blocking-mode": {
"description": "__MSG_relaxBlockingMode__"
},
"toggle-cosmetic-filtering": {
"description": "__MSG_toggleCosmeticFiltering__"
}
},
"content_scripts": [
Expand Down
8 changes: 4 additions & 4 deletions src/_locales/en/messages.json
Expand Up @@ -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}}",
Expand Down
32 changes: 19 additions & 13 deletions src/js/commands.js
Expand Up @@ -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,
Expand All @@ -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}`;
Expand All @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/js/ublock.js
Expand Up @@ -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; }

Expand All @@ -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,
Expand All @@ -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;
}
Expand All @@ -601,7 +604,7 @@ const matchBucket = function(url, hostname, bucket, start) {
details.name,
details.hostname,
!!details.deep,
details.state
newState
);
if ( changed ) {
this.saveHostnameSwitches();
Expand Down

0 comments on commit ad1800f

Please sign in to comment.