diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/background/index.js | 14 | ||||
-rw-r--r-- | src/content/index.js | 18 | ||||
-rw-r--r-- | src/content/scrolls.js | 27 |
3 files changed, 56 insertions, 3 deletions
diff --git a/src/background/index.js b/src/background/index.js index d4e0098..793f330 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -2,10 +2,14 @@ import * as tabs from './tabs'; const KEY_MAP = { 'tabs.prev': 104, - 'tabs.next': 108 + 'tabs.next': 108, + 'scroll.up': 107, + 'scroll.down': 106 }; browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + let response = null; + switch (request.key) { case KEY_MAP['tabs.prev']: tabs.selectPrevTab(sender.tab.index); @@ -13,6 +17,12 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => { case KEY_MAP['tabs.next']: tabs.selectNextTab(sender.tab.index); break; + case KEY_MAP['scroll.up']: + response = 'scroll.up' + break; + case KEY_MAP['scroll.down']: + response = 'scroll.down' + break; } - sendResponse(); + sendResponse(response); }); diff --git a/src/content/index.js b/src/content/index.js index fceed4b..e77bb0a 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,3 +1,16 @@ +import * as scrolls from './scrolls'; + +const invokeEvent = (type) => { + switch (type) { + case 'scroll.up': + scrolls.scrollUp(window); + break; + case 'scroll.down': + scrolls.scrollDown(window); + break; + } +} + window.addEventListener("keypress", (e) => { browser.runtime.sendMessage({ key: e.which || e.keyCode, @@ -5,7 +18,10 @@ window.addEventListener("keypress", (e) => { alt: e.alt, meta: e.meta, ctrl: e.ctrl, - }).then(() => { + }).then((response) => { + if (response) { + invokeEvent(response); + } }, (err) => { console.log(`Vim Vixen: ${err}`); }); diff --git a/src/content/scrolls.js b/src/content/scrolls.js new file mode 100644 index 0000000..76d1aa2 --- /dev/null +++ b/src/content/scrolls.js @@ -0,0 +1,27 @@ +const SCROLL_DELTA = 48; + +const scrollUp = (page) => { + let x = page.scrollX; + var y = page.scrollY - SCROLL_DELTA; + page.scrollTo(x, y); +}; + +const scrollDown = (page) => { + let x = page.scrollX; + var y = page.scrollY + SCROLL_DELTA; + page.scrollTo(x, y); +}; + +const scrollToTop = (page) => { + let x = page.scrollX; + var y = page.scrollMaxY; + page.scrollTo(x, y); +}; + +const scrollToBottom = (page) => { + let x = page.scrollX; + var y = 0; + page.scrollTo(x, y); +}; + +export { scrollUp, scrollDown, scrollToTop, scrollToBottom } |