@@ -29,7 +29,7 @@ import {
2929
3030import {
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+
340353export 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