-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm.js
95 lines (79 loc) · 2.92 KB
/
alarm.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
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
const getHostnameFromRegex = (url) => {
// run against regex
const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
// extract hostname (will be null if no match is found)
return matches && matches[1];
}
function renderTasks(tasklist) {
document.getElementById("tasks").innerHTML = "";
for(i = 0; i<tasklist.length; i++){
var a = new Date(); // Current date now.
var b = tasklist[i].time
var d = parseInt((b-a)/60000);
if (d > 0) {
document.getElementById("tasks").innerHTML +=
" <div class=\"row py-3\">\n" +
" <div class=\"col text-center\">\n" +
" <img id=\"icon-"+i+"\" width='23px' src=\"http://"+getHostnameFromRegex(tasklist[i].url) + "/favicon.ico"+"\">\n" +
" </div>\n" +
" <div class=\"col text-center\">\n" +
" <span id=\"path-"+i+"\">"+tasklist[i].url+"</span>\n" +
" </div>\n" +
" <div class=\"col text-center\">\n" +
" <span id=\"time-"+i+"\">"+d+"m</span>\n" +
" </div>\n" +
" </div>\n"
}
}
}
const alarmClock = {
onHandler : function(e) {
const timeInSecs = document.getElementById("minutes").value * 60;
console.log("setting a diffy for kizzi for " + timeInSecs);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, {status: "alarmStarted", timeInSeconds: timeInSecs}, () => {});
})
});
chrome.alarms.create(Date.now().toString(), {when: Date.now() + (timeInSecs * 1000)} );
chrome.storage.local.get(['alarms', 'totalTime'], function(alarms) {
alarms = alarms.alarms;
console.log(alarms);
if (alarms == null) {
alarms = [];
} else {
alarms = JSON.parse(alarms);
}
console.log('About to set ' + alarms);
const obj = {url: document.getElementById('url').value, time: Date.now() + (timeInSecs * 1000), mins: timeInSecs / 60};
alarms.push(obj);
chrome.storage.local.set({'alarms': JSON.stringify(alarms), 'totalTime': alarms.totalTime + timeInSecs}, () => window.close());
renderTasks(alarms);
});
// const alarms = localStorage.getItem("alarms");
// localStorage.setItem('alarms', JSON.stringify(alarms));
},
offHandler : function(e) {
chrome.alarms.clear("myAlarm");
window.close();
},
setup: function() {
const a = document.getElementById('alarmOn');
a.addEventListener('click', alarmClock.onHandler );
// var a = document.getElementById('alarmOff');
// a.addEventListener('click', alarmClock.offHandler );
chrome.storage.local.get(['alarms', 'totalTime'], function(alarms) {
alarms = alarms.alarms;
console.log(alarms);
if (alarms == null) {
alarms = [];
} else {
alarms = JSON.parse(alarms);
}
renderTasks(alarms);
});
}
};
document.addEventListener('DOMContentLoaded', function () {
alarmClock.setup();
});