2018-06-03 15:04:50 +02:00
|
|
|
|
// This file is only loaded in tridacyl's help pages
|
|
|
|
|
|
|
|
|
|
import * as config from "./config"
|
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
/** Created the element that should contain keybinding information */
|
|
|
|
|
function initTridactylSettingElem(
|
|
|
|
|
elem: HTMLElement,
|
|
|
|
|
kind: string,
|
|
|
|
|
): HTMLElement {
|
|
|
|
|
let bindingNode = elem.getElementsByClassName(`Tridactyl${kind}`)[0]
|
|
|
|
|
if (bindingNode) {
|
|
|
|
|
Array.from(bindingNode.children)
|
2018-06-03 15:04:50 +02:00
|
|
|
|
.filter(e => e.tagName == "LI")
|
|
|
|
|
.forEach(e => e.parentNode.removeChild(e))
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise, create it
|
2018-06-03 17:21:11 +02:00
|
|
|
|
bindingNode = document.createElement("ul")
|
|
|
|
|
bindingNode.className = `TridactylSetting Tridactyl${kind}`
|
|
|
|
|
bindingNode.textContent = kind + ": "
|
|
|
|
|
elem.insertBefore(bindingNode, elem.children[2])
|
2018-06-03 15:04:50 +02:00
|
|
|
|
}
|
2018-06-03 17:21:11 +02:00
|
|
|
|
return bindingNode as HTMLElement
|
2018-06-03 15:04:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns an object that maps excmd names to excmd documentation element */
|
|
|
|
|
function getCommandElements() {
|
|
|
|
|
return Array.from(
|
|
|
|
|
document.querySelectorAll("section.tsd-panel-group:nth-child(3) h3"),
|
|
|
|
|
).reduce((all, elem) => {
|
|
|
|
|
all[elem.textContent] = elem.parentElement
|
|
|
|
|
return all
|
|
|
|
|
}, {})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Updates the doc with aliases fetched from the config */
|
2018-06-03 17:21:11 +02:00
|
|
|
|
async function addSetting(settingName: string) {
|
2018-06-03 15:04:50 +02:00
|
|
|
|
let commandElems = getCommandElements()
|
|
|
|
|
// We're ignoring composite because it combines multiple excmds
|
|
|
|
|
delete commandElems["composite"]
|
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
// Initialize or reset the <ul> element that will contain settings in each commandElem
|
|
|
|
|
let settingElems = Object.keys(commandElems).reduce(
|
|
|
|
|
(settingElems, cmdName) => {
|
|
|
|
|
settingElems[cmdName] = initTridactylSettingElem(
|
|
|
|
|
commandElems[cmdName],
|
|
|
|
|
settingName,
|
|
|
|
|
)
|
|
|
|
|
return settingElems
|
|
|
|
|
},
|
|
|
|
|
{},
|
|
|
|
|
)
|
2018-06-03 15:04:50 +02:00
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
let settings = await config.getAsync(settingName)
|
|
|
|
|
// For each setting
|
|
|
|
|
for (let setting in settings) {
|
|
|
|
|
let excmd = settings[setting].split(" ")
|
|
|
|
|
// How can we automatically detect what commands can be skipped?
|
|
|
|
|
excmd = ["fillcmdline", "current_url"].includes(excmd[0])
|
|
|
|
|
? excmd[1]
|
|
|
|
|
: excmd[0]
|
|
|
|
|
// Find the corresponding setting
|
|
|
|
|
while (settings[excmd]) {
|
|
|
|
|
excmd = settings[excmd].split(" ")
|
|
|
|
|
excmd = ["fillcmdline", "current_url"].includes(excmd[0])
|
|
|
|
|
? excmd[1]
|
|
|
|
|
: excmd[0]
|
|
|
|
|
}
|
2018-06-03 15:04:50 +02:00
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
// If there is an HTML element for settings that correspond to the excmd we just found
|
|
|
|
|
if (settingElems[excmd]) {
|
|
|
|
|
let settingLi = document.createElement("li")
|
|
|
|
|
settingLi.innerText = setting
|
|
|
|
|
settingLi.title = settings[setting]
|
|
|
|
|
// Add the setting to the element
|
|
|
|
|
settingElems[excmd].appendChild(settingLi)
|
2018-06-03 15:04:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-03 16:38:03 +02:00
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
// Remove all settingElems that do not have at least one setting
|
|
|
|
|
Object.values(settingElems)
|
2018-06-03 16:38:03 +02:00
|
|
|
|
.filter(
|
|
|
|
|
(e: HTMLElement) =>
|
|
|
|
|
!Array.from(e.children).find(c => c.tagName == "LI"),
|
|
|
|
|
)
|
|
|
|
|
.forEach((e: HTMLElement) => e.parentNode.removeChild(e))
|
2018-06-03 15:04:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
browser.storage.onChanged.addListener((changes, areaname) => {
|
|
|
|
|
if ("userconfig" in changes) {
|
|
|
|
|
// JSON.stringify for comparisons like it's 2012
|
2018-06-03 17:21:11 +02:00
|
|
|
|
;["exaliases", "nmaps"].forEach(kind => {
|
|
|
|
|
if (
|
|
|
|
|
JSON.stringify(changes.userconfig.newValue[kind]) !=
|
|
|
|
|
JSON.stringify(changes.userconfig.oldValue[kind])
|
|
|
|
|
)
|
|
|
|
|
addSetting(kind)
|
|
|
|
|
})
|
2018-06-03 15:04:50 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2018-06-03 17:21:11 +02:00
|
|
|
|
addEventListener("load", async () => {
|
2018-06-10 15:51:16 +01:00
|
|
|
|
await addSetting("exaliases")
|
|
|
|
|
await addSetting("nmaps")
|
2018-06-03 17:21:11 +02:00
|
|
|
|
// setCommandSetting() can change the height of nodes in the page so we need to scroll to the right place again
|
2018-06-10 15:51:16 +01:00
|
|
|
|
if (document.location.hash) {
|
|
|
|
|
document.location.hash = document.location.hash
|
|
|
|
|
}
|
2018-06-03 15:04:50 +02:00
|
|
|
|
})
|