-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
153 lines (125 loc) · 3.85 KB
/
settings.js
File metadata and controls
153 lines (125 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict"
// TODO: Make stored settings private
const storage = (() => {
let loaded = false
const load = (callback = C.Noop) => {
if (loaded) {
callback()
return
}
try {
chrome.storage.local.get("settings", (storedObj) => {
if (!loaded) {
if (storedObj?.settings) {
storage.settings = storedObj.settings
}
loaded = true
}
callback()
})
}
catch(e){}
}
const reload = (callback = C.Noop) => {
loaded = false
load(callback)
}
const set = (prop, val) => {
if (!loaded) {
console.error("Attempted to set setting before settings load finished")
}
storage.settings[prop] = val
chrome.storage.local.set({"settings" : storage.settings}, () => {
chrome.runtime.sendMessage({type : message.Type.UpdateSettings})
message.tabs.all({type : message.Type.UpdateSettings})
})
}
return {
load,
reload,
set,
settings : {},
}
})()
const settings = (() => {
// These indicate expected type for reference, checks are only by Javascript type
const DATE = 0 // in ms
const INTEGER = 0
const FLOAT = 0.0
const STRING = ""
const TRUE = true
const FALSE = false
// const ARRAY = [] // Define inline
// const OBJECT = {} // Define inline
const settingDefaultValues = Object.freeze({
// Key used to access OMDB Api. Required for ratings to work
"OmdbApiKey" : STRING,
// Key used to store OMDB results. Format: {[`title + year + type`]: {imdb, metacritic, rottenTomatoes, type, time}
"OmdbResultsHash" : {},
// Playback rate to set when video begins
"InitialPlaybackSpeed" : 1.0,
// Click 'Skip Intro' Button when found, to skip repetitive video parts
"AutoSkipIntro" : false,
})
const KEYS = Object.keys(settingDefaultValues)
.reduce((keys, key) => {
keys[key] = key
return keys
}, {})
const settingKeyExists = settingName => {
const defaultValue = settingDefaultValues[settingName]
return (defaultValue !== undefined && settingDefaultValues.hasOwnProperty(settingName))
}
const isDefaultValue = settingName => {
if (!settingKeyExists(settingName)) return false
const defaultValue = getDefaultValue(settingName)
const currentValue = getSettingValue(settingName)
return (defaultValue === currentValue)
}
const getDefaultValue = settingName => {
const exists = settingKeyExists(settingName)
return (exists)
? settingDefaultValues[settingName]
: undefined
}
const setDefaultValue = settingName => {
const exists = settingKeyExists(settingName)
const defaultSettingValue = getDefaultValue(settingName)
if (exists) {
setSettingValue(settingName, defaultSettingValue)
}
return defaultSettingValue
}
const canSetValue = (settingName, newValue) => {
if (!settingKeyExists(settingName)) return false
const newValueType = typeof newValue
const expectedObjectType = typeof getDefaultValue(settingName)
const isSameValue = (newValue === getSettingValue(settingName))
const isObjectType = (newValueType === "object")
const isSameType = (newValueType === expectedObjectType)
if (!isSameType) {
console.error(`Can't set [${settingName}] to [${newValue}]. Expects [${typeof getDefaultValue(settingName)}], got [${typeof newValue}]`)
}
return ((!isSameValue || isObjectType) && isSameType)
}
const getSettingValue = (settingName) => {
const exists = settingKeyExists(settingName)
const defaultValue = getDefaultValue(settingName)
if (!exists) return defaultValue
const settingValue = storage.settings[settingName]
const actualValue = (settingValue !== undefined) ? settingValue : defaultValue
return actualValue
}
const setSettingValue = (settingName, newValue) => {
if (!canSetValue(settingName, newValue)) return
storage.set(settingName, newValue)
}
const publicApi = {
get : getSettingValue,
set : setSettingValue,
isDefault : isDefaultValue,
Setting : Object.freeze(KEYS),
}
return publicApi
})()
const {Setting} = settings