-
Notifications
You must be signed in to change notification settings - Fork 6
/
background.html
72 lines (64 loc) · 2.05 KB
/
background.html
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
<script>
var statusURL = "http://travis-ci.org/builds/last.json";
if (!localStorage.prefs) {
// Default to notifications being on.
localStorage.prefs = JSON.stringify({ "use_notifications": true });
}
var lastOpen = null;
var lastNotification = null;
function notifyIfStatusChange(open, status) {
var prefs = JSON.parse(localStorage.prefs);
if (lastOpen && lastOpen != open && prefs.use_notifications) {
if (lastNotification) {
lastNotification.cancel();
}
var notification = webkitNotifications.createNotification(
chrome.extension.getURL("icon.png"), "Tree is " + open, status);
lastNotification = notification;
notification.show();
}
lastOpen = open;
}
function updateStatus(status) {
chrome.browserAction.setTitle({title:status});
var open = /open/i;
if (open.exec(status)) {
notifyIfStatusChange("open", status);
chrome.browserAction.setBadgeText({text:"\u2022"});
chrome.browserAction.setBadgeBackgroundColor({color:[0,255,0,255]});
} else {
notifyIfStatusChange("closed", status);
//chrome.browserAction.setBadgeText("\u2639");
chrome.browserAction.setBadgeText({text:"\u00D7"});
chrome.browserAction.setBadgeBackgroundColor({color:[255,0,0,255]});
}
}
function requestStatus() {
requestURL(statusURL, updateStatus);
setTimeout(requestStatus, 30000);
}
function requestURL(url, callback) {
//console.log("requestURL: " + url);
var xhr = new XMLHttpRequest();
try {
xhr.onreadystatechange = function(state) {
if (xhr.readyState == 4) {
var text = xhr.responseText;
//console.log(text);
callback(text);
}
}
xhr.onerror = function(error) {
console.log("xhr error: " + JSON.stringify(error));
console.dir(error);
}
xhr.open("GET", url, true);
xhr.send({});
} catch(e) {
console.log("exception: " + e);
}
}
window.onload = function() {
window.setTimeout(requestStatus, 10);
}
</script>