From 1c21e4fa2860d3dd57826e2b60a1443831eae15e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 29 Aug 2017 21:55:18 +0900 Subject: [PATCH] simple buffer command --- src/background/commands.js | 2 -- src/background/index.js | 19 ++++++++++++++++--- src/background/tabs.js | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) delete mode 100644 src/background/commands.js diff --git a/src/background/commands.js b/src/background/commands.js deleted file mode 100644 index 8bd52e5..0000000 --- a/src/background/commands.js +++ /dev/null @@ -1,2 +0,0 @@ -export const OPEN = 'open'; -export const TABOPEN = 'tabopen'; diff --git a/src/background/index.js b/src/background/index.js index 7618384..e5b08c5 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,6 +1,5 @@ import * as actions from '../shared/actions'; import * as tabs from './tabs'; -import * as commands from './commands'; import * as zooms from './zooms'; import KeyQueue from './key-queue'; @@ -59,15 +58,29 @@ const normalizeUrl = (string) => { } } +const cmdBuffer = (arg) => { + if (isNaN(arg)) { + // TODO support buffer identification by non-number value + throw new TypeError(`${arg} is not a number`); + } + + let index = parseInt(arg, 10) - 1; + tabs.selectAt(index); +} + const cmdEnterHandle = (request, sender) => { let words = request.text.split(' ').filter((s) => s.length > 0); switch (words[0]) { - case commands.OPEN: + case 'open': browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) }); return; - case commands.TABOPEN: + case 'tabopen': browser.tabs.create({ url: normalizeUrl(words[1]) }); return; + case 'b': + case 'buffer': + cmdBuffer(words[1]); + return; } }; diff --git a/src/background/tabs.js b/src/background/tabs.js index 56f86eb..532ad42 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -18,6 +18,19 @@ const reopenTab = () => { }); }; +const selectAt = (index) => { + chrome.tabs.query({ currentWindow: true }, (tabs) => { + if (tabs.length < 2) { + return; + } + if (index < 0 || tabs.length <= index) { + throw new RangeError(`buffer ${index} does not exist`) + } + let id = tabs[index].id; + chrome.tabs.update(id, { active: true }) + }); +} + const selectPrevTab = (current, count) => { chrome.tabs.query({ currentWindow: true }, (tabs) => { if (tabs.length < 2) { @@ -47,4 +60,4 @@ const reload = (current, cache) => { ); }; -export { closeTab, reopenTab, selectNextTab, selectPrevTab, reload }; +export { closeTab, reopenTab, selectAt, selectNextTab, selectPrevTab, reload };