Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/common/extensions/docs/examples/extensions/talking_alarm_clock/background.js

Issue 8114011: Add sample extensions that use the text-to-speech (TTS) API. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 var a1Timer = null;
2 var a2Timer = null;
3 var port = null;
4 var iconFlashTimer = null;
5
6 var HOUR_MS = 1000 * 60 * 60;
7
8 // Override from common.js
9 window.stopFlashingIcon = function() {
10 window.clearTimeout(iconFlashTimer);
11 chrome.browserAction.setIcon({'path': 'clock-19.png'});
12 };
13
14 // Override from common.js
15 window.flashIcon = function() {
16 var flashes = 10;
17 function flash() {
18 if (flashes == 0) {
19 stopFlashingIcon();
20 return;
21 }
22
23 if (flashes % 2 == 0) {
24 chrome.browserAction.setIcon({'path': 'clock-highlighted-19.png'});
25 } else {
26 chrome.browserAction.setIcon({'path': 'clock-19.png'});
27 }
28 flashes--;
29 iconFlashTimer = window.setTimeout(flash, 500);
30 }
31 flash();
32 };
33
34 function setTimer(alarmHours, alarmMinutes) {
35 var alarmTime = (alarmHours * 60 + alarmMinutes) * 60 * 1000;
36 var d = new Date();
37 var now = d.getHours() * HOUR_MS +
38 d.getMinutes() * 60 * 1000 +
39 d.getSeconds() * 1000;
40 var delta = (alarmTime - now);
41
42 if (delta >= -5000 && delta < 1000) {
43 ringAlarm(alarmHours, alarmMinutes);
44 if (port) {
45 port.postMessage({'cmd': 'anim'});
46 }
47 return null;
48 }
49
50 if (delta < 0) {
51 delta += HOUR_MS * 24;
52 }
53 if (delta >= 1000) {
54 if (delta > HOUR_MS) {
55 delta = HOUR_MS;
56 }
57 console.log('Timer set for ' + delta + ' ms');
58 return window.setTimeout(resetTimers, delta);
59 }
60
61 return null;
62 };
63
64 function resetTimers() {
65 if (a1Timer) {
66 window.clearTimeout(a1Timer);
67 }
68
69 try {
70 var a1_on = (localStorage['a1_on'] == 'true');
71 var a1_tt = localStorage['a1_tt'] || DEFAULT_A1_TT;
72 var a1_ampm = localStorage['a1_ampm'] || DEFAULT_A1_AMPM;
73 if (a1_on) {
74 var alarmHoursMinutes = parseTime(a1_tt, a1_ampm);
75 var alarmHours = alarmHoursMinutes[0];
76 var alarmMinutes = alarmHoursMinutes[1];
77 a1Timer = setTimer(alarmHours, alarmMinutes);
78 }
79 } catch (e) {
80 console.log(e);
81 }
82
83 try {
84 var a2_on = (localStorage['a2_on'] == 'true');
85 var a2_tt = localStorage['a2_tt'] || DEFAULT_A2_TT;
86 var a2_ampm = localStorage['a2_ampm'] || DEFAULT_A2_AMPM;
87 if (a2_on) {
88 var alarmHoursMinutes = parseTime(a2_tt, a2_ampm);
89 var alarmHours = alarmHoursMinutes[0];
90 var alarmMinutes = alarmHoursMinutes[1];
91 a2Timer = setTimer(alarmHours, alarmMinutes);
92 }
93 } catch (e) {
94 console.log(e);
95 }
96
97 if (a1_on || a2_on) {
98 chrome.browserAction.setIcon({'path': 'clock-19.png'});
99 } else {
100 chrome.browserAction.setIcon({'path': 'clock-disabled-19.png'});
101 }
102 }
103
104 function onLocalStorageChange() {
105 resetTimers();
106 }
107
108 function initBackground() {
109 window.addEventListener('storage', onLocalStorageChange, false);
110
111 chrome.extension.onConnect.addListener(function(popupPort) {
112 port = popupPort;
113 port.onDisconnect.addListener(function() {
114 port = null;
115 });
116 });
117 }
118
119 initBackground();
120 resetTimers();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698