2022-02-18 00:47:22 +00:00
|
|
|
/* global API */// msg.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global prefs */
|
|
|
|
/* global t */// localization.js
|
2022-02-18 00:47:22 +00:00
|
|
|
/* global $ $$ getEventKeyName messageBoxProxy setupLivePrefs */// dom.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global
|
|
|
|
CHROME_POPUP_BORDER_BUG
|
|
|
|
FIREFOX
|
|
|
|
URLS
|
2022-01-23 09:44:25 +00:00
|
|
|
clamp
|
2021-01-01 14:27:58 +00:00
|
|
|
ignoreChromeError
|
|
|
|
openURL
|
|
|
|
*/// toolbox.js
|
2017-02-14 15:35:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2020-10-02 15:10:52 +00:00
|
|
|
$$('input[min], input[max]').forEach(enforceInputRange);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (CHROME_POPUP_BORDER_BUG) {
|
2022-01-27 12:27:10 +00:00
|
|
|
$('.chrome-no-popup-border').classList.remove('chrome-no-popup-border');
|
2017-09-09 14:21:17 +00:00
|
|
|
}
|
2018-04-12 18:02:34 +00:00
|
|
|
if (FIREFOX && 'update' in (chrome.commands || {})) {
|
2022-02-18 00:47:22 +00:00
|
|
|
$('#shortcuts').classList.remove('chromium-only');
|
2020-10-22 19:16:55 +00:00
|
|
|
}
|
2017-02-14 15:35:53 +00:00
|
|
|
// actions
|
2020-02-02 04:36:54 +00:00
|
|
|
$('#options-close-icon').onclick = () => {
|
|
|
|
top.dispatchEvent(new CustomEvent('closeOptions'));
|
|
|
|
};
|
2022-02-18 00:47:22 +00:00
|
|
|
$('#manage').onclick = () => {
|
|
|
|
API.openManage();
|
|
|
|
};
|
|
|
|
$('#shortcuts').onclick = () => {
|
|
|
|
if (FIREFOX) {
|
|
|
|
customizeHotkeys();
|
|
|
|
} else {
|
|
|
|
openURL({url: URLS.configureCommands});
|
2017-02-15 05:48:47 +00:00
|
|
|
}
|
2017-03-25 06:30:34 +00:00
|
|
|
};
|
2022-02-18 00:47:22 +00:00
|
|
|
$('#reset').onclick = async () => {
|
|
|
|
if (await messageBoxProxy.confirm(t('confirmDiscardChanges'))) {
|
|
|
|
for (const el of $$('input')) {
|
|
|
|
const id = el.id || el.name;
|
|
|
|
if (prefs.knownKeys.includes(id)) {
|
|
|
|
prefs.reset(id);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-02-14 15:24:49 +00:00
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2022-02-18 00:47:22 +00:00
|
|
|
};
|
2017-06-28 10:49:04 +00:00
|
|
|
|
2018-04-12 18:02:34 +00:00
|
|
|
function customizeHotkeys() {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBoxProxy.show({
|
2018-04-12 18:02:34 +00:00
|
|
|
title: t('shortcutsNote'),
|
2022-02-18 00:47:22 +00:00
|
|
|
contents: t.template.shortcutsFF.cloneNode(true),
|
|
|
|
className: 'center-dialog pre-line',
|
2018-04-12 18:02:34 +00:00
|
|
|
buttons: [t('confirmClose')],
|
|
|
|
onshow(box) {
|
2022-02-18 00:47:22 +00:00
|
|
|
box.oninput = onInput;
|
|
|
|
setupLivePrefs($$('input', box).map(el => el.id));
|
2018-04-12 18:02:34 +00:00
|
|
|
},
|
|
|
|
});
|
2022-02-18 00:47:22 +00:00
|
|
|
async function onInput({target: el}) {
|
|
|
|
const name = el.id.split('.')[1];
|
|
|
|
const shortcut = el.value.trim();
|
2018-04-13 11:30:36 +00:00
|
|
|
if (!shortcut) {
|
|
|
|
browser.commands.reset(name).catch(ignoreChromeError);
|
2022-02-18 00:47:22 +00:00
|
|
|
el.setCustomValidity('');
|
2018-04-12 18:02:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2022-02-18 00:47:22 +00:00
|
|
|
await browser.commands.update({name, shortcut});
|
|
|
|
el.setCustomValidity('');
|
2018-04-12 18:02:34 +00:00
|
|
|
} catch (err) {
|
2022-02-18 00:47:22 +00:00
|
|
|
el.setCustomValidity(err);
|
2018-04-12 18:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 04:36:54 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function enforceInputRange(element) {
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
|
|
|
const doNotify = () => element.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
const onChange = ({type}) => {
|
|
|
|
if (type === 'input' && element.checkValidity()) {
|
|
|
|
doNotify();
|
|
|
|
} else if (type === 'change' && !element.checkValidity()) {
|
2022-01-23 09:44:25 +00:00
|
|
|
element.value = clamp(Number(element.value), min, max);
|
2021-01-01 14:27:58 +00:00
|
|
|
doNotify();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
element.on('change', onChange);
|
|
|
|
element.on('input', onChange);
|
|
|
|
}
|
|
|
|
|
2020-02-02 04:36:54 +00:00
|
|
|
window.onkeydown = event => {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (getEventKeyName(event) === 'Escape') {
|
2020-02-02 04:36:54 +00:00
|
|
|
top.dispatchEvent(new CustomEvent('closeOptions'));
|
|
|
|
}
|
|
|
|
};
|