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 }; diff --git a/src/content/index.js b/src/content/index.js index 0fa0720..fceed4b 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,9 +1,12 @@ window.addEventListener("keypress", (e) => { browser.runtime.sendMessage({ - which: e.which || e.keyCode, - }).then((msg) => { - console.log(`Message from the background script: ${msg.response}`); + key: e.which || e.keyCode, + shift: e.shift, + alt: e.alt, + meta: e.meta, + ctrl: e.ctrl, + }).then(() => { }, (err) => { - console.log(`Error: ${err}`); + console.log(`Vim Vixen: ${err}`); }); });