From 9a808f45ed4acba8c8dc8af4738c582ac417bd49 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 15 Aug 2017 17:32:12 +0900 Subject: [PATCH] implement simple open/tabopen command --- src/background/commands.js | 2 ++ src/background/index.js | 54 ++++++++++++++++++++++++++------------ src/content/index.js | 15 ++++++++++- 3 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 src/background/commands.js diff --git a/src/background/commands.js b/src/background/commands.js new file mode 100644 index 0000000..8bd52e5 --- /dev/null +++ b/src/background/commands.js @@ -0,0 +1,2 @@ +export const OPEN = 'open'; +export const TABOPEN = 'tabopen'; diff --git a/src/background/index.js b/src/background/index.js index 604ea92..e8882c5 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,18 +1,28 @@ import * as actions from '../shared/actions'; import * as tabs from './tabs'; +import * as commands from './commands'; import KeyQueue from './key-queue'; const queue = new KeyQueue(); -const keyDownHandle = (request) => { - return queue.push({ +const keyDownHandle = (request, sender, sendResponse) => { + let action = queue.push({ code: request.code, shift: request.shift, ctrl: request.ctrl, alt: request.alt, meta: request.meta - }) -} + }); + if (!action) { + return; + } + + if (actions.isBackgroundAction(action[0])) { + doBackgroundAction(sender, action); + } else if (actions.isContentAction(action[0])) { + sendResponse(action); + } +}; const doBackgroundAction = (sender, action) => { switch(action[0]) { @@ -25,22 +35,32 @@ const doBackgroundAction = (sender, action) => { } } -browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - let action = null; - - switch (request.type) { - case 'event.keydown': - action = keyDownHandle(request); - break; - } +const normalizeUrl = (string) => { + return 'http://' + string; +} - if (action == null) { +const cmdEnterHandle = (request, sender) => { + let words = request.text.split(' ').filter((s) => s.length > 0); + switch (words[0]) { + case commands.OPEN: + browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) }); + return; + case commands.TABOPEN: + browser.tabs.create({ url: normalizeUrl(words[1]) }); return; } +}; - if (actions.isBackgroundAction(action[0])) { - doBackgroundAction(sender, action); - } else if (actions.isContentAction(action[0])) { - sendResponse(action); +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + switch (request.type) { + case 'event.keydown': + keyDownHandle(request, sender, sendResponse); + break; + case 'event.cmd.enter': + cmdEnterHandle(request, sender, sendResponse); + break; + case 'event.cmd.suggest': + // TODO make suggestion and return via sendResponse + break; } }); diff --git a/src/content/index.js b/src/content/index.js index 8cf0aed..ed33961 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -12,7 +12,20 @@ const invokeEvent = (action) => { switch (action[0]) { case actions.CMD_OPEN: footer = new FooterLine(document); - footer.input.value = ':'; + footer.onPromptChange((e) => { + let request = { + type: 'event.cmd.suggest', + text: e.target.value + }; + browser.runtime.sendMessage(request); + }); + footer.onEntered((e) => { + let request = { + type: 'event.cmd.enter', + text: e.target.value + }; + browser.runtime.sendMessage(request); + }); footer.focus(); break; case actions.SCROLL_UP: