parent
14d13e2c3a
commit
bf890a6d9d
5 changed files with 71 additions and 50 deletions
@ -0,0 +1,35 @@ |
|||||||
|
import actions from '../actions'; |
||||||
|
|
||||||
|
const normalizeUrl = (string) => { |
||||||
|
try { |
||||||
|
return new URL(string).href |
||||||
|
} catch (e) { |
||||||
|
return 'http://' + string; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export function exec(line) { |
||||||
|
let name = line.split(' ')[0]; |
||||||
|
let remaining = line.replace(name + ' ', ''); |
||||||
|
|
||||||
|
switch (name) { |
||||||
|
case 'open': |
||||||
|
// TODO use search engined and pass keywords to them
|
||||||
|
return { |
||||||
|
type: actions.COMMAND_OPEN_URL, |
||||||
|
url: normalizeUrl(remaining) |
||||||
|
}; |
||||||
|
case 'tabopen': |
||||||
|
return { |
||||||
|
type: actions.COMMAND_TABOPEN_URL, |
||||||
|
url: remaining |
||||||
|
}; |
||||||
|
case 'b': |
||||||
|
case 'buffer': |
||||||
|
return { |
||||||
|
type: actions.COMMAND_BUFFER, |
||||||
|
keywords: remaining |
||||||
|
}; |
||||||
|
} |
||||||
|
throw new Error(name + ' command is not defined'); |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
import * as tabs from '../background/tabs'; |
||||||
|
import actions from '../actions'; |
||||||
|
|
||||||
|
const cmdBuffer = (sender, arg) => { |
||||||
|
if (isNaN(arg)) { |
||||||
|
return tabs.selectByKeyword(sender.tab, arg); |
||||||
|
} else { |
||||||
|
let index = parseInt(arg, 10) - 1; |
||||||
|
return tabs.selectAt(index); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default function reducer(state, action, sender) { |
||||||
|
switch (action.type) { |
||||||
|
case actions.COMMAND_OPEN_URL: |
||||||
|
return browser.tabs.update(sender.tab.id, { url: action.url }); |
||||||
|
case actions.COMMAND_TABOPEN_URL: |
||||||
|
return browser.tabs.create({ url: action.url }); |
||||||
|
case actions.COMMAND_BUFFER: |
||||||
|
return cmdBuffer(sender, action.keywords); |
||||||
|
default: |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
} |
Reference in new issue