This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.js
149 lines (131 loc) · 4.08 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
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
// Prefs
var refreshTime;
var requestUrl;
var auth;
var desc;
var green;
// Globals (eww!)
var abortTimer;
var refreshTimer;
var jobs;
var xhr;
function init() {
desc = localStorage.desc == 'true';
green = localStorage.green == 'true';
if (!localStorage.url) {
updateStatus(-1);
jobs = null;
return;
} else
requestUrl = localStorage.url;
refreshTime = localStorage.refreshTime || REFRESH_DEFAULT;
refreshTime *= 60 * 1000; // minutes in ms
if (typeof localStorage.username == 'string') {
auth = window.btoa((localStorage.username || '') + ':' + (localStorage.password || ''));
} else {
auth = null;
}
doRequest();
}
function doRequest() {
if (xhr) {
xhr.abort();
}
xhr = new XMLHttpRequest();
window.clearTimeout(abortTimer);
abortTimer = window.setTimeout(xhr.abort, REQUEST_TIMEOUT);
try {
xhr.onreadystatechange = checkResponse;
xhr.onerror = handleError;
xhr.open('GET', requestUrl + 'api/json', true);
if (typeof auth == 'string') {
xhr.setRequestHeader('Authorization', 'Basic ' + auth);
}
xhr.send();
} catch (e) {
handleError(e);
}
}
function checkResponse() {
if (xhr.readyState != 4) return;
if (xhr.status == 200 && xhr.responseText) {
var response = JSON.parse(xhr.responseText);
var topStatus = -1;
if (response.jobs) {
jobs = response.jobs;
if (localStorage.sorting == 'status') {
jobs.sort(sortByStatus);
} else {
jobs.sort(sortByName);
}
for (var i in response.jobs)
topStatus = Math.max(topStatus, STATUSES[response.jobs[i].color]);
}
handleSuccess(topStatus);
return;
} else {
handleError(xhr.status != 200 ? 'HTTP ' + xhr.status : 'No responseText found');
}
}
function handleSuccess(status) {
xhr = null;
window.clearTimeout(abortTimer);
updateStatus(status);
window.clearTimeout(refreshTimer);
refreshTimer = window.setTimeout(doRequest, refreshTime);
}
function handleError(error) {
xhr = null;
window.clearTimeout(abortTimer);
jobs = null;
updateStatus('ERROR');
window.clearTimeout(refreshTimer);
refreshTimer = window.setTimeout(doRequest, refreshTime);
}
function sortByName(a, b) {
var o1 = a.name.toLowerCase();
var o2 = b.name.toLowerCase();
if (o1 < o2) return desc ? 1 : -1;
else if (o2 < o1) return desc ? -1 : 1;
else return 0;
}
function sortByStatus(a, b) {
if (STATUSES[a.color] == STATUSES[b.color]) return sortByName(a, b);
return desc ? STATUSES[a.color] - STATUSES[b.color] : STATUSES[b.color] - STATUSES[a.color];
}
function updateStatus(status) {
if (status == 0) {
chrome.browserAction.setIcon({path:'images/grey19.png'});
chrome.browserAction.setBadgeText({text:''});
} else if (status == 1) {
if (green) {
chrome.browserAction.setIcon({path:'images/green19.png'});
} else {
chrome.browserAction.setIcon({path:'images/blue19.png'});
}
chrome.browserAction.setBadgeText({text:''});
} else if (status == 2) {
chrome.browserAction.setIcon({path:'images/yellow19.png'});
chrome.browserAction.setBadgeText({text:''});
} else if (status == 3) {
chrome.browserAction.setIcon({path:'images/red19.png'});
chrome.browserAction.setBadgeText({text:''});
} else {
chrome.browserAction.setIcon({path:'images/icon19.png'});
chrome.browserAction.setBadgeText({text:'?'});
}
}
function openTab() {
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url && tab.url.indexOf(localStorage.url) == 0) {
chrome.tabs.update(tab.id, {selected: true});
return;
}
}
chrome.tabs.create({url: localStorage.url});
});
}
window.onload = function() {
window.setTimeout(init, 10);
}