store input keys in content script
This commit is contained in:
parent
a6b197ca73
commit
8ff302a1f2
7 changed files with 92 additions and 119 deletions
|
@ -2,6 +2,9 @@ import operations from 'shared/operations';
|
||||||
import messages from 'content/messages';
|
import messages from 'content/messages';
|
||||||
import * as tabs from 'background/tabs';
|
import * as tabs from 'background/tabs';
|
||||||
import * as zooms from 'background/zooms';
|
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) => {
|
const sendConsoleShowCommand = (tab, command) => {
|
||||||
return browser.tabs.sendMessage(tab.id, {
|
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) {
|
switch (operation.type) {
|
||||||
case operations.TAB_CLOSE:
|
case operations.TAB_CLOSE:
|
||||||
return tabs.closeTab(tab.id);
|
return tabs.closeTab(tab.id);
|
||||||
|
@ -45,11 +84,8 @@ const exec = (operation, tab) => {
|
||||||
case operations.COMMAND_SHOW_BUFFER:
|
case operations.COMMAND_SHOW_BUFFER:
|
||||||
return sendConsoleShowCommand(tab, 'buffer ');
|
return sendConsoleShowCommand(tab, 'buffer ');
|
||||||
default:
|
default:
|
||||||
return browser.tabs.sendMessage(tab.id, {
|
return Promise.resolve();
|
||||||
type: messages.CONTENT_OPERATION,
|
|
||||||
operation
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { exec };
|
export { exec, execBackground };
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import * as settingsActions from 'actions/setting';
|
import * as settingsActions from 'actions/setting';
|
||||||
import messages from 'content/messages';
|
import messages from 'content/messages';
|
||||||
import BackgroundComponent from 'components/background';
|
import BackgroundComponent from 'components/background';
|
||||||
import BackgroundInputComponent from 'components/background-input';
|
|
||||||
import reducers from 'reducers';
|
import reducers from 'reducers';
|
||||||
import { createStore } from 'store';
|
import { createStore } from 'store';
|
||||||
|
|
||||||
|
@ -15,10 +14,8 @@ const store = createStore(reducers, (e, sender) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const backgroundComponent = new BackgroundComponent(store);
|
const backgroundComponent = new BackgroundComponent(store);
|
||||||
const backgroundInputComponent = new BackgroundInputComponent(store);
|
|
||||||
store.subscribe((sender) => {
|
store.subscribe((sender) => {
|
||||||
backgroundComponent.update(sender);
|
backgroundComponent.update(sender);
|
||||||
backgroundInputComponent.update(sender);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
store.dispatch(settingsActions.load());
|
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 messages from 'content/messages';
|
||||||
import * as inputActions from 'actions/input';
|
import * as operationActions from 'actions/operation';
|
||||||
import * as settingsActions from 'actions/setting';
|
import * as settingsActions from 'actions/setting';
|
||||||
import * as tabActions from 'actions/tab';
|
import * as tabActions from 'actions/tab';
|
||||||
import * as commands from 'shared/commands';
|
import * as commands from 'shared/commands';
|
||||||
|
@ -37,9 +37,10 @@ export default class BackgroundComponent {
|
||||||
|
|
||||||
onMessage(message, sender) {
|
onMessage(message, sender) {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case messages.KEYDOWN:
|
case messages.BACKGROUND_OPERATION:
|
||||||
return this.store.dispatch(
|
return this.store.dispatch(
|
||||||
inputActions.keyPress(message.key, message.ctrl), sender);
|
operationActions.execBackground(message.operation, sender.tab),
|
||||||
|
sender);
|
||||||
case messages.OPEN_URL:
|
case messages.OPEN_URL:
|
||||||
if (message.newTab) {
|
if (message.newTab) {
|
||||||
return this.store.dispatch(
|
return this.store.dispatch(
|
||||||
|
@ -70,7 +71,7 @@ export default class BackgroundComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
onTabUpdated(id, info) {
|
onTabUpdated(id, info) {
|
||||||
if (info.url) {
|
if (info.status === 'complete') {
|
||||||
this.syncSettings(id);
|
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 {
|
export default class ContentInputComponent {
|
||||||
constructor(target) {
|
constructor(target, store) {
|
||||||
this.pressed = {};
|
this.pressed = {};
|
||||||
|
this.store = store;
|
||||||
|
|
||||||
target.addEventListener('keypress', this.onKeyPress.bind(this));
|
target.addEventListener('keypress', this.onKeyPress.bind(this));
|
||||||
target.addEventListener('keydown', this.onKeyDown.bind(this));
|
target.addEventListener('keydown', this.onKeyDown.bind(this));
|
||||||
target.addEventListener('keyup', this.onKeyUp.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) {
|
onKeyPress(e) {
|
||||||
if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') {
|
if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') {
|
||||||
return;
|
return;
|
||||||
|
@ -30,18 +59,21 @@ export default class ContentInputComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
capture(e) {
|
capture(e) {
|
||||||
if (e.target instanceof HTMLInputElement ||
|
if (this.fromInput(e)) {
|
||||||
e.target instanceof HTMLTextAreaElement ||
|
|
||||||
e.target instanceof HTMLSelectElement) {
|
|
||||||
if (e.key === 'Escape' && e.target.blur) {
|
if (e.key === 'Escape' && e.target.blur) {
|
||||||
e.target.blur();
|
e.target.blur();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
browser.runtime.sendMessage({
|
if (e.key === 'OS') {
|
||||||
type: messages.KEYDOWN,
|
return;
|
||||||
key: e.key,
|
}
|
||||||
ctrl: e.ctrlKey
|
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 './console-frame.scss';
|
||||||
import * as consoleFrames from './console-frames';
|
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 settingActions from 'actions/setting';
|
||||||
import * as followActions from 'actions/follow';
|
|
||||||
import { createStore } from 'store';
|
import { createStore } from 'store';
|
||||||
import ContentInputComponent from 'components/content-input';
|
import ContentInputComponent from 'components/content-input';
|
||||||
import FollowComponent from 'components/follow';
|
import FollowComponent from 'components/follow';
|
||||||
import reducers from 'reducers';
|
import reducers from 'reducers';
|
||||||
import operations from 'shared/operations';
|
|
||||||
import messages from './messages';
|
import messages from './messages';
|
||||||
|
|
||||||
const store = createStore(reducers);
|
const store = createStore(reducers);
|
||||||
const followComponent = new FollowComponent(window.document.body, store);
|
const followComponent = new FollowComponent(window.document.body, store);
|
||||||
|
const contentInputComponent = new ContentInputComponent(window, store);
|
||||||
store.subscribe(() => {
|
store.subscribe(() => {
|
||||||
try {
|
try {
|
||||||
followComponent.update();
|
followComponent.update();
|
||||||
|
contentInputComponent.update();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// eslint-disable-next-line no-unused-vars
|
|
||||||
const contentInputComponent = new ContentInputComponent(window);
|
|
||||||
|
|
||||||
consoleFrames.initialize(window.document);
|
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) => {
|
browser.runtime.onMessage.addListener((action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case messages.CONSOLE_HIDE:
|
case messages.CONSOLE_HIDE:
|
||||||
window.focus();
|
window.focus();
|
||||||
consoleFrames.blur(window.document);
|
consoleFrames.blur(window.document);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
case messages.CONTENT_OPERATION:
|
|
||||||
execOperation(action.operation);
|
|
||||||
return Promise.resolve();
|
|
||||||
case messages.CONTENT_SET_SETTINGS:
|
case messages.CONTENT_SET_SETTINGS:
|
||||||
store.dispatch(settingActions.set(action.settings));
|
store.dispatch(settingActions.set(action.settings));
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export default {
|
export default {
|
||||||
CONTENT_OPERATION: 'content.operation',
|
|
||||||
CONTENT_SET_SETTINGS: 'content.set.settings',
|
CONTENT_SET_SETTINGS: 'content.set.settings',
|
||||||
|
BACKGROUND_OPERATION: 'background.operation',
|
||||||
|
|
||||||
CONSOLE_BLURRED: 'console.blured',
|
CONSOLE_BLURRED: 'console.blured',
|
||||||
CONSOLE_ENTERED: 'console.entered',
|
CONSOLE_ENTERED: 'console.entered',
|
||||||
|
@ -9,8 +9,6 @@ export default {
|
||||||
CONSOLE_SHOW_ERROR: 'console.show.error',
|
CONSOLE_SHOW_ERROR: 'console.show.error',
|
||||||
CONSOLE_HIDE: 'console.hide',
|
CONSOLE_HIDE: 'console.hide',
|
||||||
|
|
||||||
KEYDOWN: 'keydown',
|
|
||||||
|
|
||||||
OPEN_URL: 'open.url',
|
OPEN_URL: 'open.url',
|
||||||
|
|
||||||
SETTINGS_RELOAD: 'settings.reload',
|
SETTINGS_RELOAD: 'settings.reload',
|
||||||
|
|
Reference in a new issue