diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-16 20:33:53 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-16 20:33:53 +0900 |
commit | 13fb726332e9638cb3fafc477cf9fe641cb906ce (patch) | |
tree | 407ac0092fcb20c94e407bf1cbc8e5c4844b3bd6 /src/background/index.js | |
parent | 85e22063fe522518e70928255349fb1c16b7cb42 (diff) | |
parent | dc860d32f50e2fc21a4f38663bfb0b9099a77513 (diff) |
Merge branch 'command-line'
Diffstat (limited to 'src/background/index.js')
-rw-r--r-- | src/background/index.js | 62 |
1 files changed, 46 insertions, 16 deletions
diff --git a/src/background/index.js b/src/background/index.js index 604ea92..f3bd65a 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,21 +1,37 @@ 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]) { + case actions.TABS_CLOSE: + tabs.closeTab(sender.tab.id); + break; + case actions.TABS_REOPEN: + tabs.reopenTab(); + break; case actions.TABS_PREV: tabs.selectPrevTab(sender.tab.index, actions[1] || 1); break; @@ -25,22 +41,36 @@ 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) => { + try { + return new URL(string).href + } catch (e) { + 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; } }); |