-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbackground.js
More file actions
54 lines (51 loc) · 1.92 KB
/
Copy pathbackground.js
File metadata and controls
54 lines (51 loc) · 1.92 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
function checkInsights(timecheck) {
var api_url = localStorage["install_url"] + "api/v1/insight.php" + "?since=" + timecheck
+ "&as=" + localStorage["install_api_key"] + '&un=' + encodeURI(localStorage["email_address"]);
//var api_url = localStorage["install_url"] + "test.json";
console.log(Date() + " checking for new insights " + api_url);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
if (typeof data.error === 'undefined') {
if (data.length >= 3) {
var title = "New insights available";
var message = "ThinkUp has " + data.length + " new insights for you.";
var notification = window.webkitNotifications.createNotification("icon.png", title, message);
notification.show();
} else {
for (var i = 0; i < data.length; i++) {
var insight = data[i];
var htmlstripper = document.createElement("div");
htmlstripper.innerHTML = insight.text;
var title = insight.prefix.replace(":","");
var notification = window.webkitNotifications.createNotification("icon.png", title, htmlstripper.innerText);
notification.show();
}
}
} else {
console.log('Error: ' + data.error.message);
}
}
}
xhr.open("GET", api_url, true);
xhr.send();
return Math.round(+new Date() / 1000);
}
// Test for notification support.
if (window.webkitNotifications) {
var timecheck = Math.round(+new Date() / 1000);
// While activated, show notifications at the display frequency.
timecheck = checkInsights(timecheck);
var interval = 5; // The polling interval, in minutes.
var minutes_passed = 0;
setInterval(function() {
minutes_passed++;
if (minutes_passed >= interval) {
timecheck = checkInsights(timecheck);
minutes_passed = 0;
} else {
console.log(Date() + ' ' + minutes_passed + ' minute(s) passed since last check, waiting to reach ' + interval);
}
}, 60000);
}