implement simple open/tabopen command
This commit is contained in:
parent
f66b7a1c21
commit
9a808f45ed
3 changed files with 53 additions and 18 deletions
2
src/background/commands.js
Normal file
2
src/background/commands.js
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const OPEN = 'open';
|
||||||
|
export const TABOPEN = 'tabopen';
|
|
@ -1,18 +1,28 @@
|
||||||
import * as actions from '../shared/actions';
|
import * as actions from '../shared/actions';
|
||||||
import * as tabs from './tabs';
|
import * as tabs from './tabs';
|
||||||
|
import * as commands from './commands';
|
||||||
import KeyQueue from './key-queue';
|
import KeyQueue from './key-queue';
|
||||||
|
|
||||||
const queue = new KeyQueue();
|
const queue = new KeyQueue();
|
||||||
|
|
||||||
const keyDownHandle = (request) => {
|
const keyDownHandle = (request, sender, sendResponse) => {
|
||||||
return queue.push({
|
let action = queue.push({
|
||||||
code: request.code,
|
code: request.code,
|
||||||
shift: request.shift,
|
shift: request.shift,
|
||||||
ctrl: request.ctrl,
|
ctrl: request.ctrl,
|
||||||
alt: request.alt,
|
alt: request.alt,
|
||||||
meta: request.meta
|
meta: request.meta
|
||||||
})
|
});
|
||||||
}
|
if (!action) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actions.isBackgroundAction(action[0])) {
|
||||||
|
doBackgroundAction(sender, action);
|
||||||
|
} else if (actions.isContentAction(action[0])) {
|
||||||
|
sendResponse(action);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const doBackgroundAction = (sender, action) => {
|
const doBackgroundAction = (sender, action) => {
|
||||||
switch(action[0]) {
|
switch(action[0]) {
|
||||||
|
@ -25,22 +35,32 @@ const doBackgroundAction = (sender, action) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
const normalizeUrl = (string) => {
|
||||||
let action = null;
|
return 'http://' + string;
|
||||||
|
}
|
||||||
|
|
||||||
switch (request.type) {
|
const cmdEnterHandle = (request, sender) => {
|
||||||
case 'event.keydown':
|
let words = request.text.split(' ').filter((s) => s.length > 0);
|
||||||
action = keyDownHandle(request);
|
switch (words[0]) {
|
||||||
break;
|
case commands.OPEN:
|
||||||
}
|
browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
|
||||||
|
return;
|
||||||
if (action == null) {
|
case commands.TABOPEN:
|
||||||
|
browser.tabs.create({ url: normalizeUrl(words[1]) });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (actions.isBackgroundAction(action[0])) {
|
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
doBackgroundAction(sender, action);
|
switch (request.type) {
|
||||||
} else if (actions.isContentAction(action[0])) {
|
case 'event.keydown':
|
||||||
sendResponse(action);
|
keyDownHandle(request, sender, sendResponse);
|
||||||
|
break;
|
||||||
|
case 'event.cmd.enter':
|
||||||
|
cmdEnterHandle(request, sender, sendResponse);
|
||||||
|
break;
|
||||||
|
case 'event.cmd.suggest':
|
||||||
|
// TODO make suggestion and return via sendResponse
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,7 +12,20 @@ const invokeEvent = (action) => {
|
||||||
switch (action[0]) {
|
switch (action[0]) {
|
||||||
case actions.CMD_OPEN:
|
case actions.CMD_OPEN:
|
||||||
footer = new FooterLine(document);
|
footer = new FooterLine(document);
|
||||||
footer.input.value = ':';
|
footer.onPromptChange((e) => {
|
||||||
|
let request = {
|
||||||
|
type: 'event.cmd.suggest',
|
||||||
|
text: e.target.value
|
||||||
|
};
|
||||||
|
browser.runtime.sendMessage(request);
|
||||||
|
});
|
||||||
|
footer.onEntered((e) => {
|
||||||
|
let request = {
|
||||||
|
type: 'event.cmd.enter',
|
||||||
|
text: e.target.value
|
||||||
|
};
|
||||||
|
browser.runtime.sendMessage(request);
|
||||||
|
});
|
||||||
footer.focus();
|
footer.focus();
|
||||||
break;
|
break;
|
||||||
case actions.SCROLL_UP:
|
case actions.SCROLL_UP:
|
||||||
|
|
Reference in a new issue