-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
99 lines (87 loc) · 2.67 KB
/
main.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
var setSound = function(sound) {
if (sound == null || sound == '')
return;
localStorage['play-sound'] = sound;
$("#notification").remove();
var audio = $("<audio id='notification'><source src='http://desksms.appspot.com/notifications/" + sound.replace("-", ".") + "'></source></audio>");
$("#body").append(audio);
}
var setupPush = function() {
desksms.push(function(err, data) {
if (data && data.envelope) {
var incomingMessages = 0;
$.each(data.envelope.data, function(index, message) {
if (message.type != 'incoming')
return;
var icon = 'http://desksms.appspot.com/images/desksms-small.png';
var name = message.name;
if (!name)
name = message.number;
if (!name)
return;
if (!message.message)
return;
var title = "SMS Received: " + name;
var notification = webkitNotifications.createNotification(icon, title, message.message);
notification.show();
setTimeout(function() {
notification.cancel();
}, 10000);
incomingMessages++;
});
// don't update the badge if nothing is incoming
if (incomingMessages == 0)
return;
var badgeCount = localStorage['badge'];
try {
badgeCount = parseInt(badgeCount);
if (isNaN(badgeCount))
badgeCount = 0;
}
catch (e) {
badgeCount = 0;
}
var sound = localStorage['play-sound'];
if (sound && sound != '') {
var notification = $('#notification');
if (notification.length > 0) {
notification = notification[0];
notification.play();
}
}
badgeCount += incomingMessages;
localStorage['badge'] = badgeCount;
chrome.browserAction.setBadgeText({ text: String(badgeCount) } );
}
});
}
var showToast = false;
$(document).ready(function() {
chrome.browserAction.setBadgeBackgroundColor({color:[255,0,0,255]});
setSound(localStorage['play-sound']);
var whoamiChecker = function() {
desksms.whoami(function(err, data) {
if (err || !data.email) {
console.log('login fail.');
return;
}
console.log('successfully logged in.');
// prevent further whoamis from being called once logged in
whoamiChecker = null;
setupPush();
});
}
whoamiChecker();
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
var e = request['event'];
if (e == "login" && whoamiChecker) {
whoamiChecker();
}
else if (e == "sound") {
setSound(request.sound);
}
else if (e == "toast") {
showToast = request.toast;
}
});
});