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",
|
2017-11-30 13:50:10 +00:00
|
|
|
"gh": "home",
|
|
|
|
"gH": "home true",
|
2017-11-29 16:28:06 +00:00
|
|
|
"p": "clipboard open",
|
|
|
|
"P": "clipboard tabopen",
|
|
|
|
"j": "scrollline 10",
|
|
|
|
"k": "scrollline -10",
|
|
|
|
"h": "scrollpx -50",
|
|
|
|
"l": "scrollpx 50",
|
|
|
|
"G": "scrollto 100",
|
|
|
|
"gg": "scrollto 0",
|
2017-12-08 21:11:40 -08:00
|
|
|
"$": "scrollto 100 x",
|
|
|
|
// "0": "scrollto 0 x", // will get interpreted as a count
|
|
|
|
"^": "scrollto 0 x",
|
2017-11-29 16:28:06 +00:00
|
|
|
"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-12-04 11:50:35 +00:00
|
|
|
"zi": "zoom 0.1 true",
|
|
|
|
"zo": "zoom -0.1 true",
|
|
|
|
"zz": "zoom 1",
|
2017-12-02 12:08:30 +01:00
|
|
|
".": "repeat",
|
2017-11-29 16:40:16 +00:00
|
|
|
}),
|
2017-11-29 19:51:18 +00:00
|
|
|
"searchengine": "google",
|
2017-12-03 11:30:44 +00:00
|
|
|
"searchurls": o({
|
|
|
|
"google":"https://www.google.com/search?q=",
|
2017-12-03 12:10:25 +00:00
|
|
|
"scholar":"https://scholar.google.com/scholar?q=",
|
2017-12-03 11:30:44 +00:00
|
|
|
"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=",
|
2017-12-03 13:01:23 +00:00
|
|
|
"startpage":"https://startpage.com/do/search?language=english&cat=web&query=",
|
2017-12-03 11:30:44 +00:00
|
|
|
"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=",
|
2017-12-03 13:01:23 +00:00
|
|
|
"qwant":"https://www.qwant.com/?q=",
|
|
|
|
|
2017-12-03 11:30:44 +00:00
|
|
|
}),
|
2017-12-06 14:59:00 +00:00
|
|
|
"newtab": "",
|
2017-11-29 19:51:18 +00:00
|
|
|
"storageloc": "sync",
|
2017-11-30 13:50:10 +00:00
|
|
|
"homepages": [],
|
2017-11-29 20:13:40 +00:00
|
|
|
"hintchars": "hjklasdfgyuiopqwertnmzxcvb",
|
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-12-05 22:07:23 +00:00
|
|
|
"vimium-gi": true,
|
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-30 13:50:10 +00:00
|
|
|
// only merge "proper" objects, not arrays
|
|
|
|
if (Array.isArray(DEFAULTS[target])) return USERCONFIG[target] || DEFAULTS[target]
|
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) {
|
2017-12-05 11:52:25 +00:00
|
|
|
USERCONFIG = changes[CONFIGNAME].newValue
|
2017-11-29 21:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|