-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimer.js
50 lines (46 loc) · 1.12 KB
/
timer.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
/**
* Initially called when extension page loads. Sets up a bunch of stuff.
*/
function init() {
addOnClick();
addMessageListeners();
startTimer();
}
/**
* Sends a message to background page to start the timer.
*/
function startTimer() {
chrome.runtime.sendMessage({
"command": "startTimer"
}, function(response) {
console.log(response.message);
});
}
/**
* Adds listeners so it knows how to handle the messages from the background page.
*/
function addMessageListeners() {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.command) {
case "updateTime":
document.getElementById("current-time").innerText = request.time;
break;
case "timerEnded":
console.log("Timer ended.");
break;
}
});
}
/**
* Adds onclick listener to the stop button.
*/
function addOnClick() {
document.getElementById("stop").onclick = function() {
chrome.runtime.sendMessage({
"command": "endTimer"
});
document.location = chrome.runtime.getURL("popup.html");
chrome.browserAction.setBadgeText({"text" : ""});
}
}
document.addEventListener('DOMContentLoaded', init);