diff options
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 46 | ||||
-rw-r--r-- | src/background/key-queue.js | 38 | ||||
-rw-r--r-- | src/background/keys.js | 28 | ||||
-rw-r--r-- | src/background/tabs.js | 23 |
4 files changed, 135 insertions, 0 deletions
diff --git a/src/background/index.js b/src/background/index.js new file mode 100644 index 0000000..604ea92 --- /dev/null +++ b/src/background/index.js @@ -0,0 +1,46 @@ +import * as actions from '../shared/actions'; +import * as tabs from './tabs'; +import KeyQueue from './key-queue'; + +const queue = new KeyQueue(); + +const keyDownHandle = (request) => { + return queue.push({ + code: request.code, + shift: request.shift, + ctrl: request.ctrl, + alt: request.alt, + meta: request.meta + }) +} + +const doBackgroundAction = (sender, action) => { + switch(action[0]) { + case actions.TABS_PREV: + tabs.selectPrevTab(sender.tab.index, actions[1] || 1); + break; + case actions.TABS_NEXT: + tabs.selectNextTab(sender.tab.index, actions[1] || 1); + break; + } +} + +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + let action = null; + + switch (request.type) { + case 'event.keydown': + action = keyDownHandle(request); + break; + } + + if (action == null) { + return; + } + + if (actions.isBackgroundAction(action[0])) { + doBackgroundAction(sender, action); + } else if (actions.isContentAction(action[0])) { + sendResponse(action); + } +}); diff --git a/src/background/key-queue.js b/src/background/key-queue.js new file mode 100644 index 0000000..e21399e --- /dev/null +++ b/src/background/key-queue.js @@ -0,0 +1,38 @@ +import * as keys from './keys'; +import * as actions from '../shared/actions'; + +const DEFAULT_KEYMAP = [ + { keys: [{ code: KeyboardEvent.DOM_VK_K }], action: [ actions.SCROLL_UP, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_J }], action: [ actions.SCROLL_DOWN, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_G }, { code: KeyboardEvent.DOM_VK_G }], action: [ actions.SCROLL_TOP ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_G, shift: true }], action: [ actions.SCROLL_BOTTOM ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_H }], action: [ actions.TABS_PREV, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_L }], action: [ actions.TABS_NEXT, 1 ]}, +] + +export default class KeyQueue { + + constructor(keymap) { + this.data = []; + this.keymap = keymap; + } + + push(key) { + this.data.push(key); + let filtered = DEFAULT_KEYMAP.filter((map) => { + return keys.hasPrefix(map.keys, this.data) + }); + + if (filtered.length == 0) { + this.data = []; + return; + } else if (filtered.length == 1) { + let map = filtered[0]; + if (map.keys.length == this.data.length) { + this.data = []; + return map.action; + } + } + return null; + } +} diff --git a/src/background/keys.js b/src/background/keys.js new file mode 100644 index 0000000..2fd00a2 --- /dev/null +++ b/src/background/keys.js @@ -0,0 +1,28 @@ +const identifyKey = (key1, key2) => { + return (key1.code === key2.code) && + ((key1.shift || false) === (key2.shift || false)) && + ((key1.ctrl || false) === (key2.ctrl || false)) && + ((key1.alt || false) === (key2.alt || false)) && + ((key1.meta || false) === (key2.meta || false)); +}; + +const hasPrefix = (keys, prefix) => { + if (keys.length < prefix.length) { + return false; + } + for (let i = 0; i < prefix.length; ++i) { + if (!identifyKey(keys[i], prefix[i])) { + return false; + } + } + return true; +} + +const identifyKeys = (keys1, keys2) => { + if (keys1.length !== keys2.length) { + return false; + } + return hasPrefix(keys1, keys2); +} + +export { identifyKey, identifyKeys, hasPrefix }; diff --git a/src/background/tabs.js b/src/background/tabs.js new file mode 100644 index 0000000..000bd7d --- /dev/null +++ b/src/background/tabs.js @@ -0,0 +1,23 @@ +const selectPrevTab = (current, count) => { + chrome.tabs.query({ currentWindow: true }, (tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current - count) % tabs.length + let id = tabs[select].id; + chrome.tabs.update(id, { active: true }) + }); +}; + +const selectNextTab = (current, count) => { + chrome.tabs.query({ currentWindow: true }, (tabs) => { + if (tabs.length < 2) { + return; + } + let select = (current + count + tabs.length) % tabs.length + let id = tabs[select].id; + chrome.tabs.update(id, { active: true }) + }); +}; + +export { selectNextTab, selectPrevTab }; |