-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
220 lines (184 loc) · 6.63 KB
/
popup.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var upcoming_contests = {items: []};
var ongoing_contests = {items: []};
var x = new Date();
var offset = x.getTimezoneOffset();
function get_contests(){
var xmlhttp = new XMLHttpRequest();
var upcoming_count = 0;
var ongoing_count = 0;
//console.log("rinkle ka infinite swag");
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//document.getElementById("aa").innerHTML = contest_list.result[];
var contest_list = JSON.parse(this.responseText);
var count = 0;
var localStorageData = localStorage.getItem("FetchedContestURLs");
if((localStorageData === null) || (localStorageData.length === 0))
{ localStorageData = "{}";}
var prevFetchContestURLs = JSON.parse(localStorageData);
//document.getElementById("aa").innerHTML= contest_list.result[0].id;
//console.log(contest_list.result.length);
for(var i=0; i<contest_list.result.length; i++)
{
var start_time = new Date(contest_list.result[i].startTimeSeconds * 1000);
var end_time = Date.now() - contest_list.result[i].relativeTimeSeconds*1000;
var output = check_status(start_time,end_time);
if(output == 1){
ongoing_contests.items.push({"id": contest_list.result[i].id,
"name": contest_list.result[i].name,
"st": contest_list.result[i].startTimeSeconds-offset*60,
"du": contest_list.result[i].durationSeconds,
"url": "http://codeforces.com/contests/"+contest_list.result[i].id
});
}
if(output == 2){
upcoming_contests.items.push({"id": contest_list.result[i].id,
"name": contest_list.result[i].name,
"st": contest_list.result[i].startTimeSeconds-offset*60,
"du": contest_list.result[i].durationSeconds,
"url": "http://codeforces.com/contests/"+contest_list.result[i].id
});
console.log(x.toDateString());
console.log(x);
console.log(new Date(contest_list.result[i].startTimeSeconds*1000-offset*60000));
}
}
console.log('putdata called');
putdata();
}
};
xmlhttp.open("GET", "http://codeforces.com/api/contest.list", true);
xmlhttp.send();
}
function check_status(start_time, end_time){
var current_time = Date.now();
//console.log(current_time);
//console.log(end_time);
if(current_time > end_time) {
return 0;
}
else if(current_time > start_time && current_time < end_time) {
return 1;
}
else if(current_time < start_time) {
return 2;
}
}
function sortResults(people, asc) {
people = people.sort(function(a, b) {
if (asc) {
return (a.st > b.st) ? 1 : ((a.st < b.st) ? -1 : 0);
} else {
return (b.st > a.st) ? 1 : ((b.st < a.st) ? -1 : 0);
}
});
}
get_contests();
function putdata()
{
console.log(upcoming_contests);
console.log(ongoing_contests);
// removes the previous contest entries.
$("#upcoming").empty();
$("#ongoing").empty();
$("hr").remove();
var FetchedContestURLs = {};
var notifierTag = true;
var localStorageData = localStorage.getItem("FetchedContestURLs");
if((localStorageData === null) || (localStorageData.length === 0)){ localStorageData = "{}";}
var prevFetchContestURLs = JSON.parse(localStorageData);
curTime = new Date();
sortResults(ongoing_contests.items,true);
$.each(ongoing_contests.items , function(i,post){
e = new Date((post.st+post.du)*1000);
//console.log(e.toDateString());
console.log(min);
if(e>curTime)
{
Time_dif = e-curTime+offset*60000;
var day = (Time_dif/(24*3600000));
if (day <1)
{day = '';}
else
{day = day.toString().split('.')[0];
day = day.concat(" days ");
}
console.log(day);
var hrs = ((Time_dif%(24*3600000))/3600000);
// console.log(hrs);
var min;
if(hrs<1)
{
hrs= '';
}
else
{
hrs = hrs.toString().split('.')[0];
hrs = hrs.concat(" hours ");
}
// console.log(hrs);
min = ((Time_dif%(24*3600000))%3600000)/60000;
min = min.toString().split('.')[0];
min = min.concat(" minutes ");
//
$("#ongoing").append('<div class = "row contest"><div class="col s9">' + '<a class = "contest_url" href='+'"'+post.url+'"'+' >'+
'<li>'+post.name+
'<br><br>End: '+e.toDateString()+' <br> ( Contest ends in : ' + day +' '+ hrs+ ' ' + min + ' '+' )<br>'+
'Duration: '+((post.du/60).toString().split('.')[0])+' min <br>'+
'</li></a><br></div><div class =col s3><img src = "images/cf.png"></div><hr>');
}
});
if(ongoing_contests.items.length==0)
{
$("#ongoing").append('<div class = "no_contest row">No ongoing contests!!</div>');
}
sortResults(upcoming_contests.items,true);
$.each(upcoming_contests.items , function(i,post){
var s = new Date(post.st*1000);
var e = new Date((post.st+post.du)*1000);
FetchedContestURLs[post.url] = 1;
var Time_dif = s-curTime+offset*60000;
var day = (Time_dif/(24*3600000));
if (day <1)
{day = '';}
else
{day = day.toString().split('.')[0];
day = day.concat(" days");
}
// console.log(day);
var hrs = ((Time_dif%(24*3600000))/3600000);
//console.log(hrs);
var min;
if(hrs<1)
{
hrs= '';
}
else
{
hrs = hrs.toString().split('.')[0];
hrs = hrs.concat(" hours");
}
//console.log(hrs);
min = ((Time_dif%(24*3600000))%3600000)/60000;
min = min.toString().split('.')[0];
min = min.concat(" minutes");
//console.log(min);
//unreadTag = '<div class="unread">new</div>';
$("#upcoming").append('<div class = "row contest"><div class="col s9">' + '<a class = "contest_url" href='+'"'+post.url+'"'+' >'+
'<li>'+post.name+
'<br><br>Start: '+s.toDateString()+' ( after ' + day +' '+ hrs+ ' ' + min + ' '+' )<br>'+
'Duration: '+((post.du/60).toString().split('.')[0])+' min <br>'+
'</li></a><br></div><div class =col s3><img src = "images/cf.png"></div>');
});
//saving into local storage
localStorage.setItem("FetchedContestURLs",JSON.stringify(FetchedContestURLs));
if(notifierTag){document.getElementById('scroll-info').style.display = "inline";}
chrome.browserAction.setBadgeText({text: ""}); // We have 0 unread items.
setTimeout(function(){$("#scroll-info").fadeOut(700);},5000);
}
$(document).ready(function(){
$('body').on('click', 'a', function(){
chrome.tabs.create({url: $(this).attr('href')});
return false;
});
});