-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
173 lines (154 loc) · 5.81 KB
/
options.js
File metadata and controls
173 lines (154 loc) · 5.81 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//Set texts in local language
const objects = document.getElementsByTagName('*')
for(let i = 0; i < objects.length; i++) {
if (objects[i].hasAttribute('data-text')) {
const textKey = objects[i].getAttribute('data-text')
objects[i].innerText = chrome.i18n.getMessage(textKey)
}
}
//Notify user when limit is reached
const notificationSetter = document.getElementById('notification_setter')
const limit = document.getElementById('limit')
const limitSwitch = document.getElementById('limit_switch')
const onOffIndicator = document.getElementById('on_off_label')
chrome.storage.local.get('notification', (counter) => {
if (counter.notification) {
notificationSetter.classList.remove('inactive')
limitSwitch.checked = true
onOffIndicator.innerHTML = 'on'
}
})
chrome.storage.local.get('limit', (counter) => {
limit.value = counter.limit
})
limitSwitch.addEventListener('change', (e) => {
if (e.target.checked) {
chrome.permissions.contains({
permissions: ['notifications']
}, (result) => {
if (result) {
chrome.storage.local.set({'notification': true})
notificationSetter.classList.remove('inactive')
onOffIndicator.innerHTML = 'on'
} else {
chrome.permissions.request({permissions: ['notifications']}, (granted) => {
if (granted) {
chrome.storage.local.set({'notification': true})
notificationSetter.classList.remove('inactive')
onOffIndicator.innerHTML = 'on'
} else {
e.target.checked = false
chrome.storage.local.set({'notification': false})
notificationSetter.classList.add('inactive')
onOffIndicator.innerHTML = 'off'
}
})
}
})
} else {
chrome.storage.local.set({'notification': false})
notificationSetter.classList.add('inactive')
onOffIndicator.innerHTML = 'off'
}
})
limit.addEventListener('change', () => {
const limitValue = Math.trunc(limit.value * 10) / 10
chrome.storage.local.set({'limit': limitValue})
limit.value = limitValue
})
//Set counter step by
const step = document.getElementById('step')
chrome.storage.local.get('step', (counter) => {
step.value = counter.step
})
step.addEventListener('change', (e) => {
const stepValue = Math.trunc(step.value * 10) / 10
chrome.storage.local.set({'step': stepValue})
step.value = stepValue
})
//Set new counter total
const counterTotal = document.getElementById('total')
document.getElementById('set_new_total').addEventListener('click', (e) => {
const totalValue = Math.trunc(counterTotal.value * 10) / 10
chrome.storage.local.set({'total': totalValue}, () => {
chrome.action.setBadgeText({'text': totalValue.toString()})
})
counterTotal.value = totalValue
})
//Sound
const soundSetter = document.getElementById('sound_setter')
const soundSwitch = document.getElementById('sound_switch')
const soundOnOffIndicator = document.getElementById('on_off_label_sound')
const soundVolumeContainer = document.getElementById('sound_volume_container')
const volumeSlider = document.getElementById('sound_volume_slider')
chrome.storage.local.get(['sound', 'volume'], (counter) => {
if (counter.sound) {
soundSetter.classList.remove('inactive')
soundSwitch.checked = true
soundOnOffIndicator.innerHTML = 'on'
soundVolumeContainer.style.visibility = 'visible'
}
volumeSlider.value = counter.volume ? counter.volume : 0.5
})
soundSwitch.addEventListener('change', (e) => {
if (e.target.checked) {
chrome.storage.local.set({'sound': true})
soundSetter.classList.remove('inactive')
soundOnOffIndicator.innerHTML = 'on'
soundVolumeContainer.style.visibility = 'visible'
} else {
chrome.storage.local.set({'sound': false})
soundSetter.classList.add('inactive')
soundOnOffIndicator.innerHTML = 'off'
soundVolumeContainer.style.visibility = 'hidden'
}
})
volumeSlider.addEventListener('input', (e) => {
const newVolume = parseFloat(volumeSlider.value)
chrome.storage.local.set({'volume': newVolume})
})
//Timestamp
const timeStampSetter = document.getElementById('timestamp_setter')
const timeStampSwitch = document.getElementById('timestamp_switch')
const timeStampOffIndicator = document.getElementById('on_off_label_timestamp')
chrome.storage.local.get(['showTimestamp'], (counter) => {
if (counter.showTimestamp) {
timeStampSetter.classList.remove('inactive')
timeStampSwitch.checked = true
timeStampOffIndicator.innerHTML = 'on'
}
})
timeStampSwitch.addEventListener('change', (e) => {
if (e.target.checked) {
chrome.storage.local.set({'showTimestamp': true})
timeStampSetter.classList.remove('inactive')
timeStampOffIndicator.innerHTML = 'on'
} else {
chrome.storage.local.set({'showTimestamp': false})
timeStampSetter.classList.add('inactive')
timeStampOffIndicator.innerHTML = 'off'
}
})
//Open clicks chronology button
document.getElementById('chronology_button').addEventListener('click', () => { chrome.tabs.create({ url: chrome.runtime.getURL('Chronology/chronology.html') }) })
//Reset button
document.getElementById('reset').addEventListener('click', () => {
chrome.storage.local.set({'total': 0}, () => {
chrome.action.setBadgeText({'text': '0'})
chrome.permissions.contains({permissions: ['notifications']}, (result) => {
if (result) {
chrome.notifications.getAll((items) => {
if (items) {
for (let key in items) {
chrome.notifications.clear(key)
}
}
})
}
})
})
})
//Set keyboard shortcut button
document.getElementById('set_keyboard_shortcut_button').addEventListener('click', () => { chrome.tabs.create({ url: 'chrome://extensions/shortcuts' }) })
//Close page button
document.getElementById('close').addEventListener('click', () => { window.close() })