implement simple scrolling

jh-changes
Shin'ya Ueoka 7 years ago
parent 8e5ceebf61
commit 6bdacc7758
  1. 14
      src/background/index.js
  2. 18
      src/content/index.js
  3. 27
      src/content/scrolls.js

@ -2,10 +2,14 @@ import * as tabs from './tabs';
const KEY_MAP = { const KEY_MAP = {
'tabs.prev': 104, 'tabs.prev': 104,
'tabs.next': 108 'tabs.next': 108,
'scroll.up': 107,
'scroll.down': 106
}; };
browser.runtime.onMessage.addListener((request, sender, sendResponse) => { browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
let response = null;
switch (request.key) { switch (request.key) {
case KEY_MAP['tabs.prev']: case KEY_MAP['tabs.prev']:
tabs.selectPrevTab(sender.tab.index); tabs.selectPrevTab(sender.tab.index);
@ -13,6 +17,12 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
case KEY_MAP['tabs.next']: case KEY_MAP['tabs.next']:
tabs.selectNextTab(sender.tab.index); tabs.selectNextTab(sender.tab.index);
break; break;
case KEY_MAP['scroll.up']:
response = 'scroll.up'
break;
case KEY_MAP['scroll.down']:
response = 'scroll.down'
break;
} }
sendResponse(); sendResponse(response);
}); });

@ -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) => { window.addEventListener("keypress", (e) => {
browser.runtime.sendMessage({ browser.runtime.sendMessage({
key: e.which || e.keyCode, key: e.which || e.keyCode,
@ -5,7 +18,10 @@ window.addEventListener("keypress", (e) => {
alt: e.alt, alt: e.alt,
meta: e.meta, meta: e.meta,
ctrl: e.ctrl, ctrl: e.ctrl,
}).then(() => { }).then((response) => {
if (response) {
invokeEvent(response);
}
}, (err) => { }, (err) => {
console.log(`Vim Vixen: ${err}`); console.log(`Vim Vixen: ${err}`);
}); });

@ -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 }