2017-03-16 13:36:33 +00:00
|
|
|
/* globals configureCommands */
|
2017-02-14 15:35:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function restore () {
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
|
|
|
document.getElementById('badgeDisabled').value = bg.prefs.get('badgeDisabled');
|
|
|
|
document.getElementById('badgeNormal').value = bg.prefs.get('badgeNormal');
|
2017-03-08 13:11:26 +00:00
|
|
|
document.getElementById('popupWidth').value = localStorage.getItem('popupWidth') || '246';
|
2017-02-14 15:35:53 +00:00
|
|
|
document.getElementById('updateInterval').value = bg.prefs.get('updateInterval');
|
2017-03-21 01:32:38 +00:00
|
|
|
enforceValueRange('popupWidth');
|
2017-02-14 15:35:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function save () {
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
|
|
|
bg.prefs.set('badgeDisabled', document.getElementById('badgeDisabled').value);
|
|
|
|
bg.prefs.set('badgeNormal', document.getElementById('badgeNormal').value);
|
2017-03-21 01:32:38 +00:00
|
|
|
localStorage.setItem('popupWidth', enforceValueRange('popupWidth'));
|
2017-02-14 15:35:53 +00:00
|
|
|
bg.prefs.set(
|
|
|
|
'updateInterval',
|
|
|
|
Math.max(0, +document.getElementById('updateInterval').value)
|
|
|
|
);
|
|
|
|
// display notification
|
|
|
|
let status = document.getElementById('status');
|
|
|
|
status.textContent = 'Options saved.';
|
|
|
|
setTimeout(() => status.textContent = '', 750);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function enforceValueRange(id) {
|
|
|
|
let element = document.getElementById(id);
|
|
|
|
let value = Number(element.value);
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
|
|
|
if (value < min || value > max) {
|
|
|
|
value = Math.max(min, Math.min(max, value));
|
|
|
|
element.value = value;
|
|
|
|
}
|
|
|
|
element.onchange = element.onchange || (() => enforceValueRange(id));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:35:53 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', restore);
|
|
|
|
document.getElementById('save').addEventListener('click', save);
|
|
|
|
|
|
|
|
// actions
|
|
|
|
document.addEventListener('click', e => {
|
|
|
|
let cmd = e.target.dataset.cmd;
|
|
|
|
let total = 0, updated = 0;
|
|
|
|
|
|
|
|
function update () {
|
|
|
|
document.getElementById('update-counter').textContent = `${updated}/${total}`;
|
|
|
|
}
|
|
|
|
function done (target) {
|
|
|
|
target.disabled = false;
|
|
|
|
window.setTimeout(() => {
|
|
|
|
document.getElementById('update-counter').textContent = '';
|
|
|
|
}, 750);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd === 'open-manage') {
|
|
|
|
chrome.tabs.query({
|
|
|
|
url: chrome.runtime.getURL('manage.html')
|
|
|
|
}, tabs => {
|
|
|
|
if (tabs.length) {
|
|
|
|
chrome.tabs.update(tabs[0].id, {
|
|
|
|
active: true,
|
|
|
|
}, () => {
|
|
|
|
chrome.windows.update(tabs[0].windowId, {
|
|
|
|
focused: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chrome.tabs.create({
|
|
|
|
url: chrome.runtime.getURL('manage.html')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (cmd === 'check-updates') {
|
|
|
|
e.target.disabled = true;
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
|
|
|
bg.update.perform((cmd, value) => {
|
|
|
|
if (cmd === 'count') {
|
|
|
|
total = value;
|
|
|
|
if (!total) {
|
|
|
|
done(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd === 'single-updated' || cmd === 'single-skipped') {
|
|
|
|
updated += 1;
|
|
|
|
if (total && updated === total) {
|
|
|
|
done(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// notify the automatic updater to reset the next automatic update accordingly
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'resetInterval'
|
|
|
|
});
|
|
|
|
}
|
2017-02-15 05:48:47 +00:00
|
|
|
else if (cmd === 'open-keyboard') {
|
2017-03-16 13:36:33 +00:00
|
|
|
configureCommands.open();
|
2017-02-15 05:48:47 +00:00
|
|
|
}
|
2017-02-14 15:35:53 +00:00
|
|
|
});
|
2017-03-16 13:36:33 +00:00
|
|
|
// overwrite the default URL if browser is Opera
|
|
|
|
document.querySelector('[data-cmd="open-keyboard"]').textContent =
|
|
|
|
configureCommands.url;
|