-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathementa.js
More file actions
143 lines (122 loc) · 3.5 KB
/
Copy pathementa.js
File metadata and controls
143 lines (122 loc) · 3.5 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var hoje = new Date().setHours(0, 0, 0, 0);
const APIKey = "AIzaSyBJd7mhA4xaW0qPZDpuexEwG_yBYPX-VA0";
const diasSemana = [
"Dom.",
"Seg.",
"Ter.",
"Qua.",
"Qui.",
"Sex.",
"Sab.",
"Dom."
];
let ementaNormalID = "cfc0289d736db3a530d663a2a3e70d7f2da313eaebfa9e76718338f09a6175ae";
let ementaVegetarianaID = "c0d56c91bf4cdf9fc7233d057124efeeae599e905f1397cc8394566a97216d23";
function getProximasEmentas(onComplete) {
var datasProx = getDataProximaEmenta();
const lastUpdate =
new Date() -
(localStorage["calendarioEmenta"]
? JSON.parse(localStorage["calendarioEmenta"]).data || 0
: 0);
if (datasProx.length === 0 && lastUpdate > 30 * 1000) {
return getEmentas(function() {
getProximasEmentas(onComplete);
});
}
onComplete(datasProx);
}
function getDataProximaEmenta() {
var res = [];
if (localStorage["calendarioEmenta"]) {
var calendario = JSON.parse(localStorage["calendarioEmenta"]);
var datas = Object.keys(calendario);
var i;
for (i = 0; i < datas.length; i++) {
var data = datas[i];
if (data >= hoje) {
res.push({
ementaA: calendario[data].almoco,
ementaJ: calendario[data].jantar,
data: data
});
}
}
res.sort(function(a, b) {
if (a.data < b.data) return -1;
if (a.data > b.data) return 1;
return 0;
});
}
return res;
}
function getEmentas(onComplete) {
chrome.runtime.sendMessage({ "get-vegetariano": true }, function(
isVegetariano
) {
var calendario = {};
var m = new Date(hoje);
var hojeTXT =
m.getFullYear() +
"-" +
("0" + (m.getMonth() + 1)).slice(-2) +
"-" +
("0" + m.getDate()).slice(-2); // YYYY-MM-DD
var opts =
"?singleEvents=true&timeMin=" +
hojeTXT +
"T00:00:00Z&orderBy=startTime&alwaysIncludeEmail=false&showDeleted=false&maxResults=250&key=" +
APIKey;
var selectedID = isVegetariano ? ementaVegetarianaID : ementaNormalID;
var pagEmenta =
"https://www.googleapis.com/calendar/v3/calendars/" +
selectedID +
"@group.calendar.google.com/events" +
opts;
$.ajax({
url: pagEmenta,
dataType: "json",
timeout: 8000,
success: function(ementas) {
if (ementas && ementas.items) {
$.each(ementas.items, function(i, comer) {
date = new Date(comer.start.dateTime);
if (date.getHours() <= 16){
var ementa = comer.summary;
var dia = new Date(comer.start.dateTime).setHours(0, 0, 0, 0);
calendario[dia] = { almoco: ementa, jantar: "" };
} else {
var ementa = comer.summary;
var dia = new Date(comer.start.dateTime).setHours(0, 0, 0, 0);
if (calendario[dia]) {
calendario[dia].jantar = ementa;
}
}
});
calendario["data"] = new Date().getTime();
localStorage["calendarioEmenta"] = JSON.stringify(calendario);
}
onComplete();
},
error: function() {
onComplete();
}
});
});
}
function dataToRelativeString(data) {
var dif = data - hoje;
var day = 1000 * 60 * 60 * 24;
if (dif >= 0) {
if (dif < day) return "Hoje";
if (dif < 2 * day) return "Amanhã";
}
var d = new Date(data);
if (dif > 0 && dif < 7 * day) {
// se for nos próximos 7 dias
return diasSemana[d.getDay()];
}
var month = d.getMonth() + 1;
var day = d.getDate();
return day + "/" + month;
}