implement simple tab switch

jh-changes
Shin'ya Ueoka 7 years ago
parent f1d4bb8c37
commit 8e5ceebf61
  1. 18
      src/background/index.js
  2. 23
      src/background/tabs.js
  3. 11
      src/content/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) => { browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Pressed " + request.which); switch (request.key) {
sendResponse({ response: "Response from background script" }); case KEY_MAP['tabs.prev']:
tabs.selectPrevTab(sender.tab.index);
break;
case KEY_MAP['tabs.next']:
tabs.selectNextTab(sender.tab.index);
break;
}
sendResponse();
}); });

@ -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 };

@ -1,9 +1,12 @@
window.addEventListener("keypress", (e) => { window.addEventListener("keypress", (e) => {
browser.runtime.sendMessage({ browser.runtime.sendMessage({
which: e.which || e.keyCode, key: e.which || e.keyCode,
}).then((msg) => { shift: e.shift,
console.log(`Message from the background script: ${msg.response}`); alt: e.alt,
meta: e.meta,
ctrl: e.ctrl,
}).then(() => {
}, (err) => { }, (err) => {
console.log(`Error: ${err}`); console.log(`Vim Vixen: ${err}`);
}); });
}); });