This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
69 lines (55 loc) · 1.8 KB
/
background.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
var colors = ["black", "white", "red", "blue", "green", "yellow"];
var color = "black";
var format24 = true;
chrome.storage.sync.get(['color'], function(result) {
color = result.color || 'black';
update();
});
chrome.storage.sync.get(['format24'], function(result) {
var stringFormat = result.format24 || "twentyfour";
format24 = (stringFormat == "twentyfour");
update();
});
function update() {
var date = new Date();
var hours = date.getHours();
if(format24 != true) {
hours = hours % 12
if(hours == 0){
hours = 12;
}
}
var minutes= date.getMinutes();
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
context.fillStyle = color;
context.font = "80px sans-serif";
context.fillText(hours, 1, 58);
context.font = "62px sans-serif";
context.fillText((minutes<10 ? "0": "") + minutes, 60, 110);
var imageData = context.getImageData(0, 0, 128, 128);
chrome.browserAction.setIcon({imageData: imageData});
chrome.browserAction.setTitle({title: date.toString()});
setTimeout(update, (60-date.getSeconds())*1000);
}
chrome.browserAction.onClicked.addListener(function() {
var currentIndex = colors.indexOf(color);
var newIndex = (currentIndex + 1) % colors.length;
if(newIndex == 0) {
format24 = !format24;
}
chrome.storage.sync.set({
color: colors[newIndex],
format24: (format24 ? "twentyfour" : "twelve")
});
});
function logStorageChange(changes, area) {
if(changes['color']) {
color = changes['color'].newValue
}
if(changes['format24']) {
format24 = (changes['format24'].newValue == "twentyfour");
}
update();
}
chrome.storage.onChanged.addListener(logStorageChange);