stylus/options/index.js
tophf 5c8d1950a7 Isolate storage.js in background context
To prevent cross-page leaks we need to create/copy prefs and cachedStyles inside the background page context.

* storage.js is now used only in the background page

* messaging.js now contains less bg-specific methods and more common methods. Added saveStyleSafe, deleteStyleSafe which automatically invoke onRuntimeMessage of the current page or just handleUpdate/handleDelete when notify:false

* prefs.js with 'prefs' for background and UI pages: separate objects because a UI page may load before the background page and it can read prefs from localStorage/sync/defaults
2017-04-18 12:46:34 +03:00

80 lines
1.7 KiB
JavaScript

'use strict';
setupLivePrefs([
'show-badge',
'popup.stylesFirst',
'badgeNormal',
'badgeDisabled',
'popupWidth',
'updateInterval',
]);
enforceInputRange($('#popupWidth'));
// overwrite the default URL if browser is Opera
$('[data-cmd="open-keyboard"]').href = URLS.configureCommands;
// actions
document.onclick = e => {
const cmd = e.target.dataset.cmd;
let total = 0;
let updated = 0;
function showProgress() {
$('#update-counter').textContent = `${updated} / ${total}`;
}
function done(target) {
target.disabled = false;
window.setTimeout(() => {
$('#update-counter').textContent = '';
}, 750);
}
function check() {
BG.update.perform((cmd, value) => {
switch (cmd) {
case 'count':
total = value;
if (!total) {
done(e.target);
}
break;
case 'single-updated':
case 'single-skipped':
updated++;
if (total && updated === total) {
done(e.target);
}
break;
}
showProgress();
});
// notify the automatic updater to reset the next automatic update accordingly
chrome.runtime.sendMessage({
method: 'resetInterval'
});
}
switch (cmd) {
case 'open-manage':
openURL({url: '/manage.html'});
break;
case 'check-updates':
e.target.disabled = true;
check();
break;
case 'open-keyboard':
openURL({url: e.target.href});
e.preventDefault();
break;
case 'reset':
$$('input')
.filter(input => input.id in prefs.readOnlyValues)
.forEach(input => prefs.reset(input.id));
break;
}
};