diff options
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/index.js | 54 | ||||
-rw-r--r-- | src/content/scrolls.js | 27 |
2 files changed, 81 insertions, 0 deletions
diff --git a/src/content/index.js b/src/content/index.js new file mode 100644 index 0000000..03efc5e --- /dev/null +++ b/src/content/index.js @@ -0,0 +1,54 @@ +import * as scrolls from './scrolls'; +import * as actions from '../shared/actions'; + +const invokeEvent = (action) => { + if (typeof action === 'undefined' || action === null) { + return; + } + + switch (action[0]) { + case actions.SCROLL_UP: + scrolls.scrollUp(window, action[1] || 1); + break; + case actions.SCROLL_DOWN: + scrolls.scrollDown(window, action[1] || 1); + break; + case actions.SCROLL_TOP: + scrolls.scrollTop(window, action[1]); + break; + case actions.SCROLL_BOTTOM: + scrolls.scrollBottom(window, action[1]); + break; + } +} + +const isModifier = (code) => { + return code === KeyboardEvent.DOM_VK_SHIFT || + code === KeyboardEvent.DOM_VK_ALT || + code === KeyboardEvent.DOM_VK_CONTROL || + code === KeyboardEvent.DOM_VK_META; +} + +window.addEventListener("keydown", (e) => { + if (e.target instanceof HTMLInputElement) { + return; + } + if (isModifier(e.keyCode)) { + return; + } + + let request = { + type: 'event.keydown', + code: e.keyCode, + shift: e.shiftKey, + alt: e.altKey, + meta: e.metaKey, + ctrl: e.ctrlKey, + } + + browser.runtime.sendMessage(request) + .then(invokeEvent, + (err) => { + console.log(`Vim Vixen: ${err}`); + }); +}); diff --git a/src/content/scrolls.js b/src/content/scrolls.js new file mode 100644 index 0000000..2a233c2 --- /dev/null +++ b/src/content/scrolls.js @@ -0,0 +1,27 @@ +const SCROLL_DELTA = 48; + +const scrollUp = (page, count) => { + let x = page.scrollX; + let y = page.scrollY - SCROLL_DELTA * count; + page.scrollTo(x, y); +}; + +const scrollDown = (page, count) => { + let x = page.scrollX; + let y = page.scrollY + SCROLL_DELTA * count; + page.scrollTo(x, y); +}; + +const scrollTop = (page) => { + let x = page.scrollX; + let y = 0; + page.scrollTo(x, y); +}; + +const scrollBottom = (page) => { + let x = page.scrollX; + let y = page.scrollMaxY; + page.scrollTo(x, y); +}; + +export { scrollUp, scrollDown, scrollTop, scrollBottom } |