2018-08-05 23:48:56 +02:00
class Settings {
2018-08-30 00:56:15 +02:00
constructor ( activeSettings , updateCallback ) {
2018-08-22 22:34:07 +02:00
this . active = activeSettings ? activeSettings : undefined ;
2018-08-05 23:48:56 +02:00
this . default = ExtensionConf ;
2018-08-21 23:48:47 +02:00
this . useSync = false ;
2018-08-22 22:34:07 +02:00
this . version = undefined ;
2018-08-30 00:56:15 +02:00
this . updateCallback = updateCallback ;
2018-08-23 01:04:37 +02:00
const ths = this ;
if ( BrowserDetect . firefox ) {
browser . storage . onChanged . addListener ( ( changes , area ) => {
2018-08-30 00:56:15 +02:00
if ( Debug . debug && Debug . debugStorage ) {
2018-08-29 00:41:26 +02:00
console . log ( "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:" , changes , "storage area:" , area ) ;
if ( changes [ 'uwSettings' ] && changes [ 'uwSettings' ] . newValue ) {
console . log ( "[Settings::<storage/on change>] new settings object:" , JSON . parse ( changes . uwSettings . newValue ) ) ;
}
}
2018-08-23 01:04:37 +02:00
if ( changes [ 'uwSettings' ] && changes [ 'uwSettings' ] . newValue ) {
2018-09-17 00:39:32 +02:00
ths . setActive ( JSON . parse ( changes . uwSettings . newValue ) ) ;
2018-08-23 01:04:37 +02:00
}
2018-08-30 00:56:15 +02:00
if ( this . updateCallback ) {
try {
updateCallback ( ) ;
} catch ( e ) {
console . log ( "[Settings] CALLING UPDATE CALLBACK FAILED." )
}
}
2018-08-23 01:04:37 +02:00
} ) ;
2018-08-29 00:41:26 +02:00
} else if ( BrowserDetect . chrome ) {
chrome . storage . onChanged . addListener ( ( changes , area ) => {
2018-08-30 00:56:15 +02:00
if ( Debug . debug && Debug . debugStorage ) {
2018-08-29 00:41:26 +02:00
console . log ( "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:" , changes , "storage area:" , area ) ;
if ( changes [ 'uwSettings' ] && changes [ 'uwSettings' ] . newValue ) {
console . log ( "[Settings::<storage/on change>] new settings object:" , JSON . parse ( changes . uwSettings . newValue ) ) ;
}
}
if ( changes [ 'uwSettings' ] && changes [ 'uwSettings' ] . newValue ) {
2018-09-17 00:39:32 +02:00
ths . setActive ( JSON . parse ( changes . uwSettings . newValue ) ) ;
2018-08-29 00:41:26 +02:00
}
2018-08-30 00:56:15 +02:00
if ( this . updateCallback ) {
try {
updateCallback ( ) ;
} catch ( e ) {
console . log ( "[Settings] CALLING UPDATE CALLBACK FAILED." )
}
}
} ) ;
2018-08-23 01:04:37 +02:00
}
2018-08-05 23:48:56 +02:00
}
async init ( ) {
2018-08-21 23:48:47 +02:00
const settings = await this . get ( ) ;
2018-08-05 23:48:56 +02:00
if ( Debug . debug ) {
2018-08-21 23:48:47 +02:00
console . log ( "[Settings::init] Configuration fetched from storage:" , settings ) ;
2018-08-05 23:48:56 +02:00
}
// if there's no settings saved, return default settings.
2018-08-07 23:31:28 +02:00
if ( ! settings || ( Object . keys ( settings ) . length === 0 && settings . constructor === Object ) ) {
2018-08-05 23:48:56 +02:00
this . setDefaultSettings ( ) ;
this . active = this . getDefaultSettings ( ) ;
return this . active ;
}
// if there's settings, set saved object as active settings
this . active = settings ;
// check if extension has been updated. If not, return settings as they were retreived
2018-08-07 23:31:28 +02:00
if ( BrowserDetect . firefox ) {
2018-08-22 22:34:07 +02:00
this . version = browser . runtime . getManifest ( ) . version ;
2018-08-07 23:31:28 +02:00
} else if ( BrowserDetect . chrome ) {
2018-08-22 22:34:07 +02:00
this . version = chrome . runtime . getManifest ( ) . version ;
2018-08-07 23:31:28 +02:00
} else if ( BrowserDetect . edge ) {
2018-08-22 22:34:07 +02:00
this . version = browser . runtime . getManifest ( ) . version ;
2018-08-07 23:31:28 +02:00
}
2018-08-22 22:34:07 +02:00
if ( settings . version === this . version ) {
2018-08-05 23:48:56 +02:00
if ( Debug . debug ) {
2018-08-22 22:34:07 +02:00
console . log ( "[Settings::init] extension was saved with current version of ultrawidify (" , this . version , "). Returning object as-is." ) ;
2018-08-05 23:48:56 +02:00
}
return this . active ;
}
// if extension has been updated, update existing settings with any options added in the
// new version. In addition to that, we remove old keys that are no longer used.
2018-08-22 22:34:07 +02:00
const patched = ObjectCopy . addNew ( settings , this . default ) ;
if ( Debug . debug ) {
console . log ( "[Settings.init] Results from ObjectCopy.addNew()?" , patched , "\n\nSettings from storage" , settings , "\ndefault?" , this . default , ) ;
}
if ( patched ) {
this . active = patched ;
} else {
this . active = JSON . parse ( JSON . stringify ( this . default ) ) ;
}
2018-08-05 23:48:56 +02:00
this . set ( this . active ) ;
return this . active ;
}
async get ( ) {
2018-09-25 23:37:08 +02:00
if ( BrowserDetect . firefox ) {
const ret = await browser . storage . local . get ( 'uwSettings' ) ;
2018-08-21 23:48:47 +02:00
try {
2018-08-22 22:34:07 +02:00
return JSON . parse ( ret . uwSettings ) ;
2018-08-21 23:48:47 +02:00
} catch ( e ) {
return undefined ;
}
2018-08-05 23:48:56 +02:00
} else if ( BrowserDetect . chrome ) {
2018-08-29 00:41:26 +02:00
const ret = new Promise ( ( resolve , reject ) => {
chrome . storage . sync . get ( 'uwSettings' , ( res ) => resolve ( res ) ) ;
} ) ;
2018-08-22 22:34:07 +02:00
return ret [ 'uwSettings' ] ;
2018-09-25 23:37:08 +02:00
} else if ( BrowserDetect . edge ) {
const ret = new Promise ( ( resolve , reject ) => {
browser . storage . sync . get ( 'uwSettings' , ( res ) => resolve ( res ) ) ;
} ) ;
return ret [ 'uwSettings' ] ;
2018-08-05 23:48:56 +02:00
}
}
async set ( extensionConf ) {
2018-08-23 01:04:37 +02:00
if ( Debug . debug ) {
console . log ( "[Settings::set] setting new settings:" , extensionConf )
}
2018-08-05 23:48:56 +02:00
if ( BrowserDetect . firefox || BrowserDetect . edge ) {
2018-08-22 22:34:07 +02:00
extensionConf . version = this . version ;
return this . useSync ? browser . storage . sync . set ( { 'uwSettings' : JSON . stringify ( extensionConf ) } ) : browser . storage . local . set ( { 'uwSettings' : JSON . stringify ( extensionConf ) } ) ;
2018-08-05 23:48:56 +02:00
} else if ( BrowserDetect . chrome ) {
2018-08-29 00:41:26 +02:00
return chrome . storage . sync . set ( { 'uwSettings' : JSON . stringify ( extensionConf ) } ) ;
2018-08-05 23:48:56 +02:00
}
}
2018-08-07 23:31:28 +02:00
async setActive ( activeSettings ) {
this . active = activeSettings ;
}
async setProp ( prop , value ) {
this . active [ prop ] = value ;
}
async save ( ) {
2018-08-23 01:04:37 +02:00
if ( Debug . debug ) {
console . log ( "[Settings::save] Saving active settings:" , this . active ) ;
}
2018-08-07 23:31:28 +02:00
this . set ( this . active ) ;
}
2018-08-05 23:48:56 +02:00
getDefaultSettings ( ) {
return JSON . parse ( JSON . stringify ( this . default ) ) ;
}
setDefaultSettings ( ) {
this . set ( this . default ) ;
}
2018-09-17 00:39:32 +02:00
2018-08-05 23:48:56 +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
2018-09-17 00:39:32 +02:00
getSiteSettings ( site ) {
if ( ! site ) {
site = window . location . hostname ;
}
if ( ! site || ! this . active . sites [ site ] ) {
return { } ;
}
return this . active . sites [ site ] ;
}
2018-08-05 23:48:56 +02:00
canStartExtension ( site ) {
// returns 'true' if extension can be started on a given site. Returns false if we shouldn't run.
if ( ! site ) {
site = window . location . hostname ;
if ( ! site ) {
2018-08-07 23:31:28 +02:00
console . log ( "[Settings::canStartExtension] window.location.hostname is null or undefined:" , window . location . hostname )
console . log ( "active settings:" , this . active )
2018-08-05 23:48:56 +02:00
return false ;
}
}
if ( Debug . debug ) {
// let's just temporarily disable debugging while recursively calling
// this function to get extension status on current site without duplo
// console logs (and without endless recursion)
Debug . debug = false ;
const cse = this . canStartExtension ( site ) ;
Debug . debug = true ;
}
2018-08-07 23:31:28 +02:00
try {
2018-08-05 23:48:56 +02:00
// if site is not defined, we use default mode:
if ( ! this . active . sites [ site ] ) {
return this . active . extensionMode === "blacklist" ;
}
if ( this . active . extensionMode === "blacklist" ) {
2018-08-07 23:31:28 +02:00
return this . active . sites [ site ] . status !== "disabled" ;
2018-08-05 23:48:56 +02:00
} else if ( this . active . extensionMode === "whitelist" ) {
2018-08-07 23:31:28 +02:00
return this . active . sites [ site ] . status === "enabled" ;
2018-08-05 23:48:56 +02:00
} else {
return false ;
}
2018-08-22 22:34:07 +02:00
} catch ( e ) {
if ( Debug . debug ) {
console . log ( "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\nSettings object:" , this )
}
return false ;
}
2018-08-05 23:48:56 +02:00
}
2018-09-16 14:14:16 +02:00
extensionEnabled ( ) {
return this . active . extensionMode !== 'disabled'
}
extensionEnabledForSite ( site ) {
return this . canStartExtension ( site ) ;
}
2018-08-05 23:48:56 +02:00
canStartAutoAr ( site ) {
if ( ! site ) {
site = window . location . hostname ;
if ( ! site ) {
return false ;
}
}
if ( Debug . debug ) {
// let's just temporarily disable debugging while recursively calling
// this function to get extension status on current site without duplo
// console logs (and without endless recursion)
Debug . debug = false ;
const csar = this . canStartAutoAr ( site ) ;
Debug . debug = true ;
console . log ( "[Settings::canStartAutoAr] ----------------\nCAN WE START THIS EXTENSION ON SITE" , site ,
"?\n\nsettings.active.sites[site]=" , this . active . sites [ site ] ,
"\nExtension mode?" , this . active . arDetect . mode ,
"\nCan extension be started?" , csar
) ;
}
// if site is not defined, we use default mode:
if ( ! this . active . sites [ site ] ) {
return this . active . arDetect . mode === "blacklist" ;
}
if ( this . active . arDetect . mode === "blacklist" ) {
return this . active . sites [ site ] . arStatus !== "disabled" ;
} else if ( this . active . arDetect . mode === "whitelist" ) {
return this . active . sites [ site ] . arStatus === "enabled" ;
} else {
return false ;
}
}
2018-09-17 00:39:32 +02:00
getDefaultAr ( site ) {
site = this . getSiteSettings ( site ) ;
2018-09-18 00:40:05 +02:00
if ( site . defaultAr ) {
return site . defaultAr ;
2018-09-17 00:39:32 +02:00
}
return this . active . miscFullscreenSettings . defaultAr ;
}
2018-09-18 00:40:05 +02:00
getDefaultStretchMode ( site ) {
2018-09-17 00:39:32 +02:00
site = this . getSiteSettings ( site ) ;
2018-09-18 00:40:05 +02:00
if ( site . stretch ) {
return site . stretch ;
2018-09-17 00:39:32 +02:00
}
2018-09-18 00:40:05 +02:00
return this . active . stretch . initialMode ;
2018-09-17 00:39:32 +02:00
}
getDefaultVideoAlignment ( site ) {
site = this . getSiteSettings ( site ) ;
2018-09-18 00:40:05 +02:00
if ( site . videoAlignment ) {
return site . videoAlignment ;
2018-09-17 00:39:32 +02:00
}
return this . active . miscFullscreenSettings . videoFloat ;
}
2018-08-05 23:48:56 +02:00
}