Skip to content

Commit cd814ee

Browse files
committed
[mv3] Improve automatically changing filtering mode
Related issue: uBlockOrigin/uBOL-home#554
1 parent ab3227b commit cd814ee

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

platform/mv3/extension/js/background.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getDefaultFilteringMode,
2727
getFilteringMode,
2828
getFilteringModeDetails,
29+
persistHostPermissions,
2930
setDefaultFilteringMode,
3031
setFilteringMode,
3132
setFilteringModeDetails,
@@ -132,6 +133,7 @@ async function reloadTab(tabId, url = '') {
132133

133134
// When a new host permission is granted through the popup panel
134135
async function onPermissionGrantedThruExtension(details, origins) {
136+
await persistHostPermissions();
135137
const defaultMode = await getDefaultFilteringMode();
136138
if ( defaultMode >= MODE_OPTIMAL ) { return; }
137139
if ( Array.isArray(origins) === false ) { return; }
@@ -159,8 +161,7 @@ async function onPermissionGrantedThruBrowser(origins) {
159161
const results = await browser.scripting.executeScript({
160162
target: { tabId, frameIds: [ 0 ] },
161163
func: ( ) => document.location.hostname,
162-
}).catch(reason => {
163-
ubolErr(`executeScript/${reason}`);
164+
}).catch(( ) => {
164165
});
165166
const tabHostname = results?.[0]?.result;
166167
if ( typeof tabHostname !== 'string' ) { return; }

platform/mv3/extension/js/mode-manager.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929

3030
import {
3131
browser,
32-
localRead, localWrite,
32+
localRead, localRemove, localWrite,
3333
sessionRead, sessionWrite,
3434
} from './ext.js';
3535

@@ -337,16 +337,33 @@ export function setDefaultFilteringMode(afterLevel) {
337337

338338
/******************************************************************************/
339339

340+
export async function persistHostPermissions(iter) {
341+
if ( iter === undefined ) {
342+
const permissions = await browser.permissions.getAll();
343+
iter = hostnamesFromMatches(permissions.origins) || [];
344+
}
345+
const hostnames = Array.from(iter);
346+
return hostnames.length !== 0
347+
? localWrite('permissions.hostnames', hostnames)
348+
: localRemove('permissions.hostnames');
349+
}
350+
351+
/******************************************************************************/
352+
340353
export async function syncWithBrowserPermissions() {
341354
const [
342-
permissions,
355+
beforePermissions,
356+
afterPermissions,
343357
beforeMode,
344358
] = await Promise.all([
359+
localRead('permissions.hostnames'),
345360
browser.permissions.getAll(),
346361
getDefaultFilteringMode(),
347362
]);
348-
const allowedHostnames = new Set(hostnamesFromMatches(permissions.origins || []));
349-
const hasBroadHostPermissions = allowedHostnames.has('all-urls');
363+
const beforeAllowedHostnames = new Set(beforePermissions);
364+
const afterAllowedHostnames = new Set(hostnamesFromMatches(afterPermissions.origins || []));
365+
await persistHostPermissions(afterAllowedHostnames);
366+
const hasBroadHostPermissions = afterAllowedHostnames.has('all-urls');
350367
const broadHostPermissionsToggled =
351368
hasBroadHostPermissions !== rulesetConfig.hasBroadHostPermissions;
352369
let modified = false;
@@ -364,13 +381,15 @@ export async function syncWithBrowserPermissions() {
364381
const afterMode = await getDefaultFilteringMode();
365382
if ( afterMode > MODE_BASIC ) { return afterMode !== beforeMode; }
366383
const filteringModes = await getFilteringModeDetails();
367-
if ( allowedHostnames.has('all-urls') === false ) {
384+
if ( afterAllowedHostnames.has('all-urls') === false ) {
368385
const { none, basic, optimal, complete } = filteringModes;
369386
for ( const hn of new Set([ ...optimal, ...complete ]) ) {
387+
if ( afterAllowedHostnames.has(hn) ) { continue; }
370388
applyFilteringMode(filteringModes, hn, afterMode);
371389
modified = true;
372390
}
373-
for ( const hn of allowedHostnames ) {
391+
for ( const hn of afterAllowedHostnames ) {
392+
if ( beforeAllowedHostnames.has(hn) ) { continue; }
374393
if ( optimal.has(hn) || complete.has(hn) ) { continue; }
375394
if ( basic.has(hn) || none.has(hn) ) { continue; }
376395
applyFilteringMode(filteringModes, hn, MODE_OPTIMAL);

0 commit comments

Comments
 (0)