forked from tridactyl/tridactyl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
366 lines (333 loc) · 11.5 KB
/
config.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Sketch
//
// Need an easy way of getting and setting settings
// If a setting is not set, the default should probably be returned.
// That probably means that binds etc. should be per-key?
//
// We should probably store all settings in memory, and only load from storage on startup and when we set it
//
// Really, we'd like a way of just letting things use the variables
//
const CONFIGNAME = "userconfig"
const WAITERS = []
let INITIALISED = false
// make a naked object
function o(object){
return Object.assign(Object.create(null),object)
}
// "Import" is a reserved word so this will have to do
function schlepp(settings){
Object.assign(USERCONFIG,settings)
}
// TODO: have list of possibilities for settings, e.g. hintmode: reverse | normal
let USERCONFIG = o({})
const DEFAULTS = o({
"configversion": "0.0",
"nmaps": o({
"o": "fillcmdline open",
"O": "current_url open",
"w": "fillcmdline winopen",
"W": "current_url winopen",
"t": "fillcmdline tabopen",
"]]": "followpage next",
"[[": "followpage prev",
"[c": "urlincrement -1",
"]c": "urlincrement 1",
"T": "current_url tabopen",
"yy": "clipboard yank",
"ys": "clipboard yankshort",
"yc": "clipboard yankcanon",
"gh": "home",
"gH": "home true",
"p": "clipboard open",
"P": "clipboard tabopen",
"j": "scrollline 10",
"k": "scrollline -10",
"h": "scrollpx -50",
"l": "scrollpx 50",
"G": "scrollto 100",
"gg": "scrollto 0",
"$": "scrollto 100 x",
// "0": "scrollto 0 x", // will get interpreted as a count
"^": "buffer #",
"H": "back",
"L": "forward",
"d": "tabclose",
"u": "undo",
"r": "reload",
"R": "reloadhard",
"gi": "focusinput -l",
"gt": "tabnext_gt",
"gT": "tabprev",
"g^": "tabfirst",
"g$": "tablast",
"gr": "reader",
"gu": "urlparent",
"gU": "urlroot",
":": "fillcmdline",
"s": "fillcmdline open search",
"S": "fillcmdline tabopen search",
// find mode not suitable for general consumption yet.
// "/": "find",
// "?": "find -1",
// "n": "findnext 1",
// "N": "findnext -1",
"M": "gobble 1 quickmark",
// "B": "fillcmdline bufferall",
"b": "fillcmdline buffer",
"ZZ": "qall",
"f": "hint",
"F": "hint -b",
";i": "hint -i",
";I": "hint -I",
";k": "hint -k",
";y": "hint -y",
";p": "hint -p",
";r": "hint -r",
";s": "hint -s",
";S": "hint -S",
";a": "hint -a",
";A": "hint -A",
";;": "hint -;",
";#": "hint -#",
"I": "mode ignore",
"a": "current_url bmark",
"A": "bmark",
"zi": "zoom 0.1 true",
"zo": "zoom -0.1 true",
"zz": "zoom 1",
".": "repeat",
}),
"autocmds": o({
"DocStart": o({
"addons.mozilla.org": "mode ignore"
}),
}),
"exaliases": o({
"alias": "command",
"au": "autocmd",
"b": "buffer",
"o": "open",
"w": "winopen",
"t": "tabopen",
"tn": "tabnext_gt",
"bn": "tabnext_gt",
"tnext": "tabnext_gt",
"bnext": "tabnext_gt",
"tp": "tabprev",
"tN": "tabprev",
"bp": "tabprev",
"bN": "tabprev",
"tprev": "tabprev",
"bprev": "tabprev",
"bfirst": "tabfirst",
"blast": "tablast",
"tfirst": "tabfirst",
"tlast": "tablast",
"bd": "tabclose",
"bdelete": "tabclose",
}),
followpagepatterns: o({
next: "^(next|newer)\\b|»|>>",
prev: "^(prev(ious)?|older)\\b|«|<<",
}),
"searchengine": "google",
"searchurls": o({
"google":"https://www.google.com/search?q=",
"scholar":"https://scholar.google.com/scholar?q=",
"googleuk":"https://www.google.co.uk/search?q=",
"bing":"https://www.bing.com/search?q=",
"duckduckgo":"https://duckduckgo.com/?q=",
"yahoo":"https://search.yahoo.com/search?p=",
"twitter":"https://twitter.com/search?q=",
"wikipedia":"https://en.wikipedia.org/wiki/Special:Search/",
"youtube":"https://www.youtube.com/results?search_query=",
"amazon":"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=",
"amazonuk":"https://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=",
"startpage":"https://startpage.com/do/search?language=english&cat=web&query=",
"github":"https://github.com/search?utf8=✓&q=",
"searx":"https://searx.me/?category_general=on&q=",
"cnrtl":"http://www.cnrtl.fr/lexicographie/",
"osm":"https://www.openstreetmap.org/search?query=",
"mdn":"https://developer.mozilla.org/en-US/search?q=",
"gentoo_wiki":"https://wiki.gentoo.org/index.php?title=Special%3ASearch&profile=default&fulltext=Search&search=",
"qwant":"https://www.qwant.com/?q=",
}),
"newtab": "",
"storageloc": "sync",
"homepages": [],
"hintchars": "hjklasdfgyuiopqwertnmzxcvb",
"hintfiltermode": "simple", // "simple", "vimperator", "vimperator-reflow"
tabopenpos: 'next',
relatedopenpos: 'related',
"ttsvoice": "default", // chosen from the listvoices list, or "default"
"ttsvolume": 1, // 0 to 1
"ttsrate": 1, // 0.1 to 10
"ttspitch": 1, // 0 to 2
// either "nextinput" or "firefox"
// If nextinput, <Tab> after gi brings selects the next input
// If firefox, <Tab> selects the next selectable element, e.g. a link
"gimode": "nextinput", // either "nextinput" or "firefox"
"theme": "default", // currently available: "default", "dark"
// Default logging levels - 2 === WARNING
"logging": o({
"messaging": 2,
"cmdline": 2,
"controller": 2,
"hinting": 2,
"state": 2,
"excmd": 1,
}),
})
/** Given an object and a target, extract the target if it exists, else return undefined
@param target path of properties as an array
*/
function getDeepProperty(obj, target) {
if (obj !== undefined && target.length) {
return getDeepProperty(obj[target[0]], target.slice(1))
} else {
return obj
}
}
/** Create the key path target if it doesn't exist and set the final property to value.
If the path is an empty array, replace the obj.
@param target path of properties as an array
*/
function setDeepProperty(obj, value, target) {
if (target.length > 1) {
// If necessary antecedent objects don't exist, create them.
if (obj[target[0]] === undefined) {
obj[target[0]] = o({})
}
return setDeepProperty(obj[target[0]], value, target.slice(1))
} else {
obj[target[0]] = value
}
}
/** Get the value of the key target.
If the user has not specified a key, use the corresponding key from
defaults, if one exists, else undefined.
*/
export function get(...target) {
const user = getDeepProperty(USERCONFIG, target)
const defult = getDeepProperty(DEFAULTS, target)
// Merge results if there's a default value and it's not an Array or primitive.
if (defult && (! Array.isArray(defult) && typeof defult === "object")) {
return Object.assign(o({}), defult, user)
} else {
if (user !== undefined) {
return user
} else {
return defult
}
}
}
/** Get the value of the key target, but wait for config to be loaded from the
database first if it has not been at least once before.
This is useful if you are a content script and you've just been loaded.
*/
export async function getAsync(...target) {
if (INITIALISED) {
return get(...target)
} else {
return new Promise((resolve) =>
WAITERS.push(() => resolve(get(...target)))
)
}
}
/** Full target specification, then value
e.g.
set("nmaps", "o", "open")
set("search", "default", "google")
set("aucmd", "BufRead", "memrise.com", "open memrise.com")
*/
export function set(...args) {
if (args.length < 2) {
throw "You must provide at least two arguments!"
}
const target = args.slice(0, args.length - 1)
const value = args[args.length - 1]
setDeepProperty(USERCONFIG, value, target)
save()
}
/** Delete the key at target if it exists */
export function unset(...target) {
const parent = getDeepProperty(USERCONFIG, target.slice(0, -1))
if (parent !== undefined) delete parent[target[target.length - 1]]
save()
}
/** Save the config back to storage API.
Config is not synchronised between different instances of this module until
sometime after this happens.
*/
export async function save(storage: "local" | "sync" = get("storageloc")){
// let storageobj = storage == "local" ? browser.storage.local : browser.storage.sync
// storageobj.set({CONFIGNAME: USERCONFIG})
let settingsobj = o({})
settingsobj[CONFIGNAME] = USERCONFIG
if (storage == "local") browser.storage.local.set(settingsobj)
else browser.storage.sync.set(settingsobj)
}
/** Updates the config to the latest version.
Proposed semantic for config versionning:
- x.y -> x+1.0 : major architectural changes
- x.y -> x.y+1 : renaming settings/changing their types
There's no need for an updater if you're only adding a new setting/changing
a default setting
When adding updaters, don't forget to set("configversion", newversionnumber)!
*/
export async function update() {
let updaters = {
"0.0": async () => {
try {
// Before we had a config system, we had nmaps, and we put them in the
// root namespace because we were young and bold.
let legacy_nmaps = await browser.storage.sync.get("nmaps")
if (legacy_nmaps) {
USERCONFIG["nmaps"] = Object.assign(legacy_nmaps["nmaps"], USERCONFIG["nmaps"])
}
} finally {
set("configversion", "1.0")
}
},
"1.0": () => {
let vimiumgi = getDeepProperty(USERCONFIG, "vimium-gi")
if (vimiumgi === true || vimiumgi === "true")
set("gimode", "nextinput")
else if (vimiumgi === false || vimiumgi === "false")
set("gimode", "firefox")
unset("vimium-gi")
set("configversion", "1.1")
}
}
if (!get("configversion"))
set("configversion", "0.0")
while (updaters[get("configversion")] instanceof Function) {
await updaters[get("configversion")]()
}
}
/** Read all user configuration from storage API then notify any waiting asynchronous calls
asynchronous calls generated by getAsync.
*/
async function init() {
let syncConfig = await browser.storage.sync.get(CONFIGNAME)
schlepp(syncConfig[CONFIGNAME])
// Local storage overrides sync
let localConfig = await browser.storage.local.get(CONFIGNAME)
schlepp(localConfig[CONFIGNAME])
await update()
INITIALISED = true
for (let waiter of WAITERS) {
waiter()
}
}
// Listen for changes to the storage and update the USERCONFIG if appropriate.
// TODO: BUG! Sync and local storage are merged at startup, but not by this thing.
browser.storage.onChanged.addListener(
(changes, areaname) => {
if (CONFIGNAME in changes) {
USERCONFIG = changes[CONFIGNAME].newValue
}
}
)
init()