implement gg/G commands

jh-changes
Shin'ya Ueoka 7 years ago
parent bbc8ff515e
commit 41069cf527
  1. 2
      src/background/key-queue.js
  2. 2
      src/background/keys.js
  3. 29
      src/content/index.js
  4. 14
      src/content/scrolls.js
  5. 6
      src/shared/actions.js

@ -4,6 +4,8 @@ import * as actions from '../shared/actions';
const DEFAULT_KEYMAP = [ const DEFAULT_KEYMAP = [
{ keys: [{ code: KeyboardEvent.DOM_VK_K }], action: [ actions.SCROLL_UP, 1 ]}, { 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_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_H }], action: [ actions.TABS_PREV, 1 ]},
{ keys: [{ code: KeyboardEvent.DOM_VK_L }], action: [ actions.TABS_NEXT, 1 ]}, { keys: [{ code: KeyboardEvent.DOM_VK_L }], action: [ actions.TABS_NEXT, 1 ]},
] ]

@ -10,7 +10,7 @@ const hasPrefix = (keys, prefix) => {
if (keys.length < prefix.length) { if (keys.length < prefix.length) {
return false; return false;
} }
for (let i = 0; i < keys.length; ++i) { for (let i = 0; i < prefix.length; ++i) {
if (!identifyKey(keys[i], prefix[i])) { if (!identifyKey(keys[i], prefix[i])) {
return false; return false;
} }

@ -1,4 +1,5 @@
import * as scrolls from './scrolls'; import * as scrolls from './scrolls';
import * as actions from '../shared/actions';
const invokeEvent = (action) => { const invokeEvent = (action) => {
if (typeof action === 'undefined' || action === null) { if (typeof action === 'undefined' || action === null) {
@ -6,27 +7,43 @@ const invokeEvent = (action) => {
} }
switch (action[0]) { switch (action[0]) {
case 'scroll.up': case actions.SCROLL_UP:
scrolls.scrollUp(window, action[1] || 1); scrolls.scrollUp(window, action[1] || 1);
break; break;
case 'scroll.down': case actions.SCROLL_DOWN:
scrolls.scrollDown(window, action[1] || 1); scrolls.scrollDown(window, action[1] || 1);
break; 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) => { window.addEventListener("keydown", (e) => {
if (e.target instanceof HTMLInputElement) { if (e.target instanceof HTMLInputElement) {
return; return;
} }
if (isModifier(e.keyCode)) {
return;
}
let request = { let request = {
type: 'event.keydown', type: 'event.keydown',
code: e.keyCode, code: e.keyCode,
shift: e.shift, shift: e.shiftKey,
alt: e.alt, alt: e.altKey,
meta: e.meta, meta: e.metaKey,
ctrl: e.ctrl, ctrl: e.ctrlKey,
} }
browser.runtime.sendMessage(request) browser.runtime.sendMessage(request)

@ -2,26 +2,26 @@ const SCROLL_DELTA = 48;
const scrollUp = (page, count) => { const scrollUp = (page, count) => {
let x = page.scrollX; let x = page.scrollX;
var y = page.scrollY - SCROLL_DELTA * count; let y = page.scrollY - SCROLL_DELTA * count;
page.scrollTo(x, y); page.scrollTo(x, y);
}; };
const scrollDown = (page, count) => { const scrollDown = (page, count) => {
let x = page.scrollX; let x = page.scrollX;
var y = page.scrollY + SCROLL_DELTA * count; let y = page.scrollY + SCROLL_DELTA * count;
page.scrollTo(x, y); page.scrollTo(x, y);
}; };
const scrollToTop = (page) => { const scrollTop = (page) => {
let x = page.scrollX; let x = page.scrollX;
var y = page.scrollMaxY; let y = 0;
page.scrollTo(x, y); page.scrollTo(x, y);
}; };
const scrollToBottom = (page) => { const scrollBottom = (page) => {
let x = page.scrollX; let x = page.scrollX;
var y = 0; let y = page.scrollMaxY;
page.scrollTo(x, y); page.scrollTo(x, y);
}; };
export { scrollUp, scrollDown, scrollToTop, scrollToBottom } export { scrollUp, scrollDown, scrollTop, scrollBottom }

@ -2,6 +2,8 @@ export const TABS_PREV = 'tabs.prev';
export const TABS_NEXT = 'tabs.next'; export const TABS_NEXT = 'tabs.next';
export const SCROLL_UP = 'scroll.up'; export const SCROLL_UP = 'scroll.up';
export const SCROLL_DOWN = 'scroll.down'; export const SCROLL_DOWN = 'scroll.down';
export const SCROLL_TOP = 'scroll.top';
export const SCROLL_BOTTOM = 'scroll.bottom';
const BACKGROUND_ACTION_SET = new Set([ const BACKGROUND_ACTION_SET = new Set([
TABS_PREV, TABS_PREV,
@ -10,7 +12,9 @@ const BACKGROUND_ACTION_SET = new Set([
const CONTENT_ACTION_SET = new Set([ const CONTENT_ACTION_SET = new Set([
SCROLL_UP, SCROLL_UP,
SCROLL_DOWN SCROLL_DOWN,
SCROLL_TOP,
SCROLL_BOTTOM
]); ]);
export const isBackgroundAction = (action) => { export const isBackgroundAction = (action) => {