diff options
Diffstat (limited to 'src/background/index.js')
-rw-r--r-- | src/background/index.js | 48 |
1 files changed, 6 insertions, 42 deletions
diff --git a/src/background/index.js b/src/background/index.js index 4d75b33..e72cab0 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,51 +1,11 @@ -import * as tabs from './tabs'; import * as keys from './keys'; import * as inputActions from '../actions/input'; import backgroundReducers from '../reducers/background'; +import commandReducer from '../reducers/command'; import inputReducers from '../reducers/input'; let inputState = inputReducers(undefined, {}); -const normalizeUrl = (string) => { - try { - return new URL(string).href - } catch (e) { - return 'http://' + string; - } -} - -const cmdBuffer = (sender, arg) => { - if (isNaN(arg)) { - return tabs.selectByKeyword(sender.tab, arg); - } else { - let index = parseInt(arg, 10) - 1; - return tabs.selectAt(index); - } -} - -const cmdEnterHandle = (request, sender) => { - let words = request.text.split(' ').filter((s) => s.length > 0); - switch (words[0]) { - case 'open': - return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) }); - case 'tabopen': - return browser.tabs.create({ url: normalizeUrl(words[1]) }); - case 'b': - case 'buffer': - return cmdBuffer(sender, words[1]); - } - throw new Error(words[0] + ' command is not defined'); -}; - -browser.runtime.onMessage.addListener((request, sender) => { - switch (request.type) { - case 'event.cmd.enter': - return cmdEnterHandle(request, sender); - default: - return browser.tabs.sendMessage(sender.tab.id, request); - } -}); - const keyQueueChanged = (sender, prevState, state) => { if (state.keys.length === 0) { return Promise.resolve(); @@ -75,7 +35,11 @@ const handleMessage = (action, sender) => { inputState = nextInputState; return keyQueueChanged(sender, prevState, inputState); } - return backgroundReducers(undefined, action, sender); + return backgroundReducers(undefined, action, sender).then(() => { + return commandReducer(undefined, action, sender).then(() => { + return browser.tabs.sendMessage(sender.tab.id, action); + }); + }); }; browser.runtime.onMessage.addListener(handleMessage); |