mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
[mv3] Improve automatically changing filtering mode
Related issue: https://github.com/uBlockOrigin/uBOL-home/issues/554
This commit is contained in:
parent
ab3227b8cb
commit
cd814ee818
2 changed files with 28 additions and 8 deletions
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue