-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconex-options-ui.js
354 lines (320 loc) · 11 KB
/
conex-options-ui.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const filePicker = $1('#file-picker');
const bg = browser.extension.getBackgroundPage();
filePicker.addEventListener('change', picker => {
const file = picker.target.files[0];
const reader = new FileReader();
reader.onload = function (r) {
try {
const json = JSON.parse(r.target.result);
const tabContainers = [];
const windows = [];
for (const w of json.windows) {
const windowTabContainers = {};
if (w.extData && w.extData['tabview-group']) {
const windowTabContainersJSON = JSON.parse(w.extData['tabview-group']);
for (const key in windowTabContainersJSON) {
if (windowTabContainersJSON[key].title) {
windowTabContainers[key] = windowTabContainersJSON[key].title;
tabContainers.push(windowTabContainers[key]);
}
}
}
const tabs = [];
for (const tab of w.tabs) {
if (tab.extData && tab.extData['tabview-tab']) {
const extData = JSON.parse(tab.extData['tabview-tab']);
if (extData && extData.groupID && windowTabContainers[Number(extData.groupID)]) {
tabs.push({ url: tab.entries[tab.entries.length - 1].url, container: windowTabContainers[Number(extData.groupID)] });
} else {
tabs.push({ url: tab.entries[tab.entries.length - 1].url, container: null });
}
} else {
tabs.push({ url: tab.entries[tab.entries.length - 1].url, container: null });
}
}
windows.push(tabs);
}
bg.restoreTabContainersBackup(tabContainers, windows);
} catch (e) { console.error(e); }
};
reader.readAsText(file);
});
async function fillShortcutFields(field) {
const commands = await browser.commands.getAll();
for(command of commands) {
if(field == undefined || field == command.name) {
$1('#' + command.name).value = command.shortcut;
}
}
}
async function setupShortcutListeners() {
const isMac = /^Mac/i.test(navigator.platform);
fillShortcutFields();
for (input of $('.keyboard-shortcut')) {
input.addEventListener('focus', e => {
e.target.value = "";
e.target.placeholder = "type shortcut";
});
input.addEventListener('blur', e => {
fillShortcutFields(e.target.id);
});
input.addEventListener('keypress', e => {
if(typeof browser.commands.update != 'function') {
alert('shortcut remapping is only available in Firefox >= v60');
return;
}
const normalizedKey = normalizeKey(e.code);
if((e.ctrlKey || e.altKey || e.metaKey) && normalizedKey) {
const shortcutParts = [e.ctrlKey ? (isMac ? "MacCtrl" : "Ctrl") : (e.altKey ? "Alt" : "Command")];
if (e.shiftKey) {
shortcutParts.push("Shift");
}
shortcutParts.push(normalizedKey);
const shortcut = shortcutParts.join("+");
for(const e of $('.keyboard-shortcut')) {
if(shortcut == e.value) {
alert('Key combinations must differ');
e.target.blur();
return;
}
}
e.target.value = shortcut;
browser.commands.update({
name: e.target.id,
shortcut: shortcut
});
console.debug(`mapping ${e.target.id} to ${shortcut}`);
e.target.blur();
} else {
alert(`
Key combinations must consist of two or three keys:
- modifier (mandatory, except for function keys). This can be any of: "Ctrl", "Alt", "Command", "MacCtrl".
- secondary modifier (optional). If supplied, this must be "Shift".
- key (mandatory). This can be any one of:
the letters A-Z
the numbers 0-9
the function keys F1-F12
Comma, Period, Home, End, PageUp, PageDown, Space, Insert, Delete, Up, Down, Left, Right
NOTE: if a key-combination triggers a different action, you will not create a new Shortcut -- but
re-assigning does work (try for example Alt+S).
`);
e.target.blur();
}
});
}
}
// from: https://github.com/piroor/webextensions-lib-shortcut-customize-ui/blob/master/ShortcutCustomizeUI.js
function normalizeKey(value) {
const aKey = value.trim().replace(/^Digit/,"").replace(/^Key/,"").toLowerCase();
const normalizedKey = aKey.replace(/\s+/g, '');
if (/^[a-z0-9]$/i.test(normalizedKey) ||
/^F([1-9]|1[0-2])$/i.test(normalizedKey))
return aKey.toUpperCase();
switch (normalizedKey) {
case 'comma':
return 'Comma';
case 'period':
return 'Period';
case 'home':
return 'Home';
case 'end':
return 'End';
case 'pageup':
return 'PageUp';
case 'pagedown':
return 'PageDown';
case 'space':
return 'Space';
case 'del':
case 'delete':
return 'Delete';
case 'up':
return 'Up';
case 'down':
return 'Down';
case 'left':
return 'Left';
case 'right':
return 'right';
case 'next':
case 'medianexttrack':
return 'MediaNextTrack';
case 'play':
case 'pause':
case 'mediaplaypause':
return 'MediaPlayPause';
case 'prev':
case 'previous':
case 'mediaprevtrack':
return 'MediaPrevTrack';
case 'stop':
case 'mediastop':
return 'MediaStop';
default:
for (let map of [keyNameMapLocales[browser.i18n.getUILanguage()] ||
keyNameMapLocales[browser.i18n.getUILanguage().replace(/[-_].+$/, '')] ||
{}, keyNameMapLocales.global]) {
for (let key of Object.keys(map)) {
if (Array.isArray(map[key])) {
if (map[key].some(aLocalizedKey => aLocalizedKey.toLowerCase() == aKey))
return key;
}
else {
if (map[key] &&
map[key].toLowerCase() == aKey)
return key;
}
}
}
break;
}
return '';
}
const keyNameMapLocales = {
global: {
Comma: [','],
Period: ['.'],
Space: [' '],
Up: ['↑'],
Down: ['↓'],
Left: ['←', '<=', '<-'],
Right: ['→', '=>', '->'],
},
// define tables with https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/i18n/LanguageCode
ja: {
// key: valid key name listed at https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/commands#Shortcut_values
// value: array of localized key names
Up: ['上'],
Down: ['下'],
Left: ['左'],
Right: ['右'],
// you can localize modifier keys also.
// Alt: ['オルト'],
// Ctrl: ['コントロール'],
// MacCtrl: ['コントロール'], // for macOS
// Command: ['コマンド`], // for macOS
// Shift: ['シフト`],
},
ru: {
// key: valid key name listed at https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/commands#Shortcut_values
// value: array of localized key names
Up: ['Вверх'],
Down: ['Вниз'],
Left: ['Влево'],
Right: ['Вправо'],
Comma: ['Запятая'],
Period: ['Точка'],
Space: ['Пробел'],
MediaNextTrack: ['Следующий трек'],
MediaPrevTrack: ['Предыдущий трек'],
MediaPlayPause: ['Пауза проигрывания'],
MediaStop: ['Остановка проигрывания']
},
// de: {...},
// fr: {...},
}
setupShortcutListeners();
browser.contextualIdentities.query({}).then(identities => {
if (!identities) {
document.querySelector('#missing-tab-container-support').style.display = 'block';
}
}, e => console.error(e));
var handlePermission = function(setting, value) {
return new Promise((resolve, reject) => {
const mapping = {
'search-bookmarks': {permissions: ['bookmarks']},
'search-history': {permissions: ['history']},
'hide-tabs': {permissions: ['tabHide']},
/* 'create-thumbnail': {origins: ['<all_urls>']}, <all_urls> does not work correctly for optional permissions :( */
};
const permissions = mapping[setting];
if (permissions) {
if (value) {
permissionQueryOpen = true;
browser.permissions.request(permissions).then(success => {
browser.permissions.getAll().then(permissions => console.debug('current conex permissions:', permissions.permissions, 'origins:', permissions.origins));
resolve(success);
}).catch(e => {
console.error(`error requesting permission ${setting}`);
reject(e);
});
} else {
browser.permissions.remove(permissions).then(success => {
browser.permissions.getAll().then(permissions => console.debug('current conex permissions:', permissions.permissions, 'origins:', permissions.origins));
resolve(success);
}).catch(e => {
console.error(`error removing permission ${setting}`);
reject(e);
});
}
} else {
resolve(true);
}
});
}
readSettings.then(_ => {
for (const key in settings) {
const checkbox = $1('#' + key);
if(settings[key] && checkbox) {
checkbox.checked = 'checked';
}
}
});
let permissionQueryOpen = false;
for(const element of $('input[type=checkbox]')) {
element.addEventListener('click', event => {
const settingId = 'conex/settings/' + event.target.id;
const value = event.target.checked;
if(permissionQueryOpen) {
event.target.checked = !event.target.checked;
return;
}
const handlePermissionResult = function(success) {
permissionQueryOpen = false;
if (success) {
console.info(`setting ${settingId} to ${value}`);
browser.storage.local.set({ [settingId]: value }).catch(e => {
console.error(`error setting ${settingId} to ${value}: ${e}`)
});
bg.refreshSettings();
} else {
event.target.checked = !event.target.checked;
}
}
if (event.target.id == 'hide-tabs') {
if (value == true) {
handlePermission(event.target.id, value).then(success => {
if(success) {
browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT }).then(tabs => {
if(tabs.length > 0) {
const activeTab = tabs[0];
bg.showCurrentContainerTabsOnly(activeTab.id);
} else {
console.error("did not find any active tab in window with id: ", browser.windows.WINDOW_ID_CURRENT);
}
});
}
handlePermissionResult(success);
}).catch(e => {
permissionQueryOpen = false;
consol.error('error handling permissions:', e);
});
} else {
browser.tabs.query({}).then(tabs => {
browser.tabs.show(tabs.map(t => t.id)).then(_ => {
handlePermission(event.target.id, value).then(success => {
handlePermissionResult(success);
}).catch(e => {
permissionQueryOpen = false;
consol.error('error handling permissions:', e);
});
});
});
}
} else {
handlePermission(event.target.id, value).then(success => {
handlePermissionResult(success);
});
}
});
}