-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
94 lines (80 loc) · 3.67 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
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
var server_url_data="https://utd-comet-cal-data-fetcher.herokuapp.com/data";
var server_url_eventData="https://utd-comet-cal-data-fetcher.herokuapp.com/eventData";
/*var server_url_data="http://localhost:5000/data"
var server_url_eventData="http://localhost:5000/eventData";*/
var time_at_utd = moment().tz("US/Central");
//console.log("Background page...");
chrome.runtime.onInstalled.addListener(function(details){
printLogToStorage("onInstalled listener called.");
fetchCalendarData();
//setting alarm time for 00:01 for the next day
var alarm_time = new Date(time_at_utd.valueOf());
alarm_time.setHours(24,1,0,0);
//printLogToStorage("Alarm Time set for "+alarm_time);
chrome.alarms.create("AlarmFetchData", {when:alarm_time.getTime(), periodInMinutes: 24*60});
});
chrome.alarms.onAlarm.addListener(function(alarm){
printLogToStorage("Alarm Fired");
chrome.storage.local.get(function(items){
if(items.calendar_data &&
items.calendar_data.date.day===time_at_utd.date() &&
items.calendar_data.date.month===(time_at_utd.month()+1) &&
items.calendar_data.date.year===time_at_utd.year())
{
//data alread exists
printLogToStorage("Data already exists");
}
else {
//data does not exist in local storage
fetchCalendarData();
}
});
});
function fetchCalendarData(){
printLogToStorage("fetching calendar data from server");
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
printLogToStorage("Main Data successfuly fetched.");
var cal_data=JSON.parse(xmlHttp.responseText);
//console.log(cal_data);
chrome.storage.local.set({calendar_data: cal_data});
cal_data.events.forEach(function(element){
var xmlHttp2 = new XMLHttpRequest();
xmlHttp2.onreadystatechange = function() {
if (xmlHttp2.readyState == 4 && xmlHttp2.status == 200) {
var event_data=JSON.parse(xmlHttp2.responseText);
//console.log(event_data);
element.details={location:event_data.location, description_html:event_data.description_html, contact_html: event_data.contact_html}
chrome.storage.local.set({calendar_data: cal_data});
}
}
xmlHttp2.open("GET", server_url_eventData+"?event_id="+element.id, true); // true for asynchronous
xmlHttp2.send(null);
});
} else if(xmlHttp.readyState == 4){
printLogToStorage("Main data could not be fetched successfuly, setting an alarm 2 minutes from now to try again.");
chrome.alarms.create("AlarmFetchDataTemp", {delayInMinutes: 2});
}
}
xmlHttp.open("GET", server_url_data, true); // true for asynchronous
xmlHttp.send(null);
};
/*
To log information to chrome local storage as the background evet page is not always active, it loads and unloads when needed.
*/
function printLogToStorage(data){
//console.log(data);
chrome.storage.local.get(function(items){
//console.log(items);
if(typeof items.storage_log !== 'undefined') {
//console.log("storage log exists");
//console.log(items.storage_log);
chrome.storage.local.set({storage_log: items.storage_log+"\nbg.js "+(new Date())+": "+data});
}
else {
//console.log("storage log does not exist");
chrome.storage.local.set({storage_log: (new Date())+"bg.js : "+data});
}
});
};