1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
import * as actions from '../shared/actions';
import * as tabs from './tabs';
import * as zooms from './zooms';
import KeyQueue from './key-queue';
const queue = new KeyQueue();
const keyPressHandle = (request, sender) => {
let action = queue.push({
code: request.code,
ctrl: request.ctrl
});
if (!action) {
return Promise.resolve();
}
if (actions.isBackgroundAction(action[0])) {
return doBackgroundAction(sender, action);
} else if (actions.isContentAction(action[0])) {
return Promise.resolve({
type: 'response.action',
action: action
});
}
return Promise.resolve();
};
const doBackgroundAction = (sender, action) => {
switch(action[0]) {
case actions.TABS_CLOSE:
return tabs.closeTab(sender.tab.id);
case actions.TABS_REOPEN:
return tabs.reopenTab();
case actions.TABS_PREV:
return tabs.selectPrevTab(sender.tab.index, actions[1] || 1);
case actions.TABS_NEXT:
return tabs.selectNextTab(sender.tab.index, actions[1] || 1);
case actions.TABS_RELOAD:
return tabs.reload(sender.tab, actions[1] || false);
case actions.ZOOM_IN:
return zooms.zoomIn();
case actions.ZOOM_OUT:
return zooms.zoomOut();
case actions.ZOOM_NEUTRAL:
return zooms.neutral();
}
return Promise.resolve();
}
const normalizeUrl = (string) => {
try {
return new URL(string).href
} catch (e) {
return 'http://' + string;
}
}
const cmdBuffer = (sender, arg) => {
if (isNaN(arg)) {
return tabs.selectByKeyword(sender.tab, arg);
} else {
let index = parseInt(arg, 10) - 1;
return tabs.selectAt(index);
}
}
const cmdEnterHandle = (request, sender) => {
let words = request.text.split(' ').filter((s) => s.length > 0);
switch (words[0]) {
case 'open':
return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
case 'tabopen':
return browser.tabs.create({ url: normalizeUrl(words[1]) });
case 'b':
case 'buffer':
return cmdBuffer(sender, words[1]);
}
throw new Error(words[0] + ' command is not defined');
};
browser.runtime.onMessage.addListener((request, sender) => {
switch (request.type) {
case 'event.keypress':
return keyPressHandle(request, sender);
case 'event.cmd.enter':
return cmdEnterHandle(request, sender);
case 'event.cmd.tabs.completion':
return tabs.getCompletions(request.text).then((tabs) => {
let items = tabs.map((tab) => {
return {
caption: tab.title,
content: tab.title,
url: tab.url,
icon: tab.favIconUrl
}
});
return {
name: "Buffers",
items: items
};
});
break;
}
return Promise.resolve();
});
|