Skip to content

Commit

Permalink
Add custom checking interval (fixes #8), added option to disable desk…
Browse files Browse the repository at this point in the history
…top notifications (fixes #2)
  • Loading branch information
umpirsky committed Feb 25, 2018
1 parent b48646c commit 89123f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ var count = 0;

function update() {
browser.storage.local.get({
username: ''
username: '',
showNotifications: false,
checkingInterval: 60
})
.then((options) => {
setTimeout(update, 1000 * Math.max(10, options.checkingInterval));

if (!options.username) {
return;
}
Expand Down Expand Up @@ -47,12 +51,14 @@ function update() {
return;
}

browser.notifications.create({
type: 'basic',
iconUrl: browser.extension.getURL('icons/128/pawn_color.png'),
title: 'Your Turn!',
message: 1 === count ? "It's your turn to move." : "It's your turn to move, " + count + ' games are waiting.'
});
if (options.showNotifications) {
browser.notifications.create({
type: 'basic',
iconUrl: browser.extension.getURL('icons/128/pawn_color.png'),
title: 'Your Turn!',
message: 1 === count ? "It's your turn to move." : "It's your turn to move, " + count + ' games are waiting.'
});
}
})
.catch((error) => {
browser.browserAction.setTitle({
Expand All @@ -63,7 +69,7 @@ function update() {
});
}

setInterval(update, 1000 * 60);
update();

browser.browserAction.onClicked.addListener((e) => {
browser.storage.local.get({
Expand Down
8 changes: 8 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
<label for="access-token">Chess.com username</label>
<input type="text" name="username" id="username">
</p>
<p>
<label for="checking_interval">Checking interval</label>
<input type="number" name="checking_interval" id="checking_interval"> seconds
</p>
<p>
<label for="show_notifications">Show desktop notifications</label>
<input type="checkbox" name="show_notifications" id="show_notifications">
</p>
<button type="submit">Save</button>
</fieldset>
</form>
Expand Down
10 changes: 8 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
document.addEventListener('DOMContentLoaded', () => {
browser.storage.local
.get({
username: ''
username: '',
showNotifications: true,
checkingInterval: 60,
})
.then((options) => {
document.querySelector('#username').value = options.username;
document.querySelector('#checking_interval').value = options.checkingInterval;
document.querySelector('#show_notifications').checked = options.showNotifications;
});
});
document.querySelector('form')
.addEventListener('submit', (e) => {
e.preventDefault();

browser.storage.local.set({
username: e.target['username'].value
username: e.target['username'].value,
checkingInterval: e.target['checking_interval'].value,
showNotifications: e.target['show_notifications'].checked
});

document.getElementById('status').style.display = 'block';
Expand Down

0 comments on commit 89123f3

Please sign in to comment.