export interface PiHoleSettingsStorage { pi_uri_base ? : string ; api_key ? : string ; } export enum PiHoleSettingsDefaults { pi_uri_base = 'http://pi.hole/admin' , default_disable_time = 10 } export interface ExtensionStorage { pi_hole_settings ? : PiHoleSettingsStorage [ ] ; default_disable_time ? : number ; reload_after_disable ? : boolean ; reload_after_white_list ? : boolean ; disable_list_feature ? : boolean ; disable_update_notification ? : boolean ; beta_feature_flag ? : boolean ; disable_context_menu ? : boolean ; } export enum ExtensionStorageEnum { pi_hole_settings = 'pi_hole_settings' , default_disable_time = 'default_disable_time' , reload_after_disable = 'reload_after_disable' , reload_after_white_list = 'reload_after_white_list' , disable_list_feature = 'disable_list_feature' , disable_update_notification = 'disable_update_notification' , disable_context_menu = 'disable_context_menu' } export class StorageService { public static savePiHoleSettingsArray ( settings ) { if ( settings . length > 0 ) { const filteredSettings = settings . filter ( value => value . pi_uri_base ) ; if ( filteredSettings . length < 1 ) { chrome . storage . local . remove ( ExtensionStorageEnum . pi_hole_settings ) ; return ; } const secureSettings = [ ] ; for ( const setting of filteredSettings ) { const secureSetting = { } ; secureSetting . pi_uri_base = String ( setting . pi_uri_base ) ; secureSetting . api_key = String ( setting . api_key ) ; secureSettings . push ( secureSetting ) ; } const storage = { pi_hole_settings : secureSettings } ; chrome . storage . local . set ( storage ) ; } } public static saveDefaultDisableTime ( time ) { if ( time < 1 ) { return ; } const storage = { default_disable_time : time } ; chrome . storage . local . set ( storage ) ; } public static getDefaultDisableTime ( ) { return this . getStorageValue < number > ( ExtensionStorageEnum . default_disable_time ) ; } public static saveReloadAfterDisable ( state ) { const storage = { reload_after_disable : state } ; chrome . storage . local . set ( storage ) ; } public static getReloadAfterDisable ( ) { return this . getStorageValue < boolean > ( ExtensionStorageEnum . reload_after_disable ) ; } public static saveReloadAfterWhitelist ( state ) { const storage = { reload_after_white_list : state } ; chrome . storage . local . set ( storage ) ; } public static getReloadAfterWhitelist ( ) { return this . getStorageValue < boolean > ( ExtensionStorageEnum . reload_after_white_list ) ; } public static getPiHoleSettingsArray ( ) { return this . getStorageValue < PiHoleSettingsStorage [ ] > ( ExtensionStorageEnum . pi_hole_settings ) ; } public static getDisableListFeature ( ) { return this . getStorageValue < boolean > ( ExtensionStorageEnum . disable_list_feature ) ; } public static saveDisableListFeature ( state ) { const storage = { disable_list_feature : state } ; chrome . storage . local . set ( storage ) ; } public static getDisableUpdateNotification ( ) { return this . getStorageValue < boolean > ( ExtensionStorageEnum . disable_update_notification ) ; } public static saveDisableUpdateNotification ( state ) { const storage = { disable_update_notification : state } ; chrome . storage . local . set ( storage ) ; } public static getDisableContextMenu ( ) { return this . getStorageValue < boolean > ( ExtensionStorageEnum . disable_context_menu , false ) ; } public static saveDisableContextMenu ( state ) { const storage = { disable_context_menu : state } ; chrome . storage . local . set ( storage ) ; } private static getStorageValue < T > ( key ) ; private static getStorageValue < T > ( key , defaultUnsetValue ) ; private static getStorageValue < T > ( key , defaultUnsetValue ? ) { return new Promise ( resolve => { chrome . storage . local . get ( key , obj => { const storageValue = obj [ key ] ; if ( typeof defaultUnsetValue !== 'undefined' && typeof storageValue === 'undefined' ) { resolve ( defaultUnsetValue ) ; } resolve ( storageValue ) ; } ) ; } ) ; } }