-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNotification.js
executable file
·108 lines (85 loc) · 2.42 KB
/
Notification.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
(function() {
var OS = null
function Notification(data, options) {
this.options = this.buildOptions(options)
this.data = this.buildData(data)
this.id = this.buildId(data)
}
Notification.prototype = {
buildData: function(data) {
var requireInteraction = this.shouldRequireInteraction()
var buttons
if (OS === 'win' && requireInteraction) {
buttons = [{ title: 'Close', iconUrl: '' }]
}
return Object.assign({
type: 'basic',
requireInteraction: requireInteraction,
buttons: buttons
}, data)
},
buildId: function(data) {
return [
Notification.PREFIX,
data.SID,
data.title,
data.message
].join('-')
},
buildOptions: function(options) {
var settings = {
expirationTime: 8,
playSound: false
}
if (! options) return settings
for (var key in settings) {
if (options[key] != null) settings[key] = options[key]
}
return settings
},
shouldRequireInteraction: function() {
return this.options.expirationTime > 10
},
send: function(tabId) {
if (this.options.playSound) {
this.playSound()
}
chrome.notifications.create(this.id, this.data, function() {
Notification.cache[this.id] = tabId
setTimeout(this.clear.bind(this), this.options.expirationTime * 1000)
}.bind(this))
},
playSound: function() {
var ding = new Audio()
ding.src = chrome.extension.getURL('audio/ding.mp3')
ding.play()
},
clear: function() {
Notification.clear(this.id)
}
}
Notification.PREFIX = '[HangoutsNotifications]'
Notification.IS_WINDOWS = false // set later
Notification.cache = {
// notificationId: tabId
}
Notification.clear = function(notificationId) {
chrome.notifications.clear(notificationId)
delete Notification.cache[notificationId]
}
Notification.clearAll = function(notificationId) {
chrome.notifications.getAll(function(notifications) {
if (! notifications) return
for (var id in notifications) {
if (id.indexOf(Notification.PREFIX) !== -1) chrome.notifications.clear(id)
}
})
}
chrome.notifications.onButtonClicked.addListener(function(id) {
Notification.clear(id)
})
chrome.runtime.getPlatformInfo(function(info) {
OS = info.os
})
window.Notification = Notification
})()