dom.js in optionsUI + reuse openURL to open/switch to URL

This commit is contained in:
tophf 2017-03-25 09:30:34 +03:00
parent 4936426fa3
commit 54bb7cdd4e
2 changed files with 35 additions and 40 deletions

View File

@ -57,6 +57,7 @@
</div> </div>
<script src="/messaging.js"></script> <script src="/messaging.js"></script>
<script src="/dom.js"></script>
<script src="index.js"></script> <script src="index.js"></script>
</body> </body>
</html> </html>

View File

@ -1,32 +1,35 @@
/* globals configureCommands */ /* globals configureCommands */
'use strict'; 'use strict';
function restore () { function restore () {
chrome.runtime.getBackgroundPage(bg => { chrome.runtime.getBackgroundPage(bg => {
document.getElementById('badgeDisabled').value = bg.prefs.get('badgeDisabled'); $('#badgeDisabled').value = bg.prefs.get('badgeDisabled');
document.getElementById('badgeNormal').value = bg.prefs.get('badgeNormal'); $('#badgeNormal').value = bg.prefs.get('badgeNormal');
document.getElementById('popupWidth').value = localStorage.getItem('popupWidth') || '246'; $('#popupWidth').value = localStorage.getItem('popupWidth') || '246';
document.getElementById('updateInterval').value = bg.prefs.get('updateInterval'); $('#updateInterval').value = bg.prefs.get('updateInterval');
enforceValueRange('popupWidth'); enforceValueRange('popupWidth');
}); });
} }
function save () { function save () {
chrome.runtime.getBackgroundPage(bg => { chrome.runtime.getBackgroundPage(bg => {
bg.prefs.set('badgeDisabled', document.getElementById('badgeDisabled').value); bg.prefs.set('badgeDisabled', $('#badgeDisabled').value);
bg.prefs.set('badgeNormal', document.getElementById('badgeNormal').value); bg.prefs.set('badgeNormal', $('#badgeNormal').value);
localStorage.setItem('popupWidth', enforceValueRange('popupWidth')); localStorage.setItem('popupWidth', enforceValueRange('popupWidth'));
bg.prefs.set( bg.prefs.set(
'updateInterval', 'updateInterval',
Math.max(0, +document.getElementById('updateInterval').value) Math.max(0, +$('#updateInterval').value)
); );
// display notification // display notification
let status = document.getElementById('status'); let status = $('#status');
status.textContent = 'Options saved.'; status.textContent = 'Options saved.';
setTimeout(() => status.textContent = '', 750); setTimeout(() => status.textContent = '', 750);
}); });
} }
function enforceValueRange(id) { function enforceValueRange(id) {
let element = document.getElementById(id); let element = document.getElementById(id);
let value = Number(element.value); let value = Number(element.value);
@ -40,45 +43,36 @@ function enforceValueRange(id) {
return value; return value;
} }
document.addEventListener('DOMContentLoaded', restore);
document.getElementById('save').addEventListener('click', save); onDOMready().then(restore);
$('#save').onclick = save;
// overwrite the default URL if browser is Opera
$('[data-cmd="open-keyboard"]').textContent =
configureCommands.url;
// actions // actions
document.addEventListener('click', e => { document.onclick = e => {
let cmd = e.target.dataset.cmd; let cmd = e.target.dataset.cmd;
let total = 0, updated = 0; let total = 0, updated = 0;
function update () { function update () {
document.getElementById('update-counter').textContent = `${updated}/${total}`; $('#update-counter').textContent = `${updated}/${total}`;
} }
function done (target) { function done (target) {
target.disabled = false; target.disabled = false;
window.setTimeout(() => { window.setTimeout(() => {
document.getElementById('update-counter').textContent = ''; $('#update-counter').textContent = '';
}, 750); }, 750);
} }
if (cmd === 'open-manage') { switch (cmd) {
chrome.tabs.query({
url: chrome.runtime.getURL('manage.html') case 'open-manage':
}, tabs => { openURL({url: '/manage.html'});
if (tabs.length) { break;
chrome.tabs.update(tabs[0].id, {
active: true, case'check-updates':
}, () => {
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; e.target.disabled = true;
chrome.runtime.getBackgroundPage(bg => { chrome.runtime.getBackgroundPage(bg => {
bg.update.perform((cmd, value) => { bg.update.perform((cmd, value) => {
@ -101,11 +95,11 @@ document.addEventListener('click', e => {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
method: 'resetInterval' method: 'resetInterval'
}); });
} break;
else if (cmd === 'open-keyboard') {
case 'open-keyboard':
configureCommands.open(); configureCommands.open();
break;
} }
}); };
// overwrite the default URL if browser is Opera
document.querySelector('[data-cmd="open-keyboard"]').textContent =
configureCommands.url;