2018-03-13 23:55:38 +01:00
// This file handles saving and loading of settings.
// Actual settings are in ExtensionConf
2017-09-24 01:54:46 +02:00
if ( Debug . debug )
console . log ( "Loading: Settings.js" ) ;
2018-03-13 23:55:38 +01:00
var _se _init = async function ( isSlave ) {
2017-12-29 23:34:40 +01:00
if ( Debug . debug )
console . log ( "[Settings::_se_init()] -------- starting init! ---------" ) ;
2018-03-13 23:55:38 +01:00
if ( isSlave === undefined )
isSlave = false ;
if ( isSlave ) {
// request settings from background script. Yes, this is supposed to be global.
var res = await Comms . sendToBackgroundScript ( { cmd : "gib-settings" } ) ;
ExtensionConf = res . response ;
if ( Debug . debug ) {
console . log ( "[Settings::_se_init()] received settings from the background script. ExtensionConf:" , ExtensionConf , "response message was this:" , res ) ;
}
return ;
}
2017-12-29 23:34:40 +01:00
var newSettings = await StorageManager . getopt _async ( "uw-settings" ) ;
2018-02-22 22:10:13 +01:00
var uwVersion = browser . runtime . getManifest ( ) . version ;
2017-12-29 23:34:40 +01:00
if ( Debug . debug )
2018-03-13 23:55:38 +01:00
console . log ( "[Settings::_se_init()] settings saved in localstorage are:" , newSettings , " - if that's empty, it's gonna be replaced by this:" , ExtensionConf , ")" ) ;
2017-12-29 23:34:40 +01:00
2018-02-01 00:26:16 +01:00
if ( ( Object . keys ( newSettings ) . length === 0 && newSettings . constructor === Object ) ) {
2018-03-13 23:55:38 +01:00
if ( Debug . debug )
console . log ( "[Settings::_se_init()] no saved settings, saving default" ) ;
StorageManager . setopt ( { "uw-settings" : ExtensionConf } ) ;
2017-12-29 23:34:40 +01:00
}
else {
2018-03-13 23:55:38 +01:00
var actualSettings = newSettings [ "uw-settings" ] ;
2018-02-22 22:10:13 +01:00
if ( actualSettings . version === undefined || actualSettings . version != uwVersion ) {
2018-03-13 23:55:38 +01:00
ExtensionConf [ 'version' ] = uwVersion ;
if ( Debug . debug )
console . log ( "[Settings::_se_init()] extension was updated, replacing settings" , ExtensionConf ) ;
StorageManager . setopt ( { "uw-settings" : ExtensionConf } ) ;
2018-02-22 22:10:13 +01:00
}
2018-03-18 15:14:57 +01:00
else {
_se _patchUserSettings ( actualSettings , ExtensionConf ) ;
}
2018-02-02 00:21:29 +01:00
if ( Debug . debug )
2018-03-18 15:14:57 +01:00
console . log ( "[Settings::_se_init()] parsed settings:" , actualSettings , "were they copied to ExtensionConf?" , ExtensionConf ) ;
2018-02-02 00:21:29 +01:00
2017-12-29 23:34:40 +01:00
}
if ( Debug . debug )
2018-03-13 23:55:38 +01:00
console . log ( "[Settings::_se_init] settings have been loaded/reloaded. Current state: " , ExtensionConf ) ;
2017-12-29 23:34:40 +01:00
2017-12-17 22:56:07 +01:00
}
2018-03-18 15:14:57 +01:00
var _se _patchUserSettings = function ( saved , extDefaults ) {
for ( var k in extDefaults ) {
if ( extDefaults [ k ] != null && typeof extDefaults [ k ] === 'object' && extDefaults [ k ] . constructor === Object ) {
if ( typeof saved [ k ] !== 'object' || saved [ k ] . constructor !== Object )
continue ;
_se _patchUserSettings ( saved [ k ] , extDefaults [ k ] ) ;
}
else {
extDefaults [ k ] = saved [ k ] ;
}
}
}
2018-02-01 00:26:16 +01:00
var _se _save = function ( settings ) {
2017-12-29 23:34:40 +01:00
StorageManager . delopt ( "uw-settings" ) ;
2018-02-01 00:26:16 +01:00
if ( settings !== undefined ) {
2018-03-13 23:55:38 +01:00
StorageManager . setopt ( { "uw-settings" : settings } ) ;
2018-02-01 00:26:16 +01:00
}
else {
2018-03-13 23:55:38 +01:00
StorageManager . setopt ( { "uw-settings" : ExtensionConf } ) ;
2018-02-01 00:26:16 +01:00
}
if ( Debug . debug )
2018-03-13 23:55:38 +01:00
console . log ( "[Settings::_se_save()] saving settings:" , settings ) ;
2017-12-29 23:34:40 +01:00
}
var _se _reload = function ( ) {
this . init ( true ) ;
}
2018-02-04 17:39:26 +01:00
var _se _isBlacklisted = function ( site ) {
return this . blacklist . indexOf ( site ) > - 1 ;
}
var _se _isWhitelisted = function ( site ) {
return this . whitelist . indexOf ( site ) > - 1 ;
}
2017-12-29 23:34:40 +01:00
var Settings = {
init : _se _init ,
save : _se _save ,
reload : _se _reload ,
2017-09-24 01:54:46 +02:00
}