[mv3] Improve automatically changing filtering mode

Related issue:
https://github.com/uBlockOrigin/uBOL-home/issues/554
This commit is contained in:
Raymond Hill 2025-11-29 09:41:46 -05:00
parent ab3227b8cb
commit cd814ee818
No known key found for this signature in database
GPG key ID: F5630CAE62A14316
2 changed files with 28 additions and 8 deletions

View file

@ -26,6 +26,7 @@ import {
getDefaultFilteringMode,
getFilteringMode,
getFilteringModeDetails,
persistHostPermissions,
setDefaultFilteringMode,
setFilteringMode,
setFilteringModeDetails,
@ -132,6 +133,7 @@ async function reloadTab(tabId, url = '') {
// When a new host permission is granted through the popup panel
async function onPermissionGrantedThruExtension(details, origins) {
await persistHostPermissions();
const defaultMode = await getDefaultFilteringMode();
if ( defaultMode >= MODE_OPTIMAL ) { return; }
if ( Array.isArray(origins) === false ) { return; }
@ -159,8 +161,7 @@ async function onPermissionGrantedThruBrowser(origins) {
const results = await browser.scripting.executeScript({
target: { tabId, frameIds: [ 0 ] },
func: ( ) => document.location.hostname,
}).catch(reason => {
ubolErr(`executeScript/${reason}`);
}).catch(( ) => {
});
const tabHostname = results?.[0]?.result;
if ( typeof tabHostname !== 'string' ) { return; }

View file

@ -29,7 +29,7 @@ import {
import {
browser,
localRead, localWrite,
localRead, localRemove, localWrite,
sessionRead, sessionWrite,
} from './ext.js';
@ -337,16 +337,33 @@ export function setDefaultFilteringMode(afterLevel) {
/******************************************************************************/
export async function persistHostPermissions(iter) {
if ( iter === undefined ) {
const permissions = await browser.permissions.getAll();
iter = hostnamesFromMatches(permissions.origins) || [];
}
const hostnames = Array.from(iter);
return hostnames.length !== 0
? localWrite('permissions.hostnames', hostnames)
: localRemove('permissions.hostnames');
}
/******************************************************************************/
export async function syncWithBrowserPermissions() {
const [
permissions,
beforePermissions,
afterPermissions,
beforeMode,
] = await Promise.all([
localRead('permissions.hostnames'),
browser.permissions.getAll(),
getDefaultFilteringMode(),
]);
const allowedHostnames = new Set(hostnamesFromMatches(permissions.origins || []));
const hasBroadHostPermissions = allowedHostnames.has('all-urls');
const beforeAllowedHostnames = new Set(beforePermissions);
const afterAllowedHostnames = new Set(hostnamesFromMatches(afterPermissions.origins || []));
await persistHostPermissions(afterAllowedHostnames);
const hasBroadHostPermissions = afterAllowedHostnames.has('all-urls');
const broadHostPermissionsToggled =
hasBroadHostPermissions !== rulesetConfig.hasBroadHostPermissions;
let modified = false;
@ -364,13 +381,15 @@ export async function syncWithBrowserPermissions() {
const afterMode = await getDefaultFilteringMode();
if ( afterMode > MODE_BASIC ) { return afterMode !== beforeMode; }
const filteringModes = await getFilteringModeDetails();
if ( allowedHostnames.has('all-urls') === false ) {
if ( afterAllowedHostnames.has('all-urls') === false ) {
const { none, basic, optimal, complete } = filteringModes;
for ( const hn of new Set([ ...optimal, ...complete ]) ) {
if ( afterAllowedHostnames.has(hn) ) { continue; }
applyFilteringMode(filteringModes, hn, afterMode);
modified = true;
}
for ( const hn of allowedHostnames ) {
for ( const hn of afterAllowedHostnames ) {
if ( beforeAllowedHostnames.has(hn) ) { continue; }
if ( optimal.has(hn) || complete.has(hn) ) { continue; }
if ( basic.has(hn) || none.has(hn) ) { continue; }
applyFilteringMode(filteringModes, hn, MODE_OPTIMAL);