-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceWorker.js
More file actions
54 lines (49 loc) · 2.11 KB
/
serviceWorker.js
File metadata and controls
54 lines (49 loc) · 2.11 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
chrome.runtime.onInstalled.addListener(() => init());
chrome.runtime.onStartup.addListener(() => run());
chrome.alarms.onAlarm.addListener((alarm) => run());
const init = () => {
let msgs = {};
fetch('../_locales/en/messages.json').then((response) => {
response.json().then((data) => {
Object.entries(data).forEach(([key, value]) => { msgs[key] = value.message });
chrome.storage.local.set({ 'msgs': msgs }, () => {
run();
let w = new Date();
w.setMinutes(w.getMinutes() + 1);
w.setSeconds(0);
chrome.alarms.create('everyMinute', { periodInMinutes: 1, when: Date.parse(w) });
});
});
});
}
const run = () => {
chrome.storage.local.get(['msgs'], (result) => {
let dateOptions = { weekday: "long", year: "numeric", month: "long", day: "numeric" };
let dt = new Date(new Date().toLocaleString("en-US", { timeZone: result.msgs.timeZone }));
let ds = dt.toLocaleString("en-US", dateOptions);
let ampm = "";
let hours = dt.getHours();
let hourMode = ' (' + result.msgs.mode + 'hr mode)';
if (result.msgs.mode == "12") {
hourMode = '';
if (hours > 12)
ampm = "pm"
else
ampm = "am"
hours = hours % 12 || 12;
}
chrome.action.setBadgeText({ 'text': hours + ':' + zeroPad(dt.getMinutes()) });
chrome.action.setTitle({ 'title': ds + ' ' + hours + ':' + zeroPad(dt.getMinutes()) + ampm + hourMode });
chrome.action.setBadgeBackgroundColor({ 'color': '#2E3338' });
});
};
const zeroPad = (n) => { return (n < 10 ? '0' + n : n) }
chrome.action.onClicked.addListener((tab) => {
chrome.storage.local.get(['msgs'], (result) => {
result.msgs.mode = (result.msgs.mode == "12") ? 24 : 12;
chrome.storage.local.set({ 'msgs': result.msgs }, () => {
chrome.action.setBadgeText({ 'text': result.msgs.mode + 'hr' });
setTimeout(run, 1000);
});
});
});