diff options
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 25 | ||||
-rw-r--r-- | src/background/tabs.js | 21 |
2 files changed, 34 insertions, 12 deletions
diff --git a/src/background/index.js b/src/background/index.js index 15c8ab0..8913a83 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -55,9 +55,9 @@ const normalizeUrl = (string) => { } } -const cmdBuffer = (arg) => { +const cmdBuffer = (sender, arg) => { if (isNaN(arg)) { - return tabs.selectByKeyword(arg); + return tabs.selectByKeyword(sender.tab, arg); } else { let index = parseInt(arg, 10) - 1; return tabs.selectAt(index); @@ -73,7 +73,7 @@ const cmdEnterHandle = (request, sender) => { return browser.tabs.create({ url: normalizeUrl(words[1]) }); case 'b': case 'buffer': - return cmdBuffer(words[1]); + return cmdBuffer(sender, words[1]); } throw new Error(words[0] + ' command is not defined'); }; @@ -84,9 +84,20 @@ browser.runtime.onMessage.addListener((request, sender) => { return keyPressHandle(request, sender); case 'event.cmd.enter': return cmdEnterHandle(request, sender); - case 'event.cmd.suggest': - // TODO make suggestion and return - break; + case 'event.cmd.tabs.completion': + return tabs.getCompletions(request.text).then((tabs) => { + let items = tabs.map((tab) => { + return { + caption: tab.title, + content: tab.title, + url: tab.url, + icon: tab.favIconUrl + } + }); + return { + name: "Buffers", + items: items + }; + }); } - return Promise.resolve(); }); diff --git a/src/background/tabs.js b/src/background/tabs.js index efecdc4..111bbd9 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -31,7 +31,7 @@ const selectAt = (index) => { }); }; -const selectByKeyword = (keyword) => { +const selectByKeyword = (current, keyword) => { return browser.tabs.query({ currentWindow: true }).then((tabs) => { let matched = tabs.filter((t) => { return t.url.includes(keyword) || t.title.includes(keyword) @@ -39,14 +39,25 @@ const selectByKeyword = (keyword) => { if (matched.length == 0) { throw new RangeError('No matching buffer for ' + keyword); - } else if (matched.length >= 2) { - throw new RangeError('More than one match for ' + keyword); } - + for (let tab of matched) { + if (tab.index > current.index) { + return browser.tabs.update(tab.id, { active: true }); + } + } return browser.tabs.update(matched[0].id, { active: true }); }); } +const getCompletions = (keyword) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title.includes(keyword) + }) + return matched; + }); +}; + const selectPrevTab = (current, count) => { return browser.tabs.query({ currentWindow: true }, (tabs) => { if (tabs.length < 2) { @@ -76,4 +87,4 @@ const reload = (current, cache) => { ); }; -export { closeTab, reopenTab, selectAt, selectByKeyword, selectNextTab, selectPrevTab, reload }; +export { closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, selectPrevTab, selectNextTab, reload }; |