2021-01-01 14:27:58 +00:00
|
|
|
/* global API msg */// msg.js
|
|
|
|
/* global prefs */
|
|
|
|
/* global t */// localization.js
|
|
|
|
/* global
|
|
|
|
$
|
|
|
|
$$
|
|
|
|
$create
|
|
|
|
$createLink
|
|
|
|
getEventKeyName
|
|
|
|
messageBoxProxy
|
|
|
|
setupLivePrefs
|
|
|
|
*/// dom.js
|
|
|
|
/* global
|
|
|
|
CHROME
|
|
|
|
CHROME_POPUP_BORDER_BUG
|
|
|
|
FIREFOX
|
2022-01-27 12:21:02 +00:00
|
|
|
UA
|
2021-01-01 14:27:58 +00:00
|
|
|
URLS
|
|
|
|
capitalize
|
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);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
if (CHROME_POPUP_BORDER_BUG) {
|
2019-09-24 13:21:36 +00:00
|
|
|
const borderOption = $('.chrome-no-popup-border');
|
|
|
|
if (borderOption) {
|
|
|
|
borderOption.classList.remove('chrome-no-popup-border');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 17:52:45 +00:00
|
|
|
// collapse #advanced block in Chrome pre-66 (classic chrome://extensions UI)
|
2022-01-27 12:21:02 +00:00
|
|
|
if (!FIREFOX && !UA.opera && CHROME < 66) {
|
2017-09-09 14:21:17 +00:00
|
|
|
const block = $('#advanced');
|
2018-03-29 03:27:37 +00:00
|
|
|
$('h1', block).onclick = event => {
|
|
|
|
event.preventDefault();
|
|
|
|
block.classList.toggle('collapsed');
|
|
|
|
const isCollapsed = block.classList.contains('collapsed');
|
|
|
|
const visibleToggle = $(isCollapsed ? '.is-collapsed' : '.is-expanded', block);
|
|
|
|
visibleToggle.focus();
|
2017-09-09 14:21:17 +00:00
|
|
|
};
|
2017-12-09 15:25:44 +00:00
|
|
|
block.classList.add('collapsible', 'collapsed');
|
2017-09-09 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 18:02:34 +00:00
|
|
|
if (FIREFOX && 'update' in (chrome.commands || {})) {
|
|
|
|
$('[data-cmd="open-keyboard"]').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'));
|
|
|
|
};
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
document.onclick = e => {
|
2017-04-14 15:30:09 +00:00
|
|
|
const target = e.target.closest('[data-cmd]');
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// prevent double-triggering in case a sub-element was clicked
|
|
|
|
e.stopPropagation();
|
2017-02-14 15:35:53 +00:00
|
|
|
|
2017-04-14 15:30:09 +00:00
|
|
|
switch (target.dataset.cmd) {
|
2017-03-26 02:30:59 +00:00
|
|
|
case 'open-manage':
|
2020-02-02 04:36:54 +00:00
|
|
|
API.openManage();
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'check-updates':
|
2017-04-20 18:27:10 +00:00
|
|
|
checkUpdates();
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'open-keyboard':
|
2018-04-12 18:02:34 +00:00
|
|
|
if (FIREFOX) {
|
|
|
|
customizeHotkeys();
|
|
|
|
} else {
|
|
|
|
openURL({url: URLS.configureCommands});
|
|
|
|
}
|
2017-04-09 06:43:51 +00:00
|
|
|
e.preventDefault();
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
2017-03-25 06:30:34 +00:00
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
case 'reset':
|
|
|
|
$$('input')
|
2021-01-01 14:27:58 +00:00
|
|
|
.filter(input => prefs.knownKeys.includes(input.id))
|
2017-04-04 17:21:03 +00:00
|
|
|
.forEach(input => prefs.reset(input.id));
|
|
|
|
break;
|
2017-02-15 05:48:47 +00:00
|
|
|
}
|
2017-03-25 06:30:34 +00:00
|
|
|
};
|
2017-04-20 18:27:10 +00:00
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
// sync to cloud
|
|
|
|
(() => {
|
2021-01-01 14:27:58 +00:00
|
|
|
const elCloud = $('.sync-options .cloud-name');
|
|
|
|
const elStart = $('.sync-options .connect');
|
|
|
|
const elStop = $('.sync-options .disconnect');
|
|
|
|
const elSyncNow = $('.sync-options .sync-now');
|
|
|
|
const elStatus = $('.sync-options .sync-status');
|
|
|
|
const elLogin = $('.sync-options .sync-login');
|
2021-12-12 00:05:58 +00:00
|
|
|
const elDriveOptions = $('.sync-options .drive-options');
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {Sync.Status} */
|
2019-11-05 19:30:45 +00:00
|
|
|
let status = {};
|
|
|
|
msg.onExtension(e => {
|
|
|
|
if (e.method === 'syncStatusUpdate') {
|
2021-01-01 14:27:58 +00:00
|
|
|
setStatus(e.status);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
API.sync.getStatus()
|
|
|
|
.then(setStatus);
|
|
|
|
|
|
|
|
elCloud.on('change', updateButtons);
|
|
|
|
for (const [btn, fn] of [
|
2021-12-12 00:05:58 +00:00
|
|
|
[elStart, async () => {
|
|
|
|
await API.sync.setDriveOptions(elCloud.value, getDriveOptions());
|
|
|
|
await API.sync.start(elCloud.value);
|
|
|
|
}],
|
2021-01-01 14:27:58 +00:00
|
|
|
[elStop, API.sync.stop],
|
|
|
|
[elSyncNow, API.sync.syncNow],
|
2021-12-09 04:00:38 +00:00
|
|
|
[elLogin, async () => {
|
|
|
|
await API.sync.login();
|
|
|
|
await API.sync.syncNow();
|
|
|
|
}],
|
2021-01-01 14:27:58 +00:00
|
|
|
]) {
|
|
|
|
btn.on('click', e => {
|
|
|
|
if (getEventKeyName(e) === 'MouseL') {
|
|
|
|
fn();
|
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:05:58 +00:00
|
|
|
function getDriveOptions() {
|
|
|
|
const result = {};
|
|
|
|
for (const el of $$(`[data-drive=${elCloud.value}] [data-option]`)) {
|
|
|
|
result[el.dataset.option] = el.value;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDriveOptions(options) {
|
|
|
|
for (const el of $$(`[data-drive=${elCloud.value}] [data-option]`)) {
|
|
|
|
el.value = options[el.dataset.option] || '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function setStatus(newStatus) {
|
|
|
|
status = newStatus;
|
|
|
|
updateButtons();
|
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-12-12 00:05:58 +00:00
|
|
|
async function updateButtons() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {state, STATES} = status;
|
|
|
|
const isConnected = state === STATES.connected;
|
|
|
|
const isDisconnected = state === STATES.disconnected;
|
2019-11-05 19:30:45 +00:00
|
|
|
if (status.currentDriveName) {
|
2021-01-01 14:27:58 +00:00
|
|
|
elCloud.value = status.currentDriveName;
|
|
|
|
}
|
|
|
|
for (const [el, enable] of [
|
|
|
|
[elCloud, isDisconnected],
|
2021-12-12 00:05:58 +00:00
|
|
|
[elDriveOptions, isDisconnected],
|
2021-01-01 14:27:58 +00:00
|
|
|
[elStart, isDisconnected && elCloud.value !== 'none'],
|
|
|
|
[elStop, isConnected && !status.syncing],
|
2021-02-14 15:24:49 +00:00
|
|
|
[elSyncNow, isConnected && !status.syncing && status.login],
|
2021-01-01 14:27:58 +00:00
|
|
|
]) {
|
|
|
|
el.disabled = !enable;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
elStatus.textContent = getStatusText();
|
|
|
|
elLogin.hidden = !isConnected || status.login;
|
2021-12-12 00:05:58 +00:00
|
|
|
for (const el of elDriveOptions.children) {
|
|
|
|
el.hidden = el.dataset.drive !== elCloud.value;
|
|
|
|
}
|
|
|
|
setDriveOptions(await API.sync.getDriveOptions(elCloud.value));
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getStatusText() {
|
|
|
|
if (status.syncing) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {phase, loaded, total} = status.progress || {};
|
2021-02-14 15:24:49 +00:00
|
|
|
return phase
|
2021-01-01 14:27:58 +00:00
|
|
|
? t(`optionsSyncStatus${capitalize(phase)}`, [loaded + 1, total], false) ||
|
|
|
|
`${phase} ${loaded} / ${total}`
|
|
|
|
: t('optionsSyncStatusSyncing');
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-02-14 15:24:49 +00:00
|
|
|
|
|
|
|
const {state, errorMessage, STATES} = status;
|
|
|
|
if (errorMessage && (state === STATES.connected || state === STATES.disconnected)) {
|
|
|
|
return errorMessage;
|
|
|
|
}
|
|
|
|
if (state === STATES.connected && !status.login) {
|
|
|
|
return t('optionsSyncStatusRelogin');
|
|
|
|
}
|
|
|
|
return t(`optionsSyncStatus${capitalize(state)}`, null, false) || state;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2017-04-20 18:27:10 +00:00
|
|
|
function checkUpdates() {
|
|
|
|
let total = 0;
|
|
|
|
let checked = 0;
|
|
|
|
let updated = 0;
|
2017-04-24 13:29:48 +00:00
|
|
|
const maxWidth = $('#update-progress').parentElement.clientWidth;
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
chrome.runtime.onConnect.addListener(function onConnect(port) {
|
|
|
|
if (port.name !== 'updater') return;
|
|
|
|
port.onMessage.addListener(observer);
|
|
|
|
chrome.runtime.onConnect.removeListener(onConnect);
|
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
API.updater.checkAllStyles({observe: true});
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
function observer(info) {
|
|
|
|
if ('count' in info) {
|
|
|
|
total = info.count;
|
|
|
|
document.body.classList.add('update-in-progress');
|
|
|
|
} else if (info.updated) {
|
|
|
|
updated++;
|
|
|
|
checked++;
|
|
|
|
} else if (info.error) {
|
|
|
|
checked++;
|
|
|
|
} else if (info.done) {
|
|
|
|
document.body.classList.remove('update-in-progress');
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
2017-04-24 13:29:48 +00:00
|
|
|
$('#update-progress').style.width = Math.round(checked / total * maxWidth) + 'px';
|
|
|
|
$('#updates-installed').dataset.value = updated || '';
|
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
2017-06-28 10:49:04 +00:00
|
|
|
|
2018-04-12 18:02:34 +00:00
|
|
|
function customizeHotkeys() {
|
|
|
|
// command name -> i18n id
|
|
|
|
const hotkeys = new Map([
|
|
|
|
['_execute_browser_action', 'optionsCustomizePopup'],
|
|
|
|
['openManage', 'openManage'],
|
|
|
|
['styleDisableAll', 'disableAllStyles'],
|
|
|
|
]);
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBoxProxy.show({
|
2018-04-12 18:02:34 +00:00
|
|
|
title: t('shortcutsNote'),
|
|
|
|
contents: [
|
|
|
|
$create('table',
|
|
|
|
[...hotkeys.entries()].map(([cmd, i18n]) =>
|
|
|
|
$create('tr', [
|
|
|
|
$create('td', t(i18n)),
|
|
|
|
$create('td',
|
|
|
|
$create('input', {
|
|
|
|
id: 'hotkey.' + cmd,
|
2018-04-13 11:30:36 +00:00
|
|
|
type: 'search',
|
2018-04-12 18:02:34 +00:00
|
|
|
//placeholder: t('helpKeyMapHotkey'),
|
|
|
|
})),
|
|
|
|
]))),
|
|
|
|
],
|
|
|
|
className: 'center',
|
|
|
|
buttons: [t('confirmClose')],
|
|
|
|
onshow(box) {
|
|
|
|
const ids = [];
|
|
|
|
for (const cmd of hotkeys.keys()) {
|
|
|
|
const id = 'hotkey.' + cmd;
|
|
|
|
ids.push(id);
|
|
|
|
$('#' + id).oninput = onInput;
|
|
|
|
}
|
|
|
|
setupLivePrefs(ids);
|
|
|
|
$('button', box).insertAdjacentElement('beforebegin',
|
|
|
|
$createLink(
|
|
|
|
'https://developer.mozilla.org/Add-ons/WebExtensions/manifest.json/commands#Key_combinations',
|
|
|
|
t('helpAlt')));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function onInput() {
|
2018-04-13 11:30:36 +00:00
|
|
|
const name = this.id.split('.')[1];
|
|
|
|
const shortcut = this.value.trim();
|
|
|
|
if (!shortcut) {
|
|
|
|
browser.commands.reset(name).catch(ignoreChromeError);
|
2018-04-12 18:02:34 +00:00
|
|
|
this.setCustomValidity('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2018-04-13 11:30:36 +00:00
|
|
|
browser.commands.update({name, shortcut}).then(
|
2018-04-12 18:02:34 +00:00
|
|
|
() => this.setCustomValidity(''),
|
|
|
|
err => this.setCustomValidity(err)
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
this.setCustomValidity(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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'));
|
|
|
|
}
|
|
|
|
};
|