LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module notificationHelper */ | 18 /** @module notificationHelper */ |
19 | 19 |
20 let {startIconAnimation, stopIconAnimation} = require("icon"); | 20 "use strict"; |
21 let {Utils} = require("utils"); | 21 |
22 let {Notification: NotificationStorage} = require("notification"); | 22 const {startIconAnimation, stopIconAnimation} = require("icon"); |
23 let {stringifyURL} = require("url"); | 23 const {Utils} = require("utils"); |
24 let {initAntiAdblockNotification} = require("antiadblockInit"); | 24 const {Notification: NotificationStorage} = require("notification"); |
| 25 const {stringifyURL} = require("url"); |
| 26 const {initAntiAdblockNotification} = require("antiadblockInit"); |
| 27 const {Prefs} = require("prefs"); |
25 | 28 |
26 let activeNotification = null; | 29 let activeNotification = null; |
27 let activeButtons = null; | 30 let activeButtons = null; |
28 let defaultDisplayMethods = ["popup"]; | 31 let defaultDisplayMethods = ["popup"]; |
29 let displayMethods = Object.create(null); | 32 let displayMethods = Object.create(null); |
30 displayMethods.critical = ["icon", "notification", "popup"]; | 33 displayMethods.critical = ["icon", "notification", "popup"]; |
31 displayMethods.question = ["notification"]; | 34 displayMethods.question = ["notification"]; |
32 displayMethods.normal = ["notification"]; | 35 displayMethods.normal = ["notification"]; |
| 36 displayMethods.relentless = ["notification"]; |
33 displayMethods.information = ["icon", "popup"]; | 37 displayMethods.information = ["icon", "popup"]; |
34 | 38 |
35 // Chrome on Linux does not fully support chrome.notifications until version 35 | |
36 // https://code.google.com/p/chromium/issues/detail?id=291485 | |
37 let canUseChromeNotifications = (function() | |
38 { | |
39 let info = require("info"); | |
40 if (typeof chrome == "object" && "notifications" in chrome) | |
41 { | |
42 if (navigator.platform.indexOf("Linux") == -1) | |
43 return true; | |
44 if (Services.vc.compare(info.applicationVersion, "35") >= 0) | |
45 return true; | |
46 } | |
47 return false; | |
48 })(); | |
49 | |
50 function prepareNotificationIconAndPopup() | 39 function prepareNotificationIconAndPopup() |
51 { | 40 { |
52 let animateIcon = shouldDisplay("icon", activeNotification.type); | 41 let animateIcon = shouldDisplay("icon", activeNotification.type); |
53 activeNotification.onClicked = function() | 42 activeNotification.onClicked = () => |
54 { | 43 { |
55 if (animateIcon) | 44 if (animateIcon) |
56 stopIconAnimation(); | 45 stopIconAnimation(); |
57 notificationClosed(); | 46 notificationClosed(); |
58 }; | 47 }; |
59 if (animateIcon) | 48 if (animateIcon) |
60 startIconAnimation(activeNotification.type); | 49 startIconAnimation(activeNotification.type); |
61 } | 50 } |
62 | 51 |
63 function getNotificationButtons(notificationType, message) | 52 function getNotificationButtons(notificationType, message) |
(...skipping 27 matching lines...) Expand all Loading... |
91 let maxButtons = (notificationType == "critical") ? 2 : 1; | 80 let maxButtons = (notificationType == "critical") ? 2 : 1; |
92 if (buttons.length > maxButtons) | 81 if (buttons.length > maxButtons) |
93 { | 82 { |
94 buttons = [ | 83 buttons = [ |
95 { | 84 { |
96 type: "open-all", | 85 type: "open-all", |
97 title: ext.i18n.getMessage("notification_open_all") | 86 title: ext.i18n.getMessage("notification_open_all") |
98 } | 87 } |
99 ]; | 88 ]; |
100 } | 89 } |
101 if (notificationType != "critical") | 90 if (!["critical", "relentless"].includes(notificationType)) |
102 { | 91 { |
103 buttons.push({ | 92 buttons.push({ |
104 type: "configure", | 93 type: "configure", |
105 title: ext.i18n.getMessage("notification_configure") | 94 title: ext.i18n.getMessage("notification_configure") |
106 }); | 95 }); |
107 } | 96 } |
108 } | 97 } |
109 | 98 |
110 return buttons; | 99 return buttons; |
111 } | 100 } |
112 | 101 |
113 function openNotificationLinks() | 102 function openNotificationLinks() |
114 { | 103 { |
115 if (activeNotification.links) | 104 if (activeNotification.links) |
116 { | 105 { |
117 for (let link of activeNotification.links) | 106 for (let link of activeNotification.links) |
118 ext.pages.open(Utils.getDocLink(link)); | 107 ext.pages.open(Utils.getDocLink(link)); |
119 } | 108 } |
120 } | 109 } |
121 | 110 |
122 function notificationButtonClick(buttonIndex) | 111 function notificationButtonClick(buttonIndex) |
123 { | 112 { |
| 113 if (!(activeButtons && buttonIndex in activeButtons)) |
| 114 return; |
| 115 |
124 switch (activeButtons[buttonIndex].type) | 116 switch (activeButtons[buttonIndex].type) |
125 { | 117 { |
126 case "link": | 118 case "link": |
127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); | 119 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); |
128 break; | 120 break; |
129 case "open-all": | 121 case "open-all": |
130 openNotificationLinks(); | 122 openNotificationLinks(); |
131 break; | 123 break; |
132 case "configure": | 124 case "configure": |
133 Prefs.notifications_showui = true; | 125 Prefs.notifications_showui = true; |
134 ext.showOptions(function(page) | 126 ext.showOptions(page => |
135 { | 127 { |
136 page.sendMessage({ | 128 page.sendMessage({ |
137 type: "app.respond", | 129 type: "app.respond", |
138 action: "focusSection", | 130 action: "focusSection", |
139 args: ["notifications"] | 131 args: ["notifications"] |
140 }); | 132 }); |
141 }); | 133 }); |
142 break; | 134 break; |
143 case "question": | 135 case "question": |
144 NotificationStorage.triggerQuestionListeners(activeNotification.id, button
Index == 0); | 136 NotificationStorage.triggerQuestionListeners(activeNotification.id, |
| 137 buttonIndex == 0); |
145 NotificationStorage.markAsShown(activeNotification.id); | 138 NotificationStorage.markAsShown(activeNotification.id); |
146 activeNotification.onClicked(); | 139 activeNotification.onClicked(); |
147 break; | 140 break; |
148 } | 141 } |
149 } | 142 } |
150 | 143 |
151 function notificationClosed() | 144 function notificationClosed() |
152 { | 145 { |
153 activeNotification = null; | 146 activeNotification = null; |
154 } | 147 } |
155 | 148 |
156 function initChromeNotifications() | 149 function initChromeNotifications() |
157 { | 150 { |
158 // Chrome hides notifications in notification center when clicked so we need t
o clear them | 151 // Chrome hides notifications in notification center when clicked so |
| 152 // we need to clear them. |
159 function clearActiveNotification(notificationId) | 153 function clearActiveNotification(notificationId) |
160 { | 154 { |
161 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) | 155 if (activeNotification && |
| 156 activeNotification.type != "question" && |
| 157 !("links" in activeNotification)) |
162 return; | 158 return; |
163 | 159 |
164 chrome.notifications.clear(notificationId, function(wasCleared) | 160 chrome.notifications.clear(notificationId, wasCleared => |
165 { | 161 { |
166 if (wasCleared) | 162 if (wasCleared) |
167 notificationClosed(); | 163 notificationClosed(); |
168 }); | 164 }); |
169 } | 165 } |
170 | 166 |
171 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt
onIndex) | 167 chrome.notifications.onButtonClicked.addListener( |
172 { | 168 (notificationId, buttonIndex) => |
173 notificationButtonClick(buttonIndex); | 169 { |
174 clearActiveNotification(notificationId); | 170 notificationButtonClick(buttonIndex); |
175 }); | 171 clearActiveNotification(notificationId); |
| 172 } |
| 173 ); |
176 chrome.notifications.onClicked.addListener(clearActiveNotification); | 174 chrome.notifications.onClicked.addListener(clearActiveNotification); |
177 chrome.notifications.onClosed.addListener(notificationClosed); | 175 chrome.notifications.onClosed.addListener(notificationClosed); |
178 } | 176 } |
179 | 177 |
180 function showNotification(notification) | 178 function showNotification(notification) |
181 { | 179 { |
182 if (activeNotification && activeNotification.id == notification.id) | 180 if (activeNotification && activeNotification.id == notification.id) |
183 return; | 181 return; |
184 | 182 |
185 activeNotification = notification; | 183 activeNotification = notification; |
186 if (shouldDisplay("notification", activeNotification.type)) | 184 if (shouldDisplay("notification", activeNotification.type)) |
187 { | 185 { |
188 let texts = NotificationStorage.getLocalizedTexts(notification); | 186 let texts = NotificationStorage.getLocalizedTexts(notification); |
189 let title = texts.title || ""; | 187 let title = texts.title || ""; |
190 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; | 188 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); |
191 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 189 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
192 let linkCount = (activeNotification.links || []).length; | 190 let linkCount = (activeNotification.links || []).length; |
193 | 191 |
194 if (canUseChromeNotifications) | 192 if ("notifications" in chrome) |
195 { | 193 { |
196 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); | 194 activeButtons = getNotificationButtons(activeNotification.type, |
| 195 texts.message); |
197 chrome.notifications.create("", { | 196 chrome.notifications.create("", { |
198 type: "basic", | 197 type: "basic", |
199 title: title, | 198 title, |
200 iconUrl: iconUrl, | 199 iconUrl, |
201 message: message, | 200 message, |
202 buttons: activeButtons.map(button => ({title: button.title})), | 201 buttons: activeButtons.map(button => ({title: button.title})), |
203 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically | 202 // We use the highest priority to prevent the notification |
| 203 // from closing automatically. |
| 204 priority: 2 |
204 }); | 205 }); |
205 } | 206 } |
206 else if ("Notification" in window && activeNotification.type != "question") | 207 else if ("Notification" in window && activeNotification.type != "question") |
207 { | 208 { |
208 if (linkCount > 0) | 209 if (linkCount > 0) |
209 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 210 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
210 | 211 |
211 let notification = new Notification( | 212 let widget = new Notification( |
212 title, | 213 title, |
213 { | 214 { |
214 lang: Utils.appLocale, | 215 lang: Utils.appLocale, |
215 dir: Utils.readingDirection, | 216 dir: Utils.readingDirection, |
216 body: message, | 217 body: message, |
217 icon: iconUrl | 218 icon: iconUrl |
218 } | 219 } |
219 ); | 220 ); |
220 | 221 |
221 notification.addEventListener("click", openNotificationLinks); | 222 widget.addEventListener("click", openNotificationLinks); |
222 notification.addEventListener("close", notificationClosed); | 223 widget.addEventListener("close", notificationClosed); |
223 } | 224 } |
224 else | 225 else |
225 { | 226 { |
226 let message = title + "\n" + message; | 227 message = title + "\n" + message; |
227 if (linkCount > 0) | 228 if (linkCount > 0) |
228 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 229 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
229 | 230 |
230 let approved = confirm(message); | 231 let approved = confirm(message); |
231 if (activeNotification.type == "question") | 232 if (activeNotification.type == "question") |
232 notificationButtonClick(approved ? 0 : 1); | 233 notificationButtonClick(approved ? 0 : 1); |
233 else if (approved) | 234 else if (approved) |
234 openNotificationLinks(); | 235 openNotificationLinks(); |
235 } | 236 } |
236 } | 237 } |
237 prepareNotificationIconAndPopup(); | 238 prepareNotificationIconAndPopup(); |
238 }; | 239 |
| 240 if (notification.type !== "question") |
| 241 NotificationStorage.markAsShown(notification.id); |
| 242 } |
239 | 243 |
240 /** | 244 /** |
241 * Initializes the notification system. | 245 * Initializes the notification system. |
242 */ | 246 */ |
243 exports.initNotifications = function() | 247 exports.initNotifications = () => |
244 { | 248 { |
245 if (canUseChromeNotifications) | 249 if ("notifications" in chrome) |
246 initChromeNotifications(); | 250 initChromeNotifications(); |
247 initAntiAdblockNotification(); | 251 initAntiAdblockNotification(); |
248 }; | 252 }; |
249 | 253 |
250 /** | 254 /** |
251 * Gets the active notification to be shown if any. | 255 * Gets the active notification to be shown if any. |
252 * | 256 * |
253 * @return {?object} | 257 * @return {?object} |
254 */ | 258 */ |
255 exports.getActiveNotification = function() | 259 exports.getActiveNotification = () => activeNotification; |
256 { | |
257 return activeNotification; | |
258 }; | |
259 | 260 |
260 let shouldDisplay = | 261 let shouldDisplay = |
261 /** | 262 /** |
262 * Determines whether a given display method should be used for a | 263 * Determines whether a given display method should be used for a |
263 * specified notification type. | 264 * specified notification type. |
264 * | 265 * |
265 * @param {string} method Display method: icon, notification or popup | 266 * @param {string} method Display method: icon, notification or popup |
266 * @param {string} notificationType | 267 * @param {string} notificationType |
267 * @return {boolean} | 268 * @return {boolean} |
268 */ | 269 */ |
269 exports.shouldDisplay = function(method, notificationType) | 270 exports.shouldDisplay = (method, notificationType) => |
270 { | 271 { |
271 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 272 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
272 return methods.indexOf(method) > -1; | 273 return methods.includes(method); |
273 }; | 274 }; |
274 | 275 |
275 ext.pages.onLoading.addListener(page => | 276 ext.pages.onLoading.addListener(page => |
276 { | 277 { |
277 NotificationStorage.showNext(stringifyURL(page.url)); | 278 NotificationStorage.showNext(stringifyURL(page.url)); |
278 }); | 279 }); |
279 | 280 |
280 NotificationStorage.addShowListener(showNotification); | 281 NotificationStorage.addShowListener(showNotification); |
LEFT | RIGHT |