diff options
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 18 | ||||
-rw-r--r-- | src/background/tabs.js | 23 |
2 files changed, 39 insertions, 2 deletions
diff --git a/src/background/index.js b/src/background/index.js index ee02f63..d4e0098 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,4 +1,18 @@ +import * as tabs from './tabs'; + +const KEY_MAP = { + 'tabs.prev': 104, + 'tabs.next': 108 +}; + browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - console.log("Pressed " + request.which); - sendResponse({ response: "Response from background script" }); + switch (request.key) { + case KEY_MAP['tabs.prev']: + tabs.selectPrevTab(sender.tab.index); + break; + case KEY_MAP['tabs.next']: + tabs.selectNextTab(sender.tab.index); + break; + } + sendResponse(); }); diff --git a/src/background/tabs.js b/src/background/tabs.js new file mode 100644 index 0000000..20ef286 --- /dev/null +++ b/src/background/tabs.js @@ -0,0 +1,23 @@ +const selectPrevTab = (current) => { + chrome.tabs.query({ currentWindow: true }, (tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current - 1) % tabs.length + let id = tabs[select].id; + chrome.tabs.update(id, { active: true }) + }); +}; + +const selectNextTab = (current) => { + chrome.tabs.query({ currentWindow: true }, (tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current + 1 + tabs.length) % tabs.length + let id = tabs[select].id; + chrome.tabs.update(id, { active: true }) + }); +}; + +export { selectNextTab, selectPrevTab }; |