-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
64 lines (58 loc) · 1.53 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict'
var form = document.forms[0]
form.addEventListener('change', function() {
var interval = +form.interval.value,
badge = form.badge.checked,
delay = +form.delay.value
chrome.storage.local.set({
interval,
sound: form.sound.checked,
badge,
delay,
})
chrome.alarms.create('notify', {
delayInMinutes: interval,
periodInMinutes: interval,
})
chrome.alarms.get('notify', function(details) {
update(details.scheduledTime, badge)
})
if (badge)
chrome.alarms.create('badge', {
delayInMinutes: 10,
periodInMinutes: 10,
})
else {
chrome.alarms.clear('badge')
chrome.action.setBadgeText({ text: '' })
}
})
function load() {
chrome.storage.local.get(
{ interval: 20, sound: true, badge: false, delay: 20 },
function(data) {
form.interval.value = data.interval
form.delay.value = data.delay
form.sound.checked = data.sound
form.badge.checked = data.badge
}
)
chrome.alarms.get('notify', function(details) {
update(details.scheduledTime)
})
}
function update(time, badge) {
const date = new Date(time)
document.getElementById('notification').innerText = date.toLocaleTimeString()
if (badge) {
const mins = Math.round((time - Date.now()) / 1000 / 60)
const string = mins + 'min'
chrome.action.setTitle({ title: 'Next alarm in ' + string })
chrome.action.setBadgeText({ text: string })
chrome.action.setBadgeBackgroundColor({ color: '#E0E0E0' }) // #BDBDBD
} else
chrome.action.setTitle({
title: 'Next alarm at' + date.toLocaleTimeString(),
})
}
window.addEventListener('load', load)