-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase-notifier.js
More file actions
439 lines (397 loc) · 14 KB
/
base-notifier.js
File metadata and controls
439 lines (397 loc) · 14 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//
// Export
//
var BaseNotifier;
YUI.add('base-notifier', function (Y) {
/**
* @constructor
*/
BaseNotifier = function () {
BaseNotifier.superclass.constructor.apply(this, arguments);
};
BaseNotifier.NAME = "BaseNotifier";
//
// Attributes
//
BaseNotifier.ATTRS = {
/**
* The title of your extension
*/
title : {
value : null
},
/**
* The URL of the app
*/
url : {
value : null
},
/**
* Known domains for the app. Domains here must be listed under
* permissions in your manifest.json.
*/
domains : {
value : [
'your.domain.com',
'another.domain.com'
]
},
/**
* Text to display to the user
*/
text : {
value : {
success : 'You have {num} new items.',
notLoggedIn : 'You are currently not signed in. Click to sign in.',
notificationTitle : 'New item!'
}
},
/**
* Icon to display while the user is logged in
*/
icons : {
value : {
loggedIn : {
'19' : 'browser-action-icon-active19x19.png',
'38' : 'browser-action-icon-active76x76.png'
},
notLoggedIn : {
'19' : 'browser-action-icon-inactive19x19.png',
'38' : 'browser-action-icon-inactive76x76.png'
},
notification : 'app.svg'
}
},
/**
* The font color of the icon while logged in
*/
loggedInColor : {
value : [246,101,2,200]
},
/**
* The font color of the icon while logged out
*/
notLoggedInColor : {
value : [105,105,105,200]
},
/**
* The user's desktop notifications preference. True means they want
* desktop notifications - False means they don't.
*/
notificationPreference : {
getter : function () {
var value = localStorage.base_notifier_desktop_notifications // legacy storage item
|| localStorage.options__enable_desktop_notifications;
if (Y.Lang.isValue(value)) {
return value === '1';
}
return true;
}
},
/**
* The user's desktop notifications preference. True means they want
* desktop notifications - False means they don't.
*/
notificationSoundPreference : {
getter : function () {
if (Y.Lang.isValue(localStorage.options__enable_notification_sound)) {
return localStorage.options__enable_notification_sound === '1';
}
return false;
}
}
};
Y.extend(BaseNotifier, Y.Base, {
/**
* Number to notify about (e.g. unread emails, new tickets etc).
* When this number increases, we'll send a desktop notification
* and update the icon.
* @type Int|Null
*/
_number : null,
/**
* Number of failed attempts to sign in to our app
* the last successful one.
* @type Int
*/
_numFailedConnections : 0,
/**
* Timer object (returned by Y.later) for the periodic checks.
* (Call cancel on this to stop the checks)
* @type Object
*/
_timer : null,
/**
* Whether or not the user is logged in to the app.
* @type Boolean
*/
_loggedIn: false,
/**
* Initializes the notifier. Starts periodic checks and subscribes
* the necessary event listeners
*/
initializer : function () {
var me = this;
this.drawIcon("?");
//
// First attempt to get number of unread emails (and show them in the
// icon).
//
this.fetchNumber();
//
// From now on check for new emails every minute
//
this._timer = Y.later(60000, this, this.fetchNumber, {}, true);
//
// Open application when the icon is clicked
//
chrome.browserAction.onClicked.addListener(function () {
me.openApp();
});
//
// Page reloads within the app
//
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete') {
if (Y.Array.find(me.get('domains'), function (e) {
return tab.url.match(new RegExp('https?:\\/\\/([^.]*\\.)?' + e));
})) {
me.onAppReload(tabId, changeInfo, tab);
}
}
});
//
// Listen for messages from the content script
//
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (Y.Lang.isValue(request.number)) {
me.drawNumberAndNotify(request.number || 0, request.notify);
try {
me._timer.cancel();
} catch (e) {
}
}
if (request.unload) {
me._timer = Y.later(60000, me, me.fetchNumber, {}, true);
}
}
);
},
/**
* Callback function fired when one of the tabs/pages of the
* app is updated/reloaded. Injects a content script that
* notifies the extension of changes in the number of items.
* @param {Int} tabId Id of the tab that's changed
* @param {Obj} changeInfo Information about the change
* @param {Chrome Extension Tab} Tab instance
*/
onAppReload : function (tabId, changeInfo, tab) {
this._timer.cancel();
this._timer = Y.later(60000, this, this.fetchNumber, {}, true);
var nodeToNumber = this.getNumberFromNode.toString();
chrome.tabs.executeScript(tabId, {
file: "lib/yui.js",
runAt: "document_start"
});
chrome.tabs.executeScript(tabId, {
file: "base-notifier.js",
runAt: "document_start"
});
chrome.tabs.executeScript(tabId, {
file: "app-notifier.js",
runAt: "document_start"
});
chrome.tabs.executeScript(tabId, {
file: "content-script.js",
runAt: "document_end"
});
},
/**
* Draws the browserAction icon for this extension.
* @param {String} txt
*/
drawIcon : function (txt) {
var
iconPath = this._loggedIn ? this.get('icons').loggedIn : this.get('icons').notLoggedIn,
badgeColor = this._loggedIn ? this.get('loggedInColor') : this.get('notLoggedInColor');
chrome.browserAction.setIcon({
path: iconPath
});
chrome.browserAction.setBadgeBackgroundColor({ color: badgeColor });
if (txt) {
chrome.browserAction.setBadgeText({ text: txt });
} else {
chrome.browserAction.setBadgeText({ text: '' });
}
},
/**
* Fetches the number to be displayed in the extension icon using
* an XHR request. Updates the icon and launches a notification if
* needed / applicable.
* @returns {Void}
*/
fetchNumber : function (xhr) {
if (this.get('url')) {
if (Y.Lang.isNull(this._number)) {
this.drawIcon("...");
}
Y.io(this.get('url'), {
on: {
success: this.onFetchNumberSuccess,
failure: this.onFetchNumberFailure
},
context : this
});
}
},
/**
* Success callback for fetchNumber()
* @param {Int} id YUI IO transaction ID
* @param {Object} reponse YUI IO response object
*/
onFetchNumberSuccess : function (id, response) {
var
tmpNode = Y.Node.create(response.responseText),
newNumber = this.getNumberFromNode(tmpNode);
if (!this.drawNumberAndNotify(newNumber, true)) {
this.onFetchNumberFailure(id, response);
}
//
// Clean up
//
tmpNode.destroy(true);
},
/**
* Success callback for fetchNumber()
* @param {Int} newNumber The new number of items to display
* @param {Boolean} doNotify Whether or not to create a desktop notification
* @returns Boolean
*/
drawNumberAndNotify : function (newNumber, doNotify) {
if (!Y.Lang.isNull(newNumber)) {
this._loggedIn = true;
if (Y.Lang.isNull(this._number)) {
} else if (this._number < newNumber) {
if (doNotify) {
this._number = newNumber;
this.notify();
}
}
this._number = newNumber;
this.drawIcon(
newNumber === 0 ? '' : newNumber.toString()
);
chrome.browserAction.setTitle({
title: Y.Lang.sub(
this.get('text').success,
{ num : (newNumber.toString() || '0') }
)
});
return true;
}
return false;
},
/**
* Get the number (to display in the icon and notify about) from a DOM node.
* The node is created from response text received by an XHR request on
* the app url.
* @parm {tmpNode}
*/
getNumberFromNode : function (tmpNode) {
console.error('getNumberFromNode() must be overridden in your sub class');
//
// e.g:
// return tmpNode.one('.some .selector').get('text');
//
},
/**
* Failure callback for fetchNumber()
* @param {Int} id YUI IO transaction ID
* @param {Object} reponse YUI IO response object
*/
onFetchNumberFailure : function (id, response) {
this._loggedIn = false;
this._numFailedConnections += 1;
if (this._numFailedConnections > 2) {
this._timer.cancel();
}
this.drawIcon("?");
chrome.browserAction.setTitle({
title: Y.Lang.sub(this.get('text').notLoggedIn, {})
});
},
/**
* Notify the user of new email.
*/
notify : function () {
this.showDesktopNotification();
this.playNotificationSound();
},
showDesktopNotification : function () {
if (this.get('notificationPreference')) {
chrome.notifications.create(this.get('text').notificationTitle, {
type: "basic",
title: this.get('text').notificationTitle,
message: Y.Lang.sub(this.get('text').success, { num : this._number.toString() }),
iconUrl: this.get('icons').notification
}, function () {});
var me = this;
chrome.notifications.onClicked.addListener(function (notificationId) {
if (notificationId === me.get('text').notificationTitle) {
me.openApp();
}
});
}
},
playNotificationSound : function() {
if (this.get('notificationSoundPreference')) {
var audio = new Audio("audio/default-notification.mp3");
audio.play();
}
},
/**
* Opens the web app in new tab unless it's opened already in the
* currently selected tab.
*/
openApp : function () {
var
me = this,
active = false,
dLen = me.get('domains').length;
Y.Array.map(
me.get('domains'),
function (e) {
var pattern = "*://*." + e + "/*";
chrome.tabs.query({
url : pattern
}, function (tabs) {
dLen = dLen-1;
var
tabsLen = tabs.length,
i;
for (i=0; i < tabsLen; i++) {
if (!tabs[i].active) {
chrome.tabs.update(tabs[i].id, {active: true});
}
chrome.windows.update(tabs[i].windowId, {focused: true});
active = true;
break;
}
if (!active && (dLen === 0)) {
active = true;
chrome.tabs.create({
url: me.get('url'),
windowId : chrome.windows.WINDOW_ID_CURRENT
}, function (tab) {
chrome.windows.update(tab.windowId, {focused: true});
});
}
});
}
);
}
});
}, '0.0.1', {
requires: ['base-base', 'io-base', 'node-base', 'array-extras']
});