forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
403 lines (359 loc) · 10.5 KB
/
popup.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var blankClockImage;
var blankClockAnim1Image;
var blankClockAnim2Image;
var animationTimer;
var currentClockImage;
var port;
function updateEnabledStatus(alarm) {
var enabled = $('a' + alarm + '_on').checked;
$('a' + alarm + '_tt').disabled = !enabled;
$('a' + alarm + '_ampm').disabled = !enabled;
var valid = true;
try {
var tt = $('a' + alarm + '_tt').value;
var ampm = $('a' + alarm + '_ampm').selectedIndex;
parseTime(tt, ampm);
} catch (x) {
valid = false;
}
if (valid) {
$('a' + alarm + '_wrap').removeAttribute('aria-invalid');
} else {
$('a' + alarm + '_wrap').setAttribute('aria-invalid', 'true');
}
if (enabled) {
$('a' + alarm + '_wrap').classList.remove('disabled');
} else {
$('a' + alarm + '_wrap').classList.add('disabled');
}
}
function loadAllImages() {
var loadCount = 0;
var img = new Image();
img.onload = function() {
blankClockImage = img;
currentClockImage = blankClockImage;
drawClock();
};
img.src = 'blank-clock-150.png';
// These will finish loading before they're needed, no need
// for an onload handler.
blankClockAnim1Image = new Image();
blankClockAnim1Image.src = 'blank-clock-ring1-150.png';
blankClockAnim2Image = new Image();
blankClockAnim2Image.src = 'blank-clock-ring2-150.png';
}
function drawClock(hh, mm, ss) {
if (hh == undefined || mm == undefined) {
var d = new Date();
hh = d.getHours();
mm = d.getMinutes();
ss = d.getSeconds() + 0.001 * d.getMilliseconds();
}
if (!currentClockImage) {
loadAllImages();
return;
}
var ctx = $('clock').getContext('2d');
ctx.drawImage(currentClockImage, 0, 0);
// Move the hour by the fraction of the minute
hh = (hh % 12) + (mm / 60);
// Move the minute by the fraction of the second
mm += (ss / 60);
var hourAngle = Math.PI * hh / 6;
var hourX = Math.sin(hourAngle);
var hourY = -Math.cos(hourAngle);
var minAngle = Math.PI * mm / 30;
var minX = Math.sin(minAngle);
var minY = -Math.cos(minAngle);
var secAngle = Math.PI * ss / 30;
var secX = Math.sin(secAngle);
var secY = -Math.cos(secAngle);
var cx = 75;
var cy = 77;
ctx.lineWidth = 5;
ctx.strokeStyle = '#ffffff';
ctx.globalAlpha = 0.5;
ctx.beginPath();
ctx.moveTo(cx - 4 * hourX, cy - 4 * hourY);
ctx.lineTo(cx + 20 * hourX, cy + 20 * hourY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(cx - 8 * minX, cy - 8 * minY);
ctx.lineTo(cx + 35 * minX, cy + 33 * minY);
ctx.stroke();
ctx.lineWidth = 3;
ctx.strokeStyle = '#696969';
ctx.globalAlpha = 1.0;
ctx.beginPath();
ctx.moveTo(cx - 4 * hourX, cy - 4 * hourY);
ctx.lineTo(cx + 20 * hourX, cy + 20 * hourY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(cx - 8 * minX, cy - 8 * minY);
ctx.lineTo(cx + 35 * minX, cy + 33 * minY);
ctx.stroke();
ctx.lineWidth = 1;
ctx.strokeStyle = '#990000';
ctx.globalAlpha = 1.0;
ctx.beginPath();
ctx.moveTo(cx - 4 * secX, cy - 4 * secY);
ctx.lineTo(cx + 40 * secX, cy + 40 * secY);
ctx.stroke();
}
function updateCurrentTime() {
var now = new Date();
var hh = now.getHours();
var mm = now.getMinutes();
var ss = now.getSeconds();
var str = '';
if (hh % 12 == 0) {
str += '12';
} else {
str += (hh % 12);
}
str += ':';
if (mm >= 10) {
str += mm;
} else {
str += '0' + mm;
}
str += ':';
if (ss >= 10) {
str += ss;
} else {
str += '0' + ss;
}
if (hh >= 12) {
str += " PM";
} else {
str += " AM";
}
$('current_time').innerText = str;
}
// Override from common.js
window.stopAlarmAnimation = function() {
window.clearTimeout(animationTimer);
currentClockImage = blankClockImage;
drawClock();
isAnimating = false;
};
// Override from common.js
window.displayAlarmAnimation = function() {
isAnimating = true;
var rings = 100;
function ring() {
if (rings == 0) {
stopAlarmAnimation();
return;
}
currentClockImage = (rings % 2 == 0)?
blankClockAnim1Image:
blankClockAnim2Image;
drawClock();
rings--;
animationTimer = window.setTimeout(ring, 50);
}
ring();
};
function addOutlineStyleListeners() {
document.addEventListener('click', function(evt) {
document.body.classList.add('nooutline');
return true;
}, true);
document.addEventListener('keydown', function(evt) {
document.body.classList.remove('nooutline');
return true;
}, true);
}
function load() {
try {
port = chrome.runtime.connect();
port.onMessage.addListener(function(msg) {
if (msg.cmd == 'anim') {
displayAlarmAnimation();
}
});
} catch (e) {
}
addOutlineStyleListeners();
stopAll();
drawClock();
setInterval(drawClock, 100);
updateCurrentTime();
setInterval(updateCurrentTime, 250);
function updateTime(timeElement) {
if (!parseTime(timeElement.value)) {
return false;
}
timeElement.valueAsNumber =
timeElement.valueAsNumber % (12 * 60 * 60 * 1000);
if (timeElement.valueAsNumber < (1 * 60 * 60 * 1000))
timeElement.valueAsNumber += (12 * 60 * 60 * 1000);
return true;
}
$('clock').addEventListener('click', function(evt) {
if (isPlaying || isSpeaking || isAnimating) {
stopAll();
} else {
ringAlarmWithCurrentTime();
}
}, false);
$('clock').addEventListener('keydown', function(evt) {
if (evt.keyCode == 13 || evt.keyCode == 32) {
if (isPlaying || isSpeaking || isAnimating) {
stopAll();
} else {
ringAlarmWithCurrentTime();
}
}
}, false);
// Alarm 1
var a1_tt = localStorage['a1_tt'] || DEFAULT_A1_TT;
$('a1_tt').value = a1_tt;
$('a1_tt').addEventListener('input', function(evt) {
updateEnabledStatus(1);
if (!updateTime($('a1_tt'))) {
evt.stopPropagation();
return false;
}
localStorage['a1_tt'] = $('a1_tt').value;
updateEnabledStatus(1);
return true;
}, false);
$('a1_tt').addEventListener('change', function(evt) {
if ($('a1_tt').value.length == 4 &&
parseTime('0' + $('a1_tt').value)) {
$('a1_tt').value = '0' + $('a1_tt').value;
}
if (!updateTime($('a1_tt'))) {
evt.stopPropagation();
return false;
}
localStorage['a1_tt'] = $('a1_tt').value;
updateEnabledStatus(1);
return true;
}, false);
var a1_on = (localStorage['a1_on'] == 'true');
$('a1_on').checked = a1_on;
$('a1_on').addEventListener('change', function(evt) {
window.setTimeout(function() {
localStorage['a1_on'] = $('a1_on').checked;
updateEnabledStatus(1);
}, 0);
}, false);
var a1_ampm = localStorage['a1_ampm'] || DEFAULT_A1_AMPM;
$('a1_ampm').selectedIndex = a1_ampm;
$('a1_ampm').addEventListener('change', function(evt) {
localStorage['a1_ampm'] = $('a1_ampm').selectedIndex;
}, false);
updateEnabledStatus(1);
// Alarm 2
var a2_tt = localStorage['a2_tt'] || DEFAULT_A2_TT;
$('a2_tt').value = a2_tt;
$('a2_tt').addEventListener('input', function(evt) {
updateEnabledStatus(2);
if (!updateTime($('a2_tt'))) {
evt.stopPropagation();
return false;
}
localStorage['a2_tt'] = $('a2_tt').value;
updateEnabledStatus(2);
return true;
}, false);
$('a2_tt').addEventListener('change', function(evt) {
if ($('a2_tt').value.length == 4 &&
parseTime('0' + $('a2_tt').value)) {
$('a2_tt').value = '0' + $('a2_tt').value;
}
if (!updateTime($('a2_tt'))) {
evt.stopPropagation();
return false;
}
localStorage['a2_tt'] = $('a2_tt').value;
updateEnabledStatus(2);
return true;
}, false);
var a2_on = (localStorage['a2_on'] == 'true');
$('a2_on').checked = a2_on;
$('a2_on').addEventListener('change', function(evt) {
window.setTimeout(function() {
localStorage['a2_on'] = $('a2_on').checked;
updateEnabledStatus(2);
}, 0);
}, false);
var a2_ampm = localStorage['a2_ampm'] || DEFAULT_A2_AMPM;
$('a2_ampm').selectedIndex = a2_ampm;
$('a2_ampm').addEventListener('change', function(evt) {
localStorage['a2_ampm'] = $('a2_ampm').selectedIndex;
}, false);
updateEnabledStatus(2);
// Phrase
var phrase = localStorage['phrase'] || DEFAULT_PHRASE;
$('phrase').value = phrase;
$('phrase').addEventListener('change', function(evt) {
localStorage['phrase'] = $('phrase').value;
}, false);
// Speech parameters
var rateElement = $('rate');
var volumeElement = $('volume');
var rate = localStorage['rate'] || DEFAULT_RATE;
var volume = localStorage['volume'] || DEFAULT_VOLUME;
rateElement.value = rate;
volumeElement.value = volume;
function listener(evt) {
rate = rateElement.value;
localStorage['rate'] = rate;
volume = volumeElement.value;
localStorage['volume'] = volume;
}
rateElement.addEventListener('keyup', listener, false);
volumeElement.addEventListener('keyup', listener, false);
rateElement.addEventListener('mouseup', listener, false);
volumeElement.addEventListener('mouseup', listener, false);
var sound = $('sound');
var currentSound = localStorage['sound'] || DEFAULT_SOUND;
for (var i = 0; i < sound.options.length; i++) {
if (sound.options[i].value == currentSound) {
sound.selectedIndex = i;
break;
}
}
localStorage['sound'] = sound.options[sound.selectedIndex].value;
sound.addEventListener('change', function() {
localStorage['sound'] = sound.options[sound.selectedIndex].value;
}, false);
var playSoundButton = $('playsound');
playSoundButton.addEventListener('click', function(evt) {
playSound(false);
});
var playSpeechButton = $('playspeech');
playSpeechButton.addEventListener('click', function(evt) {
speakPhraseWithCurrentTime();
});
var voice = $('voice');
var voiceArray = [];
if (chrome && chrome.tts) {
chrome.tts.getVoices(function(va) {
voiceArray = va;
for (var i = 0; i < voiceArray.length; i++) {
var opt = document.createElement('option');
var name = voiceArray[i].voiceName;
if (name == localStorage['voice']) {
opt.setAttribute('selected', '');
}
opt.setAttribute('value', name);
opt.innerText = voiceArray[i].voiceName;
voice.appendChild(opt);
}
});
}
voice.addEventListener('change', function() {
var i = voice.selectedIndex;
localStorage['voice'] = voiceArray[i].voiceName;
}, false);
}
document.addEventListener('DOMContentLoaded', load);