2018-11-07 06:09:29 +00:00
|
|
|
/* global messageBox msg setupLivePrefs enforceInputRange
|
|
|
|
$ $$ $create $createLink
|
2019-09-24 13:21:36 +00:00
|
|
|
FIREFOX OPERA CHROME URLS openURL prefs t API ignoreChromeError
|
2019-11-05 19:30:45 +00:00
|
|
|
CHROME_HAS_BORDER_BUG capitalize */
|
2017-02-14 15:35:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-06-28 10:49:04 +00:00
|
|
|
setupRadioButtons();
|
2017-04-05 13:14:59 +00:00
|
|
|
enforceInputRange($('#popupWidth'));
|
2017-12-12 00:27:28 +00:00
|
|
|
setTimeout(splitLongTooltips);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2020-02-02 14:02:25 +00:00
|
|
|
// TODO: add max version to re-enable once crbug.com/996859 is resolved
|
|
|
|
if (!FIREFOX && CHROME >= 3809) {
|
|
|
|
const dropboxOption = $('option[value="dropbox"]');
|
|
|
|
dropboxOption.disabled = true;
|
|
|
|
dropboxOption.setAttribute('title', t('hostDisabled'));
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:21:36 +00:00
|
|
|
if (CHROME_HAS_BORDER_BUG) {
|
|
|
|
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)
|
|
|
|
if (!FIREFOX && !OPERA && CHROME < 3343) {
|
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');
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.onExtension(msg => {
|
2018-04-12 18:02:34 +00:00
|
|
|
if (msg.method === 'optionsCustomizeHotkeys') {
|
|
|
|
customizeHotkeys();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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')
|
2018-11-07 06:09:29 +00:00
|
|
|
.filter(input => input.id in prefs.defaults)
|
2017-04-04 17:21:03 +00:00
|
|
|
.forEach(input => prefs.reset(input.id));
|
|
|
|
break;
|
2017-12-12 00:27:28 +00:00
|
|
|
|
|
|
|
case 'note': {
|
2018-01-24 20:16:24 +00:00
|
|
|
e.preventDefault();
|
|
|
|
messageBox({
|
|
|
|
className: 'note',
|
|
|
|
contents: target.title,
|
|
|
|
buttons: [t('confirmClose')],
|
|
|
|
});
|
2017-12-12 00:27:28 +00:00
|
|
|
}
|
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
|
|
|
|
(() => {
|
|
|
|
const cloud = document.querySelector('.sync-options .cloud-name');
|
|
|
|
const connectButton = document.querySelector('.sync-options .connect');
|
|
|
|
const disconnectButton = document.querySelector('.sync-options .disconnect');
|
|
|
|
const syncButton = document.querySelector('.sync-options .sync-now');
|
|
|
|
const statusText = document.querySelector('.sync-options .sync-status');
|
|
|
|
const loginButton = document.querySelector('.sync-options .sync-login');
|
|
|
|
|
|
|
|
let status = {};
|
|
|
|
|
|
|
|
msg.onExtension(e => {
|
|
|
|
if (e.method === 'syncStatusUpdate') {
|
|
|
|
status = e.status;
|
|
|
|
updateButtons();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
API.getSyncStatus()
|
|
|
|
.then(_status => {
|
|
|
|
status = _status;
|
|
|
|
updateButtons();
|
|
|
|
});
|
|
|
|
|
|
|
|
function validClick(e) {
|
|
|
|
return e.button === 0 && !e.ctrl && !e.alt && !e.shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
cloud.addEventListener('change', updateButtons);
|
|
|
|
|
|
|
|
function updateButtons() {
|
|
|
|
if (status.currentDriveName) {
|
|
|
|
cloud.value = status.currentDriveName;
|
|
|
|
}
|
|
|
|
cloud.disabled = status.state !== 'disconnected';
|
|
|
|
connectButton.disabled = status.state !== 'disconnected' || cloud.value === 'none';
|
|
|
|
disconnectButton.disabled = status.state !== 'connected' || status.syncing;
|
|
|
|
syncButton.disabled = status.state !== 'connected' || status.syncing;
|
|
|
|
statusText.textContent = getStatusText();
|
|
|
|
loginButton.style.display = status.state === 'connected' && !status.login ? '' : 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStatusText() {
|
|
|
|
if (status.syncing) {
|
|
|
|
if (status.progress) {
|
|
|
|
const {phase, loaded, total} = status.progress;
|
|
|
|
return chrome.i18n.getMessage(`optionsSyncStatus${capitalize(phase)}`, [loaded + 1, total]) ||
|
|
|
|
`${phase} ${loaded} / ${total}`;
|
|
|
|
}
|
|
|
|
return chrome.i18n.getMessage('optionsSyncStatusSyncing') || 'syncing';
|
|
|
|
}
|
|
|
|
if ((status.state === 'connected' || status.state === 'disconnected') && status.errorMessage) {
|
|
|
|
return status.errorMessage;
|
|
|
|
}
|
|
|
|
return chrome.i18n.getMessage(`optionsSyncStatus${capitalize(status.state)}`) || status.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
connectButton.addEventListener('click', e => {
|
|
|
|
if (validClick(e)) {
|
|
|
|
API.syncStart(cloud.value).catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
disconnectButton.addEventListener('click', e => {
|
|
|
|
if (validClick(e)) {
|
|
|
|
API.syncStop().catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
syncButton.addEventListener('click', e => {
|
|
|
|
if (validClick(e)) {
|
|
|
|
API.syncNow().catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
loginButton.addEventListener('click', e => {
|
|
|
|
if (validClick(e)) {
|
|
|
|
API.syncLogin().catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2019-01-02 02:10:38 +00:00
|
|
|
API.updateCheckBulk({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
|
|
|
|
|
|
|
function setupRadioButtons() {
|
|
|
|
const sets = {};
|
2017-07-16 16:49:31 +00:00
|
|
|
const onChange = function () {
|
2017-06-28 10:49:04 +00:00
|
|
|
const newValue = sets[this.name].indexOf(this);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (newValue >= 0 && prefs.get(this.name) !== newValue) {
|
2017-06-28 10:49:04 +00:00
|
|
|
prefs.set(this.name, newValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// group all radio-inputs by name="prefName" attribute
|
|
|
|
for (const el of $$('input[type="radio"][name]')) {
|
|
|
|
(sets[el.name] = sets[el.name] || []).push(el);
|
|
|
|
el.addEventListener('change', onChange);
|
|
|
|
}
|
|
|
|
// select the input corresponding to the actual pref value
|
|
|
|
for (const name in sets) {
|
|
|
|
sets[name][prefs.get(name)].checked = true;
|
|
|
|
}
|
|
|
|
// listen to pref changes and update the values
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(Object.keys(sets), (key, value) => {
|
2017-06-28 10:49:04 +00:00
|
|
|
sets[key][value].checked = true;
|
2017-09-03 17:06:20 +00:00
|
|
|
});
|
2017-06-28 10:49:04 +00:00
|
|
|
}
|
2017-12-12 00:27:28 +00:00
|
|
|
|
|
|
|
function splitLongTooltips() {
|
|
|
|
for (const el of $$('[title]')) {
|
|
|
|
if (el.title.length < 50) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const newTitle = el.title
|
|
|
|
.split('\n')
|
|
|
|
.map(s => s.replace(/([^.][.。?!]|.{50,60},)\s+/g, '$1\n'))
|
|
|
|
.map(s => s.replace(/(.{50,80}(?=.{40,}))\s+/g, '$1\n'))
|
|
|
|
.join('\n');
|
|
|
|
if (newTitle !== el.title) {
|
|
|
|
el.title = newTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
messageBox({
|
|
|
|
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
|
|
|
|
|
|
|
window.onkeydown = event => {
|
|
|
|
if (event.keyCode === 27) {
|
|
|
|
top.dispatchEvent(new CustomEvent('closeOptions'));
|
|
|
|
}
|
|
|
|
};
|