-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
executable file
·181 lines (142 loc) · 5.05 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
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
/* Globals: configuration, Notification, optionalPermissions */
// ----------------------------------------------------------
// Messages from the front-end
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(message, port) {
let tab = port.sender.tab
let data = message.data
switch (message.type) {
case 'icon': {
let status = data.active ? '' : 'inactive_'
chrome.browserAction.setIcon({
path: {
19: 'icons/' + status + '19.png',
38: 'icons/' + status + '38.png'
},
tabId: tab.id
})
break
}
case 'notification': {
let changes = data.changes
configuration.get(function(config) {
if (isUrlDisabled(tab.url, config.disabledUrls)) return
if (changes.becameOnline && !config.showOnlineNotifications) return
if (changes.becameUnread && !config.showUnreadNotifications) return
if (config.muteAllExcept && !containsAnyName(data.name, config.muteAllExcept)) return
if (!changes.becameOnline && config.messageKeywords && !containsAnyKeywords(data.text, config.messageKeywords)) return
if (data.tabActive && config.fireOnInactiveTab) return
let notificationData = {
title: data.name,
iconUrl: data.avatar,
message: changes.becameOnline ? 'Is now online' : data.text,
contextMessage: 'From ' + getBasename(tab.url)
}
new Notification(notificationData, {
expirationTime: config.expirationTime,
playSound: config.playSound
}).send(tab.id)
})
break
}
}
})
})
// ----------------------------------------------------------
// notifications onClick (dismiss)
chrome.notifications.onClicked.addListener(function(notificationId) {
let tabId = Notification.cache[notificationId]
if (tabId) {
chrome.tabs.update(tabId, {
highlighted: true,
selected: true,
active: true
}, function(tab) {
chrome.windows.update(tab.windowId, { focused: true }, function() { Notification.clear(notificationId) })
})
}
})
// -----------------------------------------------------------------------------
// Extension icon popup clicked
chrome.browserAction.onClicked.addListener(function() {
let defaultURL = 'hangouts.google.com'
let validURLs = [defaultURL, 'mail.google.com', 'gmail.com', 'inbox.google.com']
configuration.get('iconClickURL', function(url) {
const basename = isValidURL(url) ? getBasename(url) : defaultURL
optionalPermissions.isGranted('tabs', function(isGranted) {
if (isGranted) {
chrome.tabs.query({ url: 'https://' + basename + '/*' }, function(tabs) {
if (tabs && tabs.length) {
chrome.tabs.update(tabs[0].id, { selected: true, active: true })
} else {
chrome.tabs.create({ url: 'https://' + basename })
}
})
} else {
chrome.tabs.create({ url: 'https://' + basename })
}
})
})
function isValidURL(url) {
return url && validURLs.includes(getBasename(url))
}
})
// ----------------------------------------------------------
// On installed/updated
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason !== 'install' && details.reason !== 'update') return
let options = {}
let callback = function() {}
Notification.clearAll()
if (details.reason === 'update') {
getCurrentTab(function(tab) {
new Notification({
title : 'Hangouts Notifications updated',
iconUrl: chrome.extension.getURL('icons/128.png'),
message: 'You might need to refresh Hangouts tabs for this extension to work'
}).send(tab.id)
})
options['justUpdated'] = 1
}
if (details.reason === 'install') {
options['firstInstall'] = true
options['justUpdated'] = 0
callback = openOptionsPage
}
chrome.storage.local.set(options, callback)
})
// ----------------------------------------------------------
// Utils
function getCurrentTab(callback) {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
callback(tabs[0])
})
}
function isUrlDisabled(url, disabledUrls) {
disabledUrls = disabledUrls || []
for (let i = 0; i < disabledUrls.length; i++) {
if (url.search(disabledUrls[i]) !== -1) return true
}
}
function getBasename(url) {
return url.replace(/https?:\/\//, '').split('/')[0] // https://mail.google.com/mail/u/0/#inbox => mail.google.com
}
function openOptionsPage() {
let optionsURL = chrome.extension.getURL('options/options.html')
chrome.tabs.create({ url: optionsURL })
}
function containsAnyName(fullName, names) {
return splitText(names).some(function(name) {
return containsAnyKeywords(name, fullName)
})
}
function containsAnyKeywords(text, keywords) {
if (! keywords) return false
text = text.toLowerCase()
return splitText(keywords).some(function(keyword) {
return text.search(keyword.toLowerCase()) !== -1
})
}
function splitText(text) {
return text.split(/,\s*/)
}