2018-12-30 23:16:09 +01:00
import Debug from '../conf/Debug' ;
import currentBrowser from '../conf/BrowserDetect' ;
import ExtensionConf from '../conf/ExtensionConf' ;
2019-01-20 23:01:45 +01:00
import ExtensionMode from '../../common/enums/extension-mode.enum' ;
2019-01-02 20:36:00 +01:00
import ObjectCopy from '../lib/ObjectCopy' ;
2019-02-13 23:58:19 +01:00
import Stretch from '../../common/enums/stretch.enum' ;
import VideoAlignment from '../../common/enums/video-alignment.enum' ;
2019-07-05 23:45:29 +02:00
import ExtensionConfPatch from '../conf/ExtConfPatches' ;
2019-10-24 23:27:43 +02:00
import CropModePersistence from '../../common/enums/crop-mode-persistence.enum' ;
2018-12-30 23:16:09 +01:00
2020-04-13 15:20:29 +02:00
if ( process . env . CHANNEL !== 'stable' ) {
console . log ( "Loading Settings" ) ;
}
2019-06-03 00:37:19 +02:00
2018-08-05 23:48:56 +02:00
class Settings {
2019-09-03 00:28:35 +02:00
constructor ( options ) {
// Options: activeSettings, updateCallback, logger
2019-09-03 23:01:23 +02:00
this . logger = options . logger ;
2020-02-09 01:16:46 +01:00
this . onSettingsChanged = options . onSettingsChanged ;
this . active = options . activeSettings ? ? undefined ;
2018-08-05 23:48:56 +02:00
this . default = ExtensionConf ;
2019-08-25 01:52:04 +02:00
this . default [ 'version' ] = this . getExtensionVersion ( ) ;
2018-08-21 23:48:47 +02:00
this . useSync = false ;
2018-08-22 22:34:07 +02:00
this . version = undefined ;
2018-08-23 01:04:37 +02:00
2019-10-24 00:44:27 +02:00
if ( currentBrowser . firefox ) {
2019-10-27 00:10:49 +02:00
browser . storage . onChanged . addListener ( ( changes , area ) => { this . storageChangeListener ( changes , area ) } ) ;
2018-12-30 23:16:09 +01:00
} else if ( currentBrowser . chrome ) {
2019-10-27 00:10:49 +02:00
chrome . storage . onChanged . addListener ( ( changes , area ) => { this . storageChangeListener ( changes , area ) } ) ;
2019-10-24 00:44:27 +02:00
}
}
storageChangeListener ( changes , area ) {
2020-01-29 01:36:09 +01:00
if ( ! changes . uwSettings ) {
return ;
}
2019-10-24 00:44:27 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:" , changes , "storage area:" , area ) ;
2020-02-09 01:16:46 +01:00
// if (changes['uwSettings'] && changes['uwSettings'].newValue) {
// this.logger.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(changes.uwSettings.newValue));
// }
2019-10-24 21:13:45 +02:00
const parsedSettings = JSON . parse ( changes . uwSettings . newValue ) ;
2020-02-09 01:16:46 +01:00
this . setActive ( parsedSettings ) ;
this . logger . log ( 'info' , 'debug' , 'Does parsedSettings.preventReload exist?' , parsedSettings . preventReload , "Does callback exist?" , ! ! this . onSettingsChanged ) ;
2019-10-24 00:44:27 +02:00
2020-02-09 01:16:46 +01:00
if ( ! parsedSettings . preventReload && this . onSettingsChanged ) {
2019-10-24 00:44:27 +02:00
try {
2020-02-09 01:16:46 +01:00
this . onSettingsChanged ( ) ;
this . logger . log ( 'info' , 'settings' , '[Settings] Update callback finished.' )
2019-10-24 00:44:27 +02:00
} catch ( e ) {
2020-02-09 01:16:46 +01:00
this . logger . log ( 'error' , 'settings' , "[Settings] CALLING UPDATE CALLBACK FAILED. Reason:" , e )
2019-10-24 00:44:27 +02:00
}
2018-08-23 01:04:37 +02:00
}
2018-08-05 23:48:56 +02:00
}
2020-03-02 22:35:34 +01:00
static getExtensionVersion ( ) {
2019-08-25 01:52:04 +02:00
if ( currentBrowser . firefox ) {
return browser . runtime . getManifest ( ) . version ;
} else if ( currentBrowser . chrome ) {
return chrome . runtime . getManifest ( ) . version ;
2020-03-02 22:35:34 +01:00
}
}
getExtensionVersion ( ) {
return Settings . getExtensionVersion ( ) ;
2019-08-25 01:52:04 +02:00
}
2019-09-03 00:28:35 +02:00
compareExtensionVersions ( a , b ) {
2019-09-17 22:14:42 +02:00
let aa = a . split ( '.' ) ;
let bb = b . split ( '.' ) ;
2019-09-03 00:28:35 +02:00
if ( + aa [ 0 ] !== + bb [ 0 ] ) {
// difference on first digit
2019-09-17 22:14:42 +02:00
return + aa [ 0 ] - + bb [ 0 ] ;
2019-09-03 00:28:35 +02:00
} if ( + aa [ 1 ] !== + bb [ 1 ] ) {
// first digit same, difference on second digit
2019-09-17 22:14:42 +02:00
return + aa [ 1 ] - + bb [ 1 ] ;
2019-09-03 00:28:35 +02:00
} if ( + aa [ 2 ] !== + bb [ 2 ] ) {
2019-09-17 22:14:42 +02:00
return + aa [ 2 ] - + bb [ 2 ] ;
2019-09-03 00:28:35 +02:00
// first two digits the same, let's check the third digit
} else {
// fourth digit is optional. When not specified, 0 is implied
// btw, ++(aa[3] || 0) - ++(bb[3] || 0) doesn't work
2019-09-17 22:14:42 +02:00
// Since some things are easier if we actually have a value for
// the fourth digit, we turn a possible undefined into a zero
aa [ 3 ] = aa [ 3 ] === undefined ? 0 : aa [ 3 ] ;
bb [ 3 ] = bb [ 3 ] === undefined ? 0 : bb [ 3 ] ;
// also, the fourth digit can start with a letter.
// versions that start with a letter are ranked lower than
// versions x.x.x.0
if ( isNaN ( + aa [ 3 ] ) ^ isNaN ( + bb [ 3 ] ) ) {
return isNaN ( + aa [ 3 ] ) ? - 1 : 1 ;
}
// at this point, either both version numbers are a NaN or
// both versions are a number.
if ( ! isNaN ( + aa [ 3 ] ) ) {
return + aa [ 3 ] - + bb [ 3 ] ;
}
// letters have their own hierarchy:
// dev < a < b < rc
let av = this . getPrereleaseVersionHierarchy ( aa [ 3 ] ) ;
let bv = this . getPrereleaseVersionHierarchy ( bb [ 3 ] ) ;
if ( av !== bv ) {
return av - bv ;
} else {
return + ( aa [ 3 ] . replace ( /\D/g , '' ) ) - + ( bb [ 3 ] . replace ( /\D/g , '' ) ) ;
}
}
}
getPrereleaseVersionHierarchy ( version ) {
if ( version . startsWith ( 'dev' ) ) {
return 0 ;
2019-09-03 00:28:35 +02:00
}
2019-09-17 22:14:42 +02:00
if ( version . startsWith ( 'a' ) ) {
return 1 ;
}
if ( version . startsWith ( 'b' ) ) {
return 2 ;
}
return 3 ;
2019-09-03 00:28:35 +02:00
}
2019-09-17 22:14:42 +02:00
2019-09-03 00:28:35 +02:00
sortConfPatches ( patchesIn ) {
2019-09-17 22:14:42 +02:00
return patchesIn . sort ( ( a , b ) => this . compareExtensionVersions ( a . forVersion , b . forVersion ) ) ;
2019-09-03 00:28:35 +02:00
}
findFirstNecessaryPatch ( version , extconfPatches ) {
const sorted = this . sortConfPatches ( extconfPatches ) ;
2019-09-17 22:14:42 +02:00
return sorted . findIndex ( x => this . compareExtensionVersions ( x . forVersion , version ) > 0 ) ;
2019-09-03 00:28:35 +02:00
}
applySettingsPatches ( oldVersion , patches ) {
let index = this . findFirstNecessaryPatch ( oldVersion , patches ) ;
if ( index === - 1 ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , '[Settings::applySettingsPatches] There are no pending conf patches.' ) ;
2019-09-03 00:28:35 +02:00
return ;
}
// apply all remaining patches
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , ` [Settings::applySettingsPatches] There are ${ patches . length - index } settings patches to apply ` ) ;
while ( index < patches . length ) {
2019-11-02 01:05:36 +01:00
const updateFn = patches [ index ] . updateFn ;
2019-09-03 00:28:35 +02:00
delete patches [ index ] . forVersion ;
2019-11-02 01:05:36 +01:00
delete patches [ index ] . updateFn ;
if ( Object . keys ( patches [ index ] ) . length > 0 ) {
ObjectCopy . overwrite ( this . active , patches [ index ] ) ;
}
if ( updateFn ) {
try {
updateFn ( this . active , this . getDefaultSettings ( ) ) ;
} catch ( e ) {
this . logger . log ( 'error' , 'settings' , '[Settings::applySettingsPatches] Failed to execute update function. Keeping settings object as-is. Error:' , e ) ;
}
}
2019-09-17 22:14:42 +02:00
index ++ ;
}
2019-09-03 00:28:35 +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 ( ) ;
2019-09-17 22:14:42 +02:00
this . version = this . getExtensionVersion ( ) ;
2019-08-28 18:28:22 +02:00
// |—> on first setup, settings is undefined & settings.version is haram
2019-08-31 18:22:13 +02:00
// | since new installs ship with updates by default, no patching is
// | needed. In this case, we assume we're on the current version
2019-09-17 22:14:42 +02:00
const oldVersion = ( settings && settings . version ) || this . version ;
2018-08-05 23:48:56 +02:00
2019-10-27 00:10:49 +02:00
if ( settings ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings::init] Configuration fetched from storage:" , settings ,
2019-09-03 22:17:10 +02:00
"\nlast saved with:" , settings . version ,
2019-09-17 22:14:42 +02:00
"\ncurrent version:" , this . version
2019-09-03 22:17:10 +02:00
) ;
2018-08-05 23:48:56 +02:00
}
2019-10-27 00:10:49 +02:00
// if (Debug.flushStoredSettings) {
// this.logger.log('info', 'settings', "%c[Settings::init] Debug.flushStoredSettings is true. Using default settings", "background: #d00; color: #ffd");
// Debug.flushStoredSettings = false; // don't do it again this session
// this.active = this.getDefaultSettings();
// this.active.version = this.version;
// this.set(this.active);
// return this.active;
// }
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 ) ) {
2019-09-25 07:10:36 +02:00
this . logger . log (
'info' ,
'settings' ,
'[Settings::init] settings don\'t exist. Using defaults.\n#keys:' ,
2019-09-25 09:34:14 +02:00
settings ? Object . keys ( settings ) . length : 0 ,
2019-09-25 07:10:36 +02:00
'\nsettings:' ,
settings
) ;
2018-08-05 23:48:56 +02:00
this . active = this . getDefaultSettings ( ) ;
2019-09-17 22:14:42 +02:00
this . active . version = this . version ;
await this . save ( ) ;
2018-08-05 23:48:56 +02:00
return this . active ;
}
2019-09-17 22:14:42 +02:00
// if there's settings, set saved object as active settings
this . active = settings ;
2019-06-05 23:36:47 +02:00
// if last saved settings was for version prior to 4.x, we reset settings to default
// it's not like people will notice cos that version didn't preserve settings at all
2019-09-17 22:14:42 +02:00
if ( this . active . version && ! settings . version . startsWith ( '4' ) ) {
2019-06-05 23:36:47 +02:00
this . active = this . getDefaultSettings ( ) ;
2019-09-17 22:14:42 +02:00
this . active . version = this . version ;
await this . save ( ) ;
2019-06-05 23:36:47 +02:00
return this . active ;
}
2019-08-31 18:22:13 +02:00
2019-09-17 22:14:42 +02:00
// if version number is undefined, we make it defined
// this should only happen on first extension initialization
if ( ! this . active . version ) {
this . active . version = this . version ;
await this . save ( ) ;
return this . active ;
}
2019-08-25 01:52:04 +02:00
2019-09-17 22:14:42 +02:00
// check if extension has been updated. If not, return settings as they were retrieved
if ( this . active . version === this . version ) {
this . logger . log ( 'info' , 'settings' , "[Settings::init] extension was saved with current version of ultrawidify. Returning object as-is." ) ;
2018-08-05 23:48:56 +02:00
return this . active ;
}
2019-09-17 22:14:42 +02:00
// This means extension update happened.
// btw fun fact — we can do version rollbacks, which might come in handy while testing
this . active . version = this . version ;
2018-08-05 23:48:56 +02:00
// 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.
2019-06-05 23:36:47 +02:00
const patched = ObjectCopy . addNew ( settings , this . default ) ;
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings.init] Results from ObjectCopy.addNew()?" , patched , "\n\nSettings from storage" , settings , "\ndefault?" , this . default ) ;
2019-06-05 23:36:47 +02:00
2019-09-17 22:14:42 +02:00
if ( patched ) {
2019-06-05 23:36:47 +02:00
this . active = patched ;
}
2018-08-22 22:34:07 +02:00
2019-07-05 23:45:29 +02:00
// in case settings in previous version contained a fucky wucky, we overwrite existing settings with a patch
2019-09-03 00:28:35 +02:00
this . applySettingsPatches ( oldVersion , ExtensionConfPatch ) ;
2019-07-05 23:45:29 +02:00
2019-09-03 00:28:35 +02:00
// set 'whatsNewChecked' flag to false when updating, always
2019-07-07 15:12:15 +02:00
this . active . whatsNewChecked = false ;
2019-08-25 01:52:04 +02:00
// update settings version to current
2019-09-17 22:14:42 +02:00
this . active . version = this . version ;
2019-07-07 15:12:15 +02:00
2019-09-17 22:14:42 +02:00
await this . save ( ) ;
2018-08-05 23:48:56 +02:00
return this . active ;
}
async get ( ) {
2019-05-10 19:21:17 +02:00
let ret ;
2018-12-30 23:16:09 +01:00
if ( currentBrowser . firefox ) {
2019-05-10 19:21:17 +02:00
ret = await browser . storage . local . get ( 'uwSettings' ) ;
2018-12-30 23:16:09 +01:00
} else if ( currentBrowser . chrome ) {
2019-05-10 19:21:17 +02:00
ret = await new Promise ( ( resolve , reject ) => {
2019-05-03 00:58:06 +02:00
chrome . storage . local . get ( 'uwSettings' , ( res ) => resolve ( res ) ) ;
2018-08-29 00:41:26 +02:00
} ) ;
2018-12-30 23:16:09 +01:00
} else if ( currentBrowser . edge ) {
2019-05-10 19:21:17 +02:00
ret = await new Promise ( ( resolve , reject ) => {
2019-05-03 00:58:06 +02:00
browser . storage . local . get ( 'uwSettings' , ( res ) => resolve ( res ) ) ;
2018-09-25 23:37:08 +02:00
} ) ;
2019-05-10 19:21:17 +02:00
}
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , 'Got settings:' , ret && ret . uwSettings && JSON . parse ( ret . uwSettings ) ) ;
2019-06-08 03:45:35 +02:00
2019-05-10 19:21:17 +02:00
try {
return JSON . parse ( ret . uwSettings ) ;
} catch ( e ) {
return undefined ;
2018-08-05 23:48:56 +02:00
}
}
2020-01-04 02:36:46 +01:00
fixSitesSettings ( sites ) {
for ( const site in sites ) {
if ( site === '@global' ) {
continue ;
}
if ( sites [ site ] . mode === undefined ) {
sites [ site ] . mode = ExtensionMode . Default ;
}
if ( sites [ site ] . autoar === undefined ) {
2020-03-01 21:52:36 +01:00
sites [ site ] . autoar = ExtensionMode . Default ;
2020-01-04 02:36:46 +01:00
}
if ( sites [ site ] . stretch === undefined ) {
2020-03-01 21:52:36 +01:00
sites [ site ] . stretch = Stretch . Default ;
2020-01-04 02:36:46 +01:00
}
if ( sites [ site ] . videoAlignment === undefined ) {
2020-03-01 21:52:36 +01:00
sites [ site ] . videoAlignment = VideoAlignment . Default ;
2020-01-04 02:36:46 +01:00
}
if ( sites [ site ] . keyboardShortcutsEnabled === undefined ) {
2020-03-01 21:52:36 +01:00
sites [ site ] . keyboardShortcutsEnabled = ExtensionMode . Default ;
2020-01-04 02:36:46 +01:00
}
}
}
2019-11-02 01:05:36 +01:00
async set ( extensionConf , options ) {
if ( ! options || ! options . forcePreserveVersion ) {
extensionConf . version = this . version ;
}
2019-09-17 22:14:42 +02:00
2020-01-04 15:46:43 +01:00
this . fixSitesSettings ( extensionConf . sites ) ;
2020-01-04 02:36:46 +01:00
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings::set] setting new settings:" , extensionConf )
2018-08-23 01:04:37 +02:00
2018-12-30 23:16:09 +01:00
if ( currentBrowser . firefox || currentBrowser . edge ) {
2019-09-17 22:14:42 +02:00
return browser . storage . local . set ( { 'uwSettings' : JSON . stringify ( extensionConf ) } ) ;
2018-12-30 23:16:09 +01:00
} else if ( currentBrowser . chrome ) {
2019-05-03 00:58:06 +02:00
return chrome . storage . local . 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 ;
}
2019-11-02 01:05:36 +01:00
async save ( options ) {
2019-06-08 03:45:35 +02:00
if ( Debug . debug && Debug . storage ) {
2018-08-23 01:04:37 +02:00
console . log ( "[Settings::save] Saving active settings:" , this . active ) ;
}
2019-10-24 21:13:45 +02:00
this . active . preventReload = undefined ;
2019-11-02 01:05:36 +01:00
await this . set ( this . active , options ) ;
2018-08-07 23:31:28 +02:00
}
2019-10-24 00:44:27 +02:00
async saveWithoutReload ( ) {
2019-10-24 21:13:45 +02:00
this . active . preventReload = true ;
await this . set ( this . active ) ;
2019-10-24 00:44:27 +02:00
}
2019-04-12 00:49:56 +02:00
async rollback ( ) {
this . active = await this . get ( ) ;
}
2018-08-05 23:48:56 +02:00
getDefaultSettings ( ) {
return JSON . parse ( JSON . stringify ( this . default ) ) ;
}
// -----------------------------------------
// 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
2018-11-02 02:52:01 +01:00
// * basic — only allow fullscreen
2018-08-05 23:48:56 +02:00
// * default — allow if default is to allow, block if default is to block
// * disabled — never allow
2018-09-17 00:39:32 +02:00
2018-11-18 03:29:16 +01:00
getActionsForSite ( site ) {
if ( ! site ) {
return this . active . actions ;
}
if ( this . active . sites [ site ] && this . active . sites [ site ] . actions && this . active . sites [ site ] . actions . length > 0 ) {
return this . active . sites [ site ] . actions ;
}
return this . active . actions ;
}
2018-11-02 02:52:01 +01:00
getExtensionMode ( site ) {
if ( ! site ) {
site = window . location . hostname ;
if ( ! site ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , ` [Settings::canStartExtension] window.location.hostname is null or undefined: ${ window . location . hostname } \n active settings: ` , this . active ) ;
2018-11-02 02:52:01 +01:00
return ExtensionMode . Disabled ;
}
}
try {
// if site-specific settings don't exist for the site, we use default mode:
if ( ! this . active . sites [ site ] ) {
2019-06-05 23:36:47 +02:00
return this . getExtensionMode ( '@global' ) ;
2018-11-02 02:52:01 +01:00
}
2019-01-20 23:36:08 +01:00
if ( this . active . sites [ site ] . mode === ExtensionMode . Enabled ) {
2019-01-20 22:59:06 +01:00
return ExtensionMode . Enabled ;
2019-01-20 23:36:08 +01:00
} else if ( this . active . sites [ site ] . mode === ExtensionMode . Basic ) {
2019-06-05 23:36:47 +02:00
return ExtensionMode . Basic ;
2020-01-04 02:36:46 +01:00
} else if ( this . active . sites [ site ] . mode === ExtensionMode . Disabled ) {
2018-11-02 02:52:01 +01:00
return ExtensionMode . Disabled ;
2020-01-04 02:36:46 +01:00
} else {
if ( site !== '@global' ) {
return this . getExtensionMode ( '@global' ) ;
} else {
return ExtensionMode . Disabled ;
}
2018-11-02 02:52:01 +01:00
}
} catch ( e ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'error' , 'settings' , "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\n\nerror:" , e , "\n\nSettings object:" , this )
2019-09-03 22:17:10 +02:00
2018-11-02 02:52:01 +01:00
return ExtensionMode . Disabled ;
}
}
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 ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , ` [Settings::canStartExtension] window.location.hostname is null or undefined: ${ window . location . hostname } \n active settings: ` , this . active ) ;
2018-08-05 23:48:56 +02:00
return false ;
}
}
2018-11-02 02:52:01 +01:00
// 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:
2019-07-03 22:35:17 +02:00
if ( ! this . active . sites [ site ] || this . active . sites [ site ] . mode === ExtensionMode . Default ) {
2019-06-10 23:45:15 +02:00
return this . active . sites [ '@global' ] . mode === ExtensionMode . Enabled ;
}
2018-08-05 23:48:56 +02:00
2019-06-15 22:58:19 +02:00
if ( this . active . sites [ '@global' ] . mode === ExtensionMode . Enabled ) {
2019-06-10 23:45:15 +02:00
return this . active . sites [ site ] . mode !== ExtensionMode . Disabled ;
} else if ( this . active . sites [ '@global' ] . mode === ExtensionMode . Whitelist ) {
return this . active . sites [ site ] . mode === ExtensionMode . Enabled ;
} else {
return false ;
}
2019-09-03 22:17:10 +02:00
} catch ( e ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'error' , 'settings' , "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\nSettings object:" , this ) ;
2018-08-22 22:34:07 +02:00
return false ;
}
2018-08-05 23:48:56 +02:00
}
2019-06-02 23:54:32 +02:00
keyboardShortcutsEnabled ( site ) {
if ( ! site ) {
site = window . location . hostname ;
}
if ( ! site ) {
return false ;
}
try {
if ( ! this . active . sites [ site ]
|| this . active . sites [ site ] . keyboardShortcutsEnabled === undefined
|| this . active . sites [ site ] . keyboardShortcutsEnabled === ExtensionMode . Default ) {
return this . keyboardShortcutsEnabled ( '@global' ) ;
} else {
return this . active . sites [ site ] . keyboardShortcutsEnabled === ExtensionMode . Enabled ;
}
} catch ( e ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings.js::keyboardDisabled] something went wrong:" , e ) ;
2019-06-02 23:54:32 +02:00
return false ;
}
}
2018-09-16 14:14:16 +02:00
extensionEnabled ( ) {
2019-01-20 23:36:08 +01:00
return this . active . sites [ '@global' ] !== ExtensionMode . Disabled
2018-09-16 14:14:16 +02:00
}
extensionEnabledForSite ( site ) {
return this . canStartExtension ( site ) ;
}
2018-08-05 23:48:56 +02:00
canStartAutoAr ( site ) {
2019-05-26 02:53:29 +02:00
// 'site' argument is only ever used when calling this function recursively for debugging
2018-08-05 23:48:56 +02:00
if ( ! site ) {
2019-02-15 00:00:22 +01:00
site = window . location . host ;
2018-08-05 23:48:56 +02:00
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 ;
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "[Settings::canStartAutoAr] ----------------\nCAN WE START AUTOAR ON SITE" , site ,
2019-09-03 22:17:10 +02:00
"?\n\nsettings.active.sites[site]=" , this . active . sites [ site ] , "settings.active.sites[@global]=" , this . active . sites [ '@global' ] ,
"\nAutoar mode (global)?" , this . active . sites [ '@global' ] . autoar ,
` \n Autoar mode ( ${ site } ) ` , this . active . sites [ site ] ? this . active . sites [ site ] . autoar : '<not defined>' ,
"\nCan autoar be started?" , csar
) ;
2018-08-05 23:48:56 +02:00
}
// if site is not defined, we use default mode:
if ( ! this . active . sites [ site ] ) {
2019-02-15 00:00:22 +01:00
return this . active . sites [ '@global' ] . autoar === ExtensionMode . Enabled ;
2018-08-05 23:48:56 +02:00
}
2019-02-15 00:00:22 +01:00
if ( this . active . sites [ '@global' ] . autoar === ExtensionMode . Enabled ) {
2019-01-20 23:36:08 +01:00
return this . active . sites [ site ] . autoar !== ExtensionMode . Disabled ;
} else if ( this . active . sites [ '@global' ] . autoar === ExtensionMode . Whitelist ) {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "canStartAutoAr — can(not) start csar because extension is in whitelist mode, and this site is (not) equal to" , ExtensionMode . Enabled )
2019-02-15 00:00:22 +01:00
return this . active . sites [ site ] . autoar === ExtensionMode . Enabled ;
2018-08-05 23:48:56 +02:00
} else {
2019-09-17 22:14:42 +02:00
this . logger . log ( 'info' , 'settings' , "canStartAutoAr — cannot start csar because extension is globally disabled" )
2018-08-05 23:48:56 +02:00
return false ;
}
}
2018-09-17 00:39:32 +02:00
2019-02-13 23:58:19 +01:00
getDefaultOption ( option ) {
const allDefault = {
mode : ExtensionMode . Default ,
autoar : ExtensionMode . Default ,
autoarFallback : ExtensionMode . Default ,
stretch : Stretch . Default ,
videoAlignment : VideoAlignment . Default ,
} ;
if ( ! option || allDefault [ option ] === undefined ) {
return allDefault ;
}
return allDefault [ option ] ;
}
2018-09-17 00:39:32 +02:00
getDefaultAr ( site ) {
2019-02-15 20:40:56 +01:00
// site = this.getSiteSettings(site);
2018-09-17 00:39:32 +02:00
2019-02-15 20:40:56 +01:00
// if (site.defaultAr) {
// return site.defaultAr;
// }
2018-12-03 00:31:28 +01:00
return this . active . miscSettings . defaultAr ;
2018-09-17 00:39:32 +02:00
}
2018-09-18 00:40:05 +02:00
getDefaultStretchMode ( site ) {
2020-01-28 23:34:36 +01:00
if ( site && this . active . sites [ site ] ? . stretch !== Stretch . Default ) {
2019-02-15 20:40:56 +01:00
return this . active . sites [ site ] . stretch ;
2018-09-17 00:39:32 +02:00
}
2019-02-15 20:40:56 +01:00
2019-01-20 23:05:04 +01:00
return this . active . sites [ '@global' ] . stretch ;
2018-09-17 00:39:32 +02:00
}
2019-10-24 23:27:43 +02:00
getDefaultCropPersistenceMode ( site ) {
2020-01-28 23:34:36 +01:00
if ( site && this . active . sites [ site ] ? . cropModePersistence !== Stretch . Default ) {
2019-10-24 23:27:43 +02:00
return this . active . sites [ site ] . cropModePersistence ;
}
// persistence mode thing is missing from settings by default
return this . active . sites [ '@global' ] . cropModePersistence || CropModePersistence . Disabled ;
}
2018-09-17 00:39:32 +02:00
getDefaultVideoAlignment ( site ) {
2020-01-28 23:34:36 +01:00
if ( this . active . sites [ site ] ? . videoAlignment !== VideoAlignment . Default ) {
2019-02-15 20:40:56 +01:00
return this . active . sites [ site ] . videoAlignment ;
2018-09-17 00:39:32 +02:00
}
2019-02-15 20:40:56 +01:00
2019-01-20 23:05:04 +01:00
return this . active . sites [ '@global' ] . videoAlignment ;
2018-09-17 00:39:32 +02:00
}
2018-08-05 23:48:56 +02:00
}
2018-12-30 23:16:09 +01:00
2018-12-31 01:03:07 +01:00
export default Settings ;