store input keys in content script

jh-changes
Shin'ya Ueoka 7 years ago
parent a6b197ca73
commit 8ff302a1f2
  1. 48
      src/actions/operation.js
  2. 3
      src/background/index.js
  3. 53
      src/components/background-input.js
  4. 9
      src/components/background.js
  5. 52
      src/components/content-input.js
  6. 42
      src/content/index.js
  7. 4
      src/content/messages.js

@ -2,6 +2,9 @@ import operations from 'shared/operations';
import messages from 'content/messages';
import * as tabs from 'background/tabs';
import * as zooms from 'background/zooms';
import * as scrolls from 'content/scrolls';
import * as navigates from 'content/navigates';
import * as followActions from 'actions/follow';
const sendConsoleShowCommand = (tab, command) => {
return browser.tabs.sendMessage(tab.id, {
@ -10,7 +13,43 @@ const sendConsoleShowCommand = (tab, command) => {
});
};
const exec = (operation, tab) => {
const exec = (operation) => {
switch (operation.type) {
case operations.SCROLL_LINES:
return scrolls.scrollLines(window, operation.count);
case operations.SCROLL_PAGES:
return scrolls.scrollPages(window, operation.count);
case operations.SCROLL_TOP:
return scrolls.scrollTop(window);
case operations.SCROLL_BOTTOM:
return scrolls.scrollBottom(window);
case operations.SCROLL_HOME:
return scrolls.scrollLeft(window);
case operations.SCROLL_END:
return scrolls.scrollRight(window);
case operations.FOLLOW_START:
return followActions.enable(false);
case operations.NAVIGATE_HISTORY_PREV:
return navigates.historyPrev(window);
case operations.NAVIGATE_HISTORY_NEXT:
return navigates.historyNext(window);
case operations.NAVIGATE_LINK_PREV:
return navigates.linkPrev(window);
case operations.NAVIGATE_LINK_NEXT:
return navigates.linkNext(window);
case operations.NAVIGATE_PARENT:
return navigates.parent(window);
case operations.NAVIGATE_ROOT:
return navigates.root(window);
default:
browser.runtime.sendMessage({
type: messages.BACKGROUND_OPERATION,
operation,
});
}
};
const execBackground = (operation, tab) => {
switch (operation.type) {
case operations.TAB_CLOSE:
return tabs.closeTab(tab.id);
@ -45,11 +84,8 @@ const exec = (operation, tab) => {
case operations.COMMAND_SHOW_BUFFER:
return sendConsoleShowCommand(tab, 'buffer ');
default:
return browser.tabs.sendMessage(tab.id, {
type: messages.CONTENT_OPERATION,
operation
});
return Promise.resolve();
}
};
export { exec };
export { exec, execBackground };

@ -1,7 +1,6 @@
import * as settingsActions from 'actions/setting';
import messages from 'content/messages';
import BackgroundComponent from 'components/background';
import BackgroundInputComponent from 'components/background-input';
import reducers from 'reducers';
import { createStore } from 'store';
@ -15,10 +14,8 @@ const store = createStore(reducers, (e, sender) => {
}
});
const backgroundComponent = new BackgroundComponent(store);
const backgroundInputComponent = new BackgroundInputComponent(store);
store.subscribe((sender) => {
backgroundComponent.update(sender);
backgroundInputComponent.update(sender);
});
store.dispatch(settingsActions.load());

@ -1,53 +0,0 @@
import * as inputActions from 'actions/input';
import * as operationActions from 'actions/operation';
export default class BackgroundInputComponent {
constructor(store) {
this.store = store;
this.keymaps = {};
this.prevInputs = [];
}
update(sender) {
let state = this.store.getState();
this.reloadSettings(state.setting);
this.handleKeyInputs(sender, state.input);
}
reloadSettings(setting) {
if (!setting.settings.json) {
return;
}
this.keymaps = JSON.parse(setting.settings.json).keymaps;
}
handleKeyInputs(sender, input) {
if (JSON.stringify(this.prevInputs) === JSON.stringify(input)) {
return;
}
this.prevInputs = input;
if (input.keys.length === 0) {
return;
}
if (sender) {
return this.handleKeysChanged(sender, input);
}
}
handleKeysChanged(sender, input) {
let matched = Object.keys(this.keymaps).filter((keyStr) => {
return keyStr.startsWith(input.keys);
});
if (matched.length === 0) {
this.store.dispatch(inputActions.clearKeys(), sender);
return Promise.resolve();
} else if (matched.length > 1 ||
matched.length === 1 && input.keys !== matched[0]) {
return Promise.resolve();
}
let operation = this.keymaps[matched];
this.store.dispatch(operationActions.exec(operation, sender.tab), sender);
this.store.dispatch(inputActions.clearKeys(), sender);
}
}

@ -1,5 +1,5 @@
import messages from 'content/messages';
import * as inputActions from 'actions/input';
import * as operationActions from 'actions/operation';
import * as settingsActions from 'actions/setting';
import * as tabActions from 'actions/tab';
import * as commands from 'shared/commands';
@ -37,9 +37,10 @@ export default class BackgroundComponent {
onMessage(message, sender) {
switch (message.type) {
case messages.KEYDOWN:
case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
inputActions.keyPress(message.key, message.ctrl), sender);
operationActions.execBackground(message.operation, sender.tab),
sender);
case messages.OPEN_URL:
if (message.newTab) {
return this.store.dispatch(
@ -70,7 +71,7 @@ export default class BackgroundComponent {
}
onTabUpdated(id, info) {
if (info.url) {
if (info.status === 'complete') {
this.syncSettings(id);
}
}

@ -1,14 +1,43 @@
import messages from 'content/messages';
import * as inputActions from 'actions/input';
import * as operationActions from 'actions/operation';
export default class ContentInputComponent {
constructor(target) {
constructor(target, store) {
this.pressed = {};
this.store = store;
target.addEventListener('keypress', this.onKeyPress.bind(this));
target.addEventListener('keydown', this.onKeyDown.bind(this));
target.addEventListener('keyup', this.onKeyUp.bind(this));
}
update() {
let settings = this.store.getState().setting.settings;
if (!settings) {
return;
}
let input = this.store.getState().input;
let keymaps = JSON.parse(settings.json).keymaps;
let matched = Object.keys(keymaps).filter((keyStr) => {
return keyStr.startsWith(input.keys);
});
if (matched.length === 0) {
this.store.dispatch(inputActions.clearKeys());
return Promise.resolve();
} else if (matched.length > 1 ||
matched.length === 1 && input.keys !== matched[0]) {
return Promise.resolve();
}
let operation = keymaps[matched];
try {
this.store.dispatch(operationActions.exec(operation));
} catch (e) {
console.error(e);
}
this.store.dispatch(inputActions.clearKeys());
}
onKeyPress(e) {
if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') {
return;
@ -30,18 +59,21 @@ export default class ContentInputComponent {
}
capture(e) {
if (e.target instanceof HTMLInputElement ||
e.target instanceof HTMLTextAreaElement ||
e.target instanceof HTMLSelectElement) {
if (this.fromInput(e)) {
if (e.key === 'Escape' && e.target.blur) {
e.target.blur();
}
return;
}
browser.runtime.sendMessage({
type: messages.KEYDOWN,
key: e.key,
ctrl: e.ctrlKey
});
if (e.key === 'OS') {
return;
}
this.store.dispatch(inputActions.keyPress(e.key, e.ctrlKey));
}
fromInput(e) {
return e.target instanceof HTMLInputElement ||
e.target instanceof HTMLTextAreaElement ||
e.target instanceof HTMLSelectElement;
}
}

@ -1,70 +1,32 @@
import './console-frame.scss';
import * as consoleFrames from './console-frames';
import * as scrolls from 'content/scrolls';
import * as navigates from 'content/navigates';
import * as settingActions from 'actions/setting';
import * as followActions from 'actions/follow';
import { createStore } from 'store';
import ContentInputComponent from 'components/content-input';
import FollowComponent from 'components/follow';
import reducers from 'reducers';
import operations from 'shared/operations';
import messages from './messages';
const store = createStore(reducers);
const followComponent = new FollowComponent(window.document.body, store);
const contentInputComponent = new ContentInputComponent(window, store);
store.subscribe(() => {
try {
followComponent.update();
contentInputComponent.update();
} catch (e) {
console.error(e);
}
});
// eslint-disable-next-line no-unused-vars
const contentInputComponent = new ContentInputComponent(window);
consoleFrames.initialize(window.document);
const execOperation = (operation) => {
switch (operation.type) {
case operations.SCROLL_LINES:
return scrolls.scrollLines(window, operation.count);
case operations.SCROLL_PAGES:
return scrolls.scrollPages(window, operation.count);
case operations.SCROLL_TOP:
return scrolls.scrollTop(window);
case operations.SCROLL_BOTTOM:
return scrolls.scrollBottom(window);
case operations.SCROLL_HOME:
return scrolls.scrollLeft(window);
case operations.SCROLL_END:
return scrolls.scrollRight(window);
case operations.FOLLOW_START:
return store.dispatch(followActions.enable(false));
case operations.NAVIGATE_HISTORY_PREV:
return navigates.historyPrev(window);
case operations.NAVIGATE_HISTORY_NEXT:
return navigates.historyNext(window);
case operations.NAVIGATE_LINK_PREV:
return navigates.linkPrev(window);
case operations.NAVIGATE_LINK_NEXT:
return navigates.linkNext(window);
case operations.NAVIGATE_PARENT:
return navigates.parent(window);
case operations.NAVIGATE_ROOT:
return navigates.root(window);
}
};
browser.runtime.onMessage.addListener((action) => {
switch (action.type) {
case messages.CONSOLE_HIDE:
window.focus();
consoleFrames.blur(window.document);
return Promise.resolve();
case messages.CONTENT_OPERATION:
execOperation(action.operation);
return Promise.resolve();
case messages.CONTENT_SET_SETTINGS:
store.dispatch(settingActions.set(action.settings));
return Promise.resolve();

@ -1,6 +1,6 @@
export default {
CONTENT_OPERATION: 'content.operation',
CONTENT_SET_SETTINGS: 'content.set.settings',
BACKGROUND_OPERATION: 'background.operation',
CONSOLE_BLURRED: 'console.blured',
CONSOLE_ENTERED: 'console.entered',
@ -9,8 +9,6 @@ export default {
CONSOLE_SHOW_ERROR: 'console.show.error',
CONSOLE_HIDE: 'console.hide',
KEYDOWN: 'keydown',
OPEN_URL: 'open.url',
SETTINGS_RELOAD: 'settings.reload',