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 ) {
2018-06-27 23:55:37 +02:00
if ( typeof saved [ k ] !== 'object' || saved [ k ] . constructor !== Object || extDefaults [ k ] . override === true )
continue ; // if user's settings are wrong or if override flag is set, we keep value in extDefaults
2018-03-18 15:14:57 +01:00
_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
}
2018-06-27 23:55:37 +02:00
// -----------------------------------------
// Nastavitve za posamezno stran
// Config for a given page:
//
// <hostname> : {
// status: <option> // should extension work on this site?
// arStatus: <option> // should we do autodetection on this site?
// statusEmbedded: <option> // reserved for future... maybe
// }
//
// Veljavne vrednosti za možnosti
// Valid values for options:
//
// status, arStatus, statusEmbedded:
//
// * enabled — always allow
// * default — allow if default is to allow, block if default is to block
// * disabled — never allow
//
canStartExtension = function ( site ) {
if ( site === undefined ) {
site = window . location . hostname ;
}
2018-06-29 00:30:42 +02:00
// console.log("CAN WE START THIS EXTENSION ON SITE", site,
// "?\n\nExtensionConf.sites[site]=",ExtensionConf.sites[site],
// "\nExtension mode?", ExtensionConf.extensionMode
// );
2018-06-28 23:43:52 +02:00
2018-06-27 23:55:37 +02:00
if ( ExtensionConf . sites [ site ] === undefined ) {
2018-06-28 23:43:52 +02:00
return ExtensionConf . extensionMode === "blacklist" ; // site not defined, this does default option
2018-06-27 23:55:37 +02:00
}
2018-06-28 23:43:52 +02:00
if ( ExtensionConf . extensionMode === "blacklist" ) {
2018-06-27 23:55:37 +02:00
return ExtensionConf . sites [ site ] . status !== "disabled" ;
2018-06-29 00:30:42 +02:00
} else if ( ExtensionConf . extensionMode === "whitelist" ) {
2018-06-27 23:55:37 +02:00
return ExtensionConf . sites [ site ] . status === "enabled" ;
} else {
return false ;
}
}
canStartAutoAr = function ( site ) {
if ( site === undefined ) {
site = window . location . hostname ;
}
if ( ExtensionConf . sites [ site ] === undefined ) {
return ExtensionConf . arDetect . mode === "blacklist" ; // site not defined, this does default option
}
if ( ExtensionConf . arDetect . mode === "blacklist" ) {
return ExtensionConf . sites [ site ] . arStatus !== "disabled" ;
} else if ( ExtensionConf . arDetect . mode === "whitelist" ) {
return ExtensionConf . sites [ site ] . arStatus === "enabled" ;
} else {
return false ;
}
}