tridactyl/src/commandline_frame.ts

59 lines
1.8 KiB
TypeScript
Raw Normal View History

/** Script used in the commandline iframe. Communicates with background. */
import * as Messaging from './messaging'
2017-10-12 04:43:56 +01:00
import * as SELF from './commandline_frame'
let completions = window.document.getElementById("completions") as HTMLElement
let clInput = window.document.getElementById("tridactyl-input") as HTMLInputElement
export let focus = () => clInput.focus()
/* Process the commandline on enter. */
clInput.addEventListener("keydown", function (keyevent) {
if (keyevent.key === "Enter") {
process()
}
if (keyevent.key === "Escape") {
2017-10-09 23:55:24 +01:00
/** Bug workaround: clInput cannot be cleared during an "Escape"
* keydown event, presumably due to Firefox's internal handler for
* Escape. So clear clInput just after :)
*
* TODO: Report this on bugzilla.
*/
completions.innerHTML = ""
2017-10-09 23:55:24 +01:00
setTimeout(()=>{clInput.value = ""}, 0)
browser.runtime.sendMessage({type: "commandline", exStr: "hidecmdline"})
}
})
/* Send the commandline to the background script and await response. */
function process() {
console.log(clInput.value)
browser.runtime.sendMessage({type: "commandline", exStr: "hidecmdline"})
browser.runtime.sendMessage({type: "commandline", exStr: clInput.value})
completions.innerHTML = ""
clInput.value = ""
}
export function fillcmdline(newcommand?: string){
if (newcommand != "") {
2017-10-19 20:34:16 +01:00
clInput.value = newcommand + " "
}
// Focus is lost for some reason.
focus()
}
2017-10-12 04:43:56 +01:00
export function changecompletions(newcompletions: string) {
completions.innerHTML = newcompletions
}
function handler(message) {
2017-10-12 04:43:56 +01:00
SELF[message.command](...message.args)
}
try {
Messaging.addListener('commandline_frame', handler)
} catch(e) {
console.error("WAHWAH", e)
}