2017-11-29 16:28:06 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:13:40 +00:00
|
|
|
// TODO: have list of possibilities for settings, e.g. hintmode: reverse | normal
|
2017-11-29 16:28:06 +00:00
|
|
|
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",
|
|
|
|
"p": "clipboard open",
|
|
|
|
"P": "clipboard tabopen",
|
|
|
|
"j": "scrollline 10",
|
|
|
|
"k": "scrollline -10",
|
|
|
|
"h": "scrollpx -50",
|
|
|
|
"l": "scrollpx 50",
|
|
|
|
"G": "scrollto 100",
|
|
|
|
"gg": "scrollto 0",
|
|
|
|
"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",
|
2017-11-29 16:40:16 +00:00
|
|
|
"s": "fillcmdline open search",
|
|
|
|
"S": "fillcmdline tabopen search",
|
2017-11-29 16:28:06 +00:00
|
|
|
"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",
|
2017-11-30 04:11:49 +00:00
|
|
|
";r": "hint -r",
|
2017-11-29 16:28:06 +00:00
|
|
|
";;": "hint -;",
|
|
|
|
";#": "hint -#",
|
|
|
|
"I": "mode ignore",
|
|
|
|
"a": "current_url bmark",
|
|
|
|
"A": "bmark",
|
2017-11-29 16:40:16 +00:00
|
|
|
}),
|
2017-11-29 19:51:18 +00:00
|
|
|
"searchengine": "google",
|
2017-11-30 11:40:01 +00:00
|
|
|
"newtab": undefined,
|
2017-11-29 19:51:18 +00:00
|
|
|
"storageloc": "sync",
|
2017-11-29 20:13:40 +00:00
|
|
|
"hintchars": "hjklasdfgyuiopqwertnmzxcvb",
|
|
|
|
"hintorder": "normal",
|
2017-11-30 04:11:49 +00:00
|
|
|
|
|
|
|
"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
|
2017-11-29 16:28:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
}
|
2017-11-29 18:57:04 +00:00
|
|
|
if (typeof DEFAULTS[target] === "object") return Object.assign(o({}),DEFAULTS[target],USERCONFIG[target])
|
2017-11-29 16:40:16 +00:00
|
|
|
else return USERCONFIG[target] || DEFAULTS[target]
|
2017-11-29 16:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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({})
|
2017-11-29 19:51:18 +00:00
|
|
|
USERCONFIG[target][property] = value
|
|
|
|
} else USERCONFIG[target] = value
|
|
|
|
// Always save
|
|
|
|
save(get("storageloc"))
|
2017-11-29 16:28:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:57:04 +00:00
|
|
|
export function unset(target, property?){
|
|
|
|
if (property !== undefined){
|
|
|
|
delete USERCONFIG[target][property]
|
|
|
|
} else delete USERCONFIG[target]
|
2017-11-29 19:51:18 +00:00
|
|
|
save(get("storageloc"))
|
2017-11-29 18:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 16:28:06 +00:00
|
|
|
export async function save(storage: "local" | "sync" = "sync"){
|
2017-11-29 16:56:56 +00:00
|
|
|
// 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)
|
2017-11-29 16:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read all user configuration on start
|
2017-11-29 19:51:18 +00:00
|
|
|
// 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"])
|
|
|
|
})
|
|
|
|
})
|
2017-11-29 16:28:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function schlepp(settings){
|
2017-11-29 16:40:16 +00:00
|
|
|
// "Import" is a reserved word so this will have to do
|
2017-11-29 19:51:18 +00:00
|
|
|
Object.assign(USERCONFIG,settings)
|
2017-11-29 16:28:06 +00:00
|
|
|
}
|
2017-11-29 21:48:46 +00:00
|
|
|
|
|
|
|
browser.storage.onChanged.addListener(
|
|
|
|
(changes, areaname) => {
|
|
|
|
if (CONFIGNAME in changes) {
|
|
|
|
Object.assign(USERCONFIG, changes[CONFIGNAME].newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|