From 41069cf527ed159d6c5ff89cc77867537025c9f5 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 13 Aug 2017 16:37:47 +0900 Subject: [PATCH] implement gg/G commands --- src/background/key-queue.js | 2 ++ src/background/keys.js | 2 +- src/content/index.js | 29 +++++++++++++++++++++++------ src/content/scrolls.js | 14 +++++++------- src/shared/actions.js | 6 +++++- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/background/key-queue.js b/src/background/key-queue.js index cd8d865..e21399e 100644 --- a/src/background/key-queue.js +++ b/src/background/key-queue.js @@ -4,6 +4,8 @@ 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 ]}, ] diff --git a/src/background/keys.js b/src/background/keys.js index 802340d..2fd00a2 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -10,7 +10,7 @@ const hasPrefix = (keys, prefix) => { if (keys.length < prefix.length) { return false; } - for (let i = 0; i < keys.length; ++i) { + for (let i = 0; i < prefix.length; ++i) { if (!identifyKey(keys[i], prefix[i])) { return false; } diff --git a/src/content/index.js b/src/content/index.js index 88a668e..03efc5e 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,4 +1,5 @@ import * as scrolls from './scrolls'; +import * as actions from '../shared/actions'; const invokeEvent = (action) => { if (typeof action === 'undefined' || action === null) { @@ -6,27 +7,43 @@ const invokeEvent = (action) => { } switch (action[0]) { - case 'scroll.up': + case actions.SCROLL_UP: scrolls.scrollUp(window, action[1] || 1); break; - case 'scroll.down': + 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.shift, - alt: e.alt, - meta: e.meta, - ctrl: e.ctrl, + shift: e.shiftKey, + alt: e.altKey, + meta: e.metaKey, + ctrl: e.ctrlKey, } browser.runtime.sendMessage(request) diff --git a/src/content/scrolls.js b/src/content/scrolls.js index e640384..2a233c2 100644 --- a/src/content/scrolls.js +++ b/src/content/scrolls.js @@ -2,26 +2,26 @@ const SCROLL_DELTA = 48; const scrollUp = (page, count) => { let x = page.scrollX; - var y = page.scrollY - SCROLL_DELTA * count; + let y = page.scrollY - SCROLL_DELTA * count; page.scrollTo(x, y); }; const scrollDown = (page, count) => { let x = page.scrollX; - var y = page.scrollY + SCROLL_DELTA * count; + let y = page.scrollY + SCROLL_DELTA * count; page.scrollTo(x, y); }; -const scrollToTop = (page) => { +const scrollTop = (page) => { let x = page.scrollX; - var y = page.scrollMaxY; + let y = 0; page.scrollTo(x, y); }; -const scrollToBottom = (page) => { +const scrollBottom = (page) => { let x = page.scrollX; - var y = 0; + let y = page.scrollMaxY; page.scrollTo(x, y); }; -export { scrollUp, scrollDown, scrollToTop, scrollToBottom } +export { scrollUp, scrollDown, scrollTop, scrollBottom } diff --git a/src/shared/actions.js b/src/shared/actions.js index 2e10810..3e3cbd0 100644 --- a/src/shared/actions.js +++ b/src/shared/actions.js @@ -2,6 +2,8 @@ export const TABS_PREV = 'tabs.prev'; export const TABS_NEXT = 'tabs.next'; export const SCROLL_UP = 'scroll.up'; export const SCROLL_DOWN = 'scroll.down'; +export const SCROLL_TOP = 'scroll.top'; +export const SCROLL_BOTTOM = 'scroll.bottom'; const BACKGROUND_ACTION_SET = new Set([ TABS_PREV, @@ -10,7 +12,9 @@ const BACKGROUND_ACTION_SET = new Set([ const CONTENT_ACTION_SET = new Set([ SCROLL_UP, - SCROLL_DOWN + SCROLL_DOWN, + SCROLL_TOP, + SCROLL_BOTTOM ]); export const isBackgroundAction = (action) => {