Bonus late night commit

All the mistakes are Colin's fault
- Generic listener
- Send messages to command line
- ^ doesn't work unless frame is opened in new tab
This commit is contained in:
Oliver Blanthorn 2017-10-05 23:11:56 +01:00 committed by Colin Caine
parent 4dc218084d
commit 07c3688f8d
6 changed files with 55 additions and 12 deletions

View file

@ -23,7 +23,7 @@ export function show(){
if (cmdline_iframe === undefined) {
init()
}
cmdline_iframe.setAttribute("style", "position: fixed; top: 0; left: 0; z-index: 10000; width: 100%; height: 36px; border: 0; padding: 0; margin: 0;");
cmdline_iframe.setAttribute("style", "position: fixed; top: 0; left: 0; z-index: 10000; width: 100%; height: 36px; border: 0; padding: 0; margin: 1;");
}
export function hide(){

View file

@ -1,5 +1,7 @@
/** Script used in the commandline iframe. Communicates with background. */
import * as Messaging from './messaging'
let clInput = window.document.getElementById("tridactyl-input") as HTMLInputElement
clInput.focus()
@ -18,5 +20,19 @@ function process() {
clInput.value = ""
}
// Dummy export to ensure this is treated as a module
export {}
function changecommand(newcommand?: string){
clInput.value = newcommand
console.log(newcommand, clInput.value)
}
function handler(message) {
console.log(message)
// this[message.command](...message.args)
changecommand(...message.args)
}
try {
Messaging.addListener('commandline_frame', handler)
} catch(e) {
console.error("WAHWAH", e)
}

View file

@ -9,6 +9,14 @@ interface ContentCommandMessage extends Message {
args?: Array<any>
}
function messageCommandline(command: string, args?: Array<any>) {
browser.runtime.sendMessage({
type: 'commandline_frame',
command,
args,
})
}
function messageActiveTab(command: string, args?: Array<any>) {
messageFilteredTabs({active:true}, command, args)
}
@ -92,7 +100,8 @@ export async function reload(n = 1, hard = false){
// Commandline function
export function showcommandline(){
export function showcommandline(exstr?){
messageCommandline("changecommand", [exstr,])
messageActiveTab("showcommandline")
}

View file

@ -1,4 +1,6 @@
// Interface: onKeydown.addListener(func)
//
import * as Messaging from './messaging'
import {MsgSafeKeyboardEvent} from './msgsafe'
@ -20,14 +22,9 @@ export const onKeydown = { addListener }
// Receive events from content and pass to listeners
function handler(message: KeydownShimMessage) {
if (message.type === "keydown") {
for (let listener of listeners) {
listener(message.event)
}
for (let listener of listeners) {
listener(message.event)
}
// This is req. to shut typescript up.
// TODO: Fix onMessageBool in web-ext-types
return false
}
browser.runtime.onMessage.addListener(handler)
Messaging.addListener('keydown', handler)

20
src/messaging.ts Normal file
View file

@ -0,0 +1,20 @@
const listeners = new Map<string, Set<any>>()
/** Register a listener to be called for each message with type */
export function addListener(type, callback) {
if (!listeners.get(type)) {
listeners.set(type, new Set())
}
listeners.get(type).add(callback)
return () => { listeners.get(type).delete(callback) }
}
function onMessage(message) {
if (listeners.get(message.type)) {
for (let listener of listeners.get(message.type)) {
listener(message)
}
}
}
browser.runtime.onMessage.addListener(onMessage)

View file

@ -36,6 +36,7 @@ export namespace normalmode {
["l", "scrollright"],
["G", "scrolltobottom"],
["gg", "scrolltotop"],
["o", "showcommandline open"],
["H", "historyback"],
["L", "historyforward"],
["d", "tabclose"],