-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbackground.js
More file actions
124 lines (103 loc) · 2.76 KB
/
Copy pathbackground.js
File metadata and controls
124 lines (103 loc) · 2.76 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
var timeout;
var interval;
var setDate;
var pauseDate;
var alarmDate;
var greenColor = [76, 187, 23, 255];
var yellowColor = [250, 150, 0, 255];
var guiLagAdjustment = 500;
var alarmSound = new Audio("chime.mp3");
function setAlarm(tMillis)
{
interval = tMillis;
ringIn(tMillis + guiLagAdjustment);
}
function ringIn(tMillis)
{
clearTimeout(timeout);
pauseDate = null;
var tSecs = parseInt(tMillis / 1000);
var tMins = parseInt(tSecs / 60);
var secs = tSecs % 60;
var tHrs = parseInt(tMins / 60);
var mins = tMins % 60;
var millis = tMillis % 1000;
alarmDate = new Date();
// alarmDate.setTime(alarmDate.getTime() + millis);
alarmDate.setHours(alarmDate.getHours() + tHrs);
alarmDate.setMinutes(alarmDate.getMinutes() + mins);
alarmDate.setSeconds(alarmDate.getSeconds() + secs);
alarmDate.setMilliseconds(alarmDate.getMilliseconds() + millis);
setDate = new Date();
timeout = setTimeout(ring, alarmDate.getTime() - setDate.getTime());
chrome.browserAction.setBadgeBackgroundColor({color:greenColor});
setInterval(function() {
chrome.browserAction.setBadgeText({text: getTimeLeftString()});
}, 1000);
}
function pause()
{
pauseDate = new Date();
clearTimeout(timeout);
chrome.browserAction.setBadgeBackgroundColor({color:yellowColor});
}
function resume()
{
var remainingAfterPause = (alarmDate.getTime() - pauseDate.getTime());
ringIn(remainingAfterPause);
}
function restart()
{
ringIn(interval + guiLagAdjustment);
}
function getTimeLeft()
{
if (pauseDate)
return (alarmDate.getTime() - pauseDate.getTime());
var now = new Date();
return (alarmDate.getTime() - now.getTime());
}
function getTimeLeftPercent()
{
return parseInt(getTimeLeft() / interval * 100);
}
function getTimeLeftString()
{
var until = getTimeLeft();
var tSecs = parseInt(until / 1000);
var tMins = parseInt(tSecs / 60);
var secs = tSecs % 60;
var tHrs = parseInt(tMins / 60);
var mins = tMins % 60;
if(secs < 10) secs = "0" + secs;
if(mins < 10) mins = "0" + mins;
if(tHrs < 10) tHrs = "0" + tHrs;
return ((tHrs > 0 ? tHrs + ":" : "") + mins + ":" + secs);
}
function didCreateNotification(notificationId) {}
function ring()
{
var options = {
type: "basic",
title: "Timer",
message: "Time\'s up!",
iconUrl: "img/48.png",
priority: 2
}
chrome.notifications.create("", options, didCreateNotification);
alarmSound.play();
turnOff();
}
function turnOff()
{
clearTimeout(timeout);
interval = 0;
alarmDate = null;
pauseDate = null;
setDate = null;
chrome.browserAction.setBadgeText({text: ""});
}
function error()
{
alert("Please enter a number between 1 and 240.");
}