-
Notifications
You must be signed in to change notification settings - Fork 7
/
keybindings.js
251 lines (215 loc) · 6.95 KB
/
keybindings.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var current_binding;
var search_input_id = "emacsBindingsSearchInput";
chrome.runtime.sendMessage({action: "log", msg: {
'subsystem': 'content',
'level': 'debug',
'message': `Loading content script in ${document.title}`
}});
// recursively generate ESC <key> compat mappings for M-<key>
function generate_ESC_bindings(bindings){
var new_bindings = {}
var esc_bindings = {}
for (const key in bindings){
const value = bindings[key];
if (typeof value == "object"){
var submap = generate_ESC_bindings(value);
new_bindings[key] = submap;
continue;
}
new_bindings[key] = value;
if (key.startsWith("M-")){
esc_key = key.replace("M-", "");
esc_bindings[esc_key] = value;
}
}
if (Object.keys(esc_bindings).length > 0)
new_bindings['ESC'] = esc_bindings;
return new_bindings;
}
function create_search_dialog(){
var idString = "emacsBindingsSearchDialog";
let node = document.getElementById(idString);
if (node){
chrome.runtime.sendMessage({action: "log", msg: "Search box seems to exist already"});
node.showModal();
} else {
chrome.runtime.sendMessage({action: "log", msg: "Trying to create search box"});
var dialog = document.createElement("dialog");
dialog.id = idString;
dialog.role = 'dialog';
dialog.innerHTML = `
Forward search:
<form>
<label><input type="search" name="${search_input_id}" id="${search_input_id}" autofocus/><br/>
</form>
`
document.body.appendChild(dialog);
dialog.showModal();
}
}
const focus_window = () => {
if (document.activeElement) {
document.activeElement.blur();
}
}
const focus_first_input = () => {
for (var i = 0; document.forms[0].elements[i].type == 'hidden'; i++);
document.forms[0].elements[i].focus();
}
// toplevel keybindings without modifier can break some sites -> make it optional
var nomod_keybindings = {
"n": () => window.scrollBy(0, 30),
"p": () => window.scrollBy(0, -30),
"t": () => focus_first_input(),
}
var experimental_keybindings_dialog = {
"C-s": () => create_search_dialog(),
}
var experimental_keybindings_popup = {
"C-s": () => chrome.runtime.sendMessage({action: "search"}),
}
var body_keybindings = {
// scroll
"C-f": () => window.scrollBy(30, 0),
"C-b": () => window.scrollBy(-30, 0),
"C-n": () => window.scrollBy(0, 30),
"C-p": () => window.scrollBy(0, -30),
"M-<": () => window.scroll(0, 0),
"M->": () => window.scroll(0, document.body.scrollHeight),
// refresh history
"C-r": () => window.location.reload(),
"C-F": () => window.history.forward(),
"C-B": () => window.history.back(),
// tabs
"M-f": () => chrome.runtime.sendMessage({action: "next_tab"}),
"M-b": () => chrome.runtime.sendMessage({action: "previous_tab"}),
"C-h": {
"?": () => chrome.runtime.sendMessage({action: "options_page"}),
},
"C-x": {
"k": () => chrome.runtime.sendMessage({action: "close_tab"}),
"C-f": () => chrome.runtime.sendMessage({action: "new_tab"})
},
"C-u": {
"C-x": {
"k": () => chrome.runtime.sendMessage({action: "close_window"}),
"C-f": () => chrome.runtime.sendMessage({action: "new_window"})
}
}
}
var textarea_keybindings = {
"C-g": () => focus_window()
};
// initialise keybindings based on above tables + settings; this should
// be redone in a way allowing re-init on changed settings. As the bindings
// are per tab, and get updated on reload not high priority, though.
var generated_keybindings = generate_ESC_bindings(body_keybindings);
var generated_textarea_keybindings = {};
// this needs to happen before potentially adding nomod keybindings - having
// those on text fields would break stuff
Object.assign(generated_textarea_keybindings, generated_keybindings);
// add in top level bindings without modifier, if needed
chrome.storage.sync.get("bindings_without_modifier", function (setting) {
if (setting["bindings_without_modifier"] == true){
Object.assign(generated_keybindings, nomod_keybindings);
}
});
chrome.storage.sync.get("experimental", function (setting) {
if (setting["experimental"] == true){
chrome.storage.sync.get("preferred_input", function (setting) {
if (setting["preferred_input"] == "dialog"){
Object.assign(generated_keybindings, experimental_keybindings_dialog);
} else if (setting["preferred_input"] == "popup"){
Object.assign(generated_keybindings, experimental_keybindings_popup);
}
});
}
});
/**
* Turn KeyboardEvent to string.
* @param {KeyboardEvent} e
* @returns {String}
*/
const get_key = (e) => {
var key = e.key,
ctrl = e.ctrlKey ? "C-" : "",
meta = e.altKey ? "M-" : "";
if (e.key == "Escape")
return "ESC";
else
return ctrl + meta + key;
}
/**
* get current keybindings according to focus state.
* @param {string} target_type - current focus, either on textarea or window
* @return {Object} keybindings - keybindings that current page uses
*/
const get_current_bind = (target_type) =>
(target_type == "input" || target_type == "textarea"
? generated_textarea_keybindings : generated_keybindings);
document.addEventListener("keyup", (e) => {
var target_type = e.target.tagName.toLowerCase();
var target_id = e.target.id;
if (target_type == "input" && target_id == search_input_id){
var target_value = e.target.value;
if (target_value.length > 0){
chrome.runtime.sendMessage({action: "find", search: target_value});
} else {
chrome.runtime.sendMessage({action: "log", msg: "Search string too short"});
}
}
}, true);
document.addEventListener("keydown", (e) => {
if (e.key == "Shift" || e.key == "Control" || e.key == "Alt" || e.key == "Meta"){
chrome.runtime.sendMessage({action: "log", msg: {
'subsystem': 'keybinding',
'level': 'debug',
'message': "Ignoring modifier"
}});
return;
}
var key = get_key(e),
target_type = e.target.tagName.toLowerCase();
chrome.runtime.sendMessage({action: "log", msg: {
'subsystem': 'keybinding',
'level': 'debug',
'message': `user press key is ${key}, target type is ${target_type}`
}});
if (!current_binding) {
current_binding = get_current_bind(target_type);
}
chrome.runtime.sendMessage({action: "log", msg: {
'subsystem': 'keybinding',
'level': 'debug',
'message': `current binding is ${Object.keys(current_binding)}`
}});
var command = current_binding[key];
switch (typeof command) {
case "function":
command();
current_binding = null; // reset keybinding
e.preventDefault();
break;
case "object":
current_binding = command;
e.preventDefault();
break;
default:
current_binding = null;
break;
}
}, true);
chrome.runtime.onMessage.addListener((msg) => {
if (msg.action != "log")
chrome.runtime.sendMessage({action: "log", msg: {
'subsystem': 'content',
'level': 'debug',
'message': `action: ${msg.action}`
}});
switch(msg.action) {
case "focus_window":
focus_window();
break;
}
return true;
});