-
-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathconfig.ts
190 lines (178 loc) · 6.49 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
// 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"
type StorageMap = browser.storage.StorageMap
// make a naked object
function o(object){
return Object.assign(Object.create(null),object)
}
// TODO: have list of possibilities for settings, e.g. hintmode: reverse | normal
let USERCONFIG = o({})
const DEFAULTS = o({
"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
"^": "scrollto 0 x",
"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",
"M": "gobble 1 quickmark",
"xx": "something",
// "B": "fillcmdline bufferall",
"b": "fillcmdline buffer",
"ZZ": "qall",
"f": "hint",
"F": "hint -b",
";i": "hint -i",
";I": "hint -I",
";y": "hint -y",
";p": "hint -p",
";r": "hint -r",
";;": "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",
}),
"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",
"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
"vimium-gi": true,
})
// currently only supports 2D or 1D storage
export function get(target, property?){
if (property !== undefined){
if (USERCONFIG[target] !== undefined){
return USERCONFIG[target][property] || DEFAULTS[target][property]
}
else return DEFAULTS[target][property]
}
// only merge "proper" objects, not arrays
if (Array.isArray(DEFAULTS[target])) return USERCONFIG[target] || DEFAULTS[target]
if (typeof DEFAULTS[target] === "object") return Object.assign(o({}),DEFAULTS[target],USERCONFIG[target])
else return USERCONFIG[target] || DEFAULTS[target]
}
// if you don't specify a property and you should, this will wipe everything
export function set(target, value, property?){
if (property !== undefined){
if (USERCONFIG[target] === undefined) USERCONFIG[target] = o({})
USERCONFIG[target][property] = value
} else USERCONFIG[target] = value
// Always save
save(get("storageloc"))
}
export function unset(target, property?){
if (property !== undefined){
delete USERCONFIG[target][property]
} else delete USERCONFIG[target]
save(get("storageloc"))
}
export async function save(storage: "local" | "sync" = "sync"){
// 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)
}
// Read all user configuration on start
// Legacy config gets loaded first
let legacy_nmaps = {}
browser.storage.sync.get("nmaps").then(nmaps => {
legacy_nmaps = nmaps["nmaps"]
browser.storage.sync.get(CONFIGNAME).then(settings => {
schlepp(settings[CONFIGNAME])
// Local storage overrides sync
browser.storage.local.get(CONFIGNAME).then(settings => {
schlepp(settings[CONFIGNAME])
USERCONFIG["nmaps"] = Object.assign(legacy_nmaps, USERCONFIG["nmaps"])
})
})
})
function schlepp(settings){
// "Import" is a reserved word so this will have to do
Object.assign(USERCONFIG,settings)
}
browser.storage.onChanged.addListener(
(changes, areaname) => {
if (CONFIGNAME in changes) {
USERCONFIG = changes[CONFIGNAME].newValue
}
}
)