import { Initializer } from '../../general/Initializer' ; import { I18NContextMenuKeys , I18NService } from '../../../service/i18NService' ; import BackgroundService from '../../../service/BackgroundService' ; import { ContextMenuSwitchMessage , MessageEnum } from '../../../service/MessageBusService' ; import { StorageService } from '../../../service/StorageService' ; import CreateProperties = chrome . contextMenus . CreateProperties ; export default class ContextMenuInitializer implements Initializer { private get contextMenusConfigurations ( ) : CreateProperties [ ] { return [ { title : I18NService . translate ( I18NContextMenuKeys . toggle_pi_holes ) , contexts : [ 'page' ] , onclick : ( ) => { BackgroundService . togglePiHole ( ) ; } } , { type : 'separator' , contexts : [ 'page' ] } , { title : I18NService . translate ( I18NContextMenuKeys . blacklist_current_domain ) , contexts : [ 'page' ] , onclick : ( ) => { BackgroundService . blacklistCurrentDomain ( ) ; } } , { title : I18NService . translate ( I18NContextMenuKeys . whitelist_current_domain ) , contexts : [ 'page' ] , onclick : ( ) => { BackgroundService . whitelistCurrentDomain ( ) ; } } , { type : 'separator' , contexts : [ 'page' ] } , { title : I18NService . translate ( I18NContextMenuKeys . open_settings ) , contexts : [ 'page' ] , onclick : ( ) => BackgroundService . openOptions ( ) } ] ; } init ( ) { StorageService . getDisableContextMenu ( ) . then ( value => { this . removeOrCreateContextMenuByBoolean ( value ) ; } ) ; this . initMessageListener ( ) ; } private initMessageListener ( ) { chrome . runtime . onMessage . addListener ( ( request ) => { if ( request . message === MessageEnum . ContextMenuSwitch ) { this . removeOrCreateContextMenuByBoolean ( request . payload ) ; } } ) ; } private removeOrCreateContextMenuByBoolean ( state ) { if ( ! state ) { this . createContextMenu ( ) ; } else { chrome . contextMenus . removeAll ( ) ; } } private createContextMenu ( ) { for ( const contextMenusConfiguration of this . contextMenusConfigurations ) { chrome . contextMenus . create ( contextMenusConfiguration ) ; } } }