-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
361 lines (309 loc) · 11.6 KB
/
background.js
File metadata and controls
361 lines (309 loc) · 11.6 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
var badgeCount = localStorage.badgeCount;
var updater = null;
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}
function log_debug( msg ) {
//console.log( msg );
}
function log_error( msg ) {
console.log( msg );
}
function validateSettings() {
var success = true;
if( localStorage.url && localStorage.subscriber ) {
alert( chrome.i18n.getMessage( "whdnotifier_invalid_config" ) );
success = false;
}
else if( ! localStorage.url && ! localStorage.subscriber ) {
alert( chrome.i18n.getMessage( "whdnotifier_config_help" ) );
success = false;
}
else {
if( localStorage.url ) {
var url = localStorage.url;
var errorMsg = null;
if( url.indexOf( "/helpdesk/WebObjects/" ) < 0
&& url.indexOf( "/cgi-bin/WebObjects/") < 0 )
{
errorMsg = chrome.i18n.getMessage( "whdnotifier_url_missing_adaptor" );
}
else if( ! url.endsWith( ".woa" ) ) {
errorMsg = chrome.i18n.getMessage( "whdnotifier_url_missing_woa" );
}
else if( ! (url.startsWith( "http://") || url.startsWith( "https://" )) ) {
errorMsg = chrome.i18n.getMessage( "whdnotifier_url_missing_proto" );
}
if( errorMsg ) {
errorMsg += "\n\n" + chrome.i18n.getMessage( "whdnotifier_url_example" );
alert( errorMsg );
}
}
}
return success;
}
function getHostedServer() {
var server = localStorage.hostedServer;
if( ! server )
server = "www.webhelpdesk.com";
return server;
}
function setUserPass(username, password) {
if(localStorage.saveUserPass === "true" ) {
localStorage.username = username;
if( password != "*****" ) {
if( password == "" ) {
localStorage.md5password = "";
} else {
localStorage.md5password = b64_md5( password );
}
//passwordTextbox.value = "*****";
}
} else {
sessionStorage.username = username;
if( password != "*****" ) {
if( password == "" ) {
sessionStorage.md5password = "";
} else {
sessionStorage.md5password = b64_md5( password );
}
//passwordTextbox.value = "*****";
}
}
}
function getUsername() {
if(localStorage.saveUserPass === "true") {
return localStorage.username || "";
} else {
return sessionStorage.username || "";
}
}
function getPassword() {
if(localStorage.saveUserPass === "true") {
return localStorage.password || "";
} else {
return sessionStorage.password || "";
}
}
function getMd5Password() {
if(localStorage.saveUserPass === "true") {
return localStorage.md5password || "";
} else {
return sessionStorage.md5password || "";
}
/*
//alert( "in getMd5Password");
//alert( "password = " + getPassword() );
var md5Password = null;
if( getPassword() != null ) {
md5Password = b64_md5( getPassword() );
}
//alert( "md5Password = " + md5Password );
return md5Password;
*/
}
function getSubscriber() {
//alert( "in getSubscriber" );
var subscriberId = localStorage.subscriber;
if( subscriberId == null || subscriberId < 0 || subscriberId == "" ) {
subscriberId = 1;
}
//alert( "subscriberId = " + subscriberId );
return subscriberId;
}
function getUseSsl() {
return localStorage.ssl ==="true";
}
function getBaseUrl() {
//alert ('in getBaseUrl')
var baseUrl;
if( localStorage.subscriber )
baseUrl = (getUseSsl() ? "https" : "http") + "://" + getHostedServer() + "/cgi-bin/WebObjects/HostedHelpdesk.woa";
else
baseUrl = localStorage.url;
//alert('baseUrl = ' + baseUrl);
return baseUrl;
}
function getBadgeCountUrl() {
//alert('In getBadgeCountUrl');
var badgeType = localStorage.badge;
//alert( 'badgeType = ' + badgeType );
var subscriber = getSubscriber();
//alert( "subscriber = " + subscriber );
var username = getUsername();
//alert( "username = " + username );
var uriUsername = encodeURIComponent( getUsername() );
//alert( "uriUsername = " + uriUsername );
var md5Password = getMd5Password();
//alert( "md5Password = " + md5Password );
var uriMd5Password = encodeURIComponent( getMd5Password() );
//alert( "uriMd5Password = " + uriMd5Password );
var url = getBaseUrl() + "/wa/APNActions/techBadgeCount?accountId=" + getSubscriber()
+ "&badgeType=" + badgeType + "&loginId=" + encodeURIComponent( getUsername() ) + "&md5Password=" + encodeURIComponent( getMd5Password() );
//alert( "Got url!" );
//alert( 'badgeCountUrl = ' + url );
return url;
}
function getLoginUrl() {
var url = getBaseUrl();
// Don't provide the username and password because it will show up in the address bar, where it could be copied & pasted by an intruder
// in order to get access. If 'login automatically' is checked, they'll only have to log in once.
//if( getUseSsl() )
// url += "/wa/login?id=" + getSubscriber() + "&username=" + encodeURIComponent( getUsername() ) + "&md5Password=" + encodeURIComponent( getMd5Password() );
// nh 6/17/10: There seems to be a bug when connecting to the hosted app using automatic login; it doesn't log you in.
// Seems to be remedied by appending "/wa?id=#". This is a hack that may need to be cleaned up later.
if( getSubscriber() )
url += "/wa?id=" + getSubscriber();
return url;
}
function updateBadgeCount() {
updateBadge( badgeCount );
}
function reloadBadgeCount() {
// don't waste time trying to get the badge count if we don't have a username or password
if( getUsername() == null || getMd5Password() == null )
return;
//alert('In reloadBadgeCount' );
log_debug('reloadBadgeCount(): url = ' + getBadgeCountUrl() );
$.ajax({
type: "GET",
url: getBadgeCountUrl(),
data: "",
cache: false,
dataType: "text",
/*
dataFilter: function(data) {
//console.log( data );
//console.log( 'dataFilter: ' + data );
return data;
},
complete: function(XMLHttpRequest, textStatus) {
//console.log( 'complete: ' + textStatus );
},
*/
success: function(data, textStatus, xhr) {
//console.log( 'success: ' + data );
//console.log( 'textStatus: ' + textStatus );
//console.log( 'xhr: ' + xhr );
log_debug( 'techBadgeCount returned: \n"' + data + '"' );
re = /^Web Help Desk ([0-9\.]+)\n(.*)$/;
var reGroups = re.exec( data );
if( reGroups && reGroups.length >= 3 ) {
//alert( 'match returned: ' + reGroups );
//alert( 'whd version: ' + reGroups[1] );
//alert( 'badge count: ' + reGroups[2] );
badgeCount = reGroups[2];
}
else {
badgeCount = 0;
}
if(badgeCount > localStorage.badgeCount) {
// If new badge count has more tickets, there's a new or updated ticket to look at!
whdNotify( badgeCount );
}
// store in settings so it persists between launches
localStorage.badgeCount = badgeCount;
updateBadge( badgeCount );
chrome.browserAction.setPopup({popup: ""});
},
error:function (xhr, textStatus, thrownError){
badgeCount = 0;
updateBadge( badgeCount );
if(getUsername() != "" && getMd5Password() != "") { // Only throw error if username and password are not filled out
alert( xhr.status + " error while fetching tech badge count. Check your Options settings." );
log_error( "Error while fetching badge count: " + xhr.status + " (" + thrownError + ") -- check your Options settings.");
} else {
updateBadge( "login" );
chrome.browserAction.setPopup({popup: "popup.html"});
}
}
});
}
function whdNotify(n) {
var notification = webkitNotifications.createNotification(
'icon_128.png',
'New or Updated Ticket',
'New/Updated ticket count is now '+n+'.'
);
notification.onshow = notification.ondisplay = function() { setTimeout(function() { notification.cancel(); }, 5000); };
notification.show();
}
function updateBadge(n) {
if( n == 0 )
n = "";
chrome.browserAction.setBadgeText({text:n});
}
function isHelpdeskUrl(url) {
// This is the Gmail we're looking for if:
// - starts with the correct gmail url
// - doesn't contain any other path chars
var isMatch = false;
var whdUrl = getBaseUrl();
if( whdUrl && url.startsWith( whdUrl ) )
isMatch = true;
return isMatch;
}
function goToHelpdesk() {
if( validateSettings() ) {
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url && isHelpdeskUrl(tab.url)) {
chrome.tabs.update(tab.id, {selected: true, url:getLoginUrl()});
return;
}
}
chrome.tabs.create({url: getLoginUrl()});
});
}
}
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.browserAction.getPopup({}, function(pop) {
if(pop == "") {
goToHelpdesk();
}
});
});
chrome.extension.onMessage.addListener(function (details) {
if(details['act'] == "loginInit") {
if((localStorage.autoServer ? (localStorage.autoServer === "true") : true) && details['server']) {
localStorage.url = details['server'];
}
if(details['origin'] == "autoLogin") { // If coming from autoLogin...
if(!localStorage.autoCredentials || // Check if using default setting
localStorage.autoCredentials == "always" || // Or always auto-grabbing
(localStorage.autoCredentials == "sometimes" && // Or somtimes...
getUsername() == "" && // With blank username
getMd5Password() == "") // And blank password
) {
setUserPass(details['username'], details['password']);
init();
}
} else { // If not coming from autoLogin, we don't care!
setUserPass(details['username'], details['password']);
init();
}
}
return true;
});
function init() {
var success = false;
log_debug( "init()" );
//alert( 'init');
chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]});
chrome.browserAction.setIcon({path:"whd_icon.png"});
chrome.browserAction.setTitle({title:chrome.i18n.getMessage("whdnotifier_name")});
if( updater )
window.clearInterval( updater );
if( validateSettings() && localStorage.update ) {
//alert('setting updater interval');
updater = window.setInterval(reloadBadgeCount, localStorage.update * 1000);
reloadBadgeCount();
success = true;
}
else {
badgeCount = localStorage.badgeCount = 0;
updateBadge( badgeCount );
}
return success;
}
init();