Left: | ||
Right: |
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-present 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 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {startIconAnimation, stopIconAnimation} = require("icon"); | 22 const {startIconAnimation, stopIconAnimation} = require("icon"); |
23 const {Utils} = require("utils"); | 23 const {Utils} = require("utils"); |
24 const {Notification: NotificationStorage} = require("notification"); | 24 const {Notification: NotificationStorage} = require("notification"); |
25 const {stringifyURL} = require("url"); | 25 const {stringifyURL} = require("url"); |
26 const {initAntiAdblockNotification} = require("antiadblockInit"); | 26 const {initAntiAdblockNotification} = require("antiadblockInit"); |
27 const {Prefs} = require("prefs"); | 27 const {Prefs} = require("prefs"); |
28 const {showOptions} = require("options"); | |
28 | 29 |
29 let activeNotification = null; | 30 let activeNotification = null; |
30 let activeButtons = null; | 31 let activeButtons = null; |
31 let defaultDisplayMethods = ["popup"]; | 32 let defaultDisplayMethods = ["popup"]; |
32 let displayMethods = Object.create(null); | 33 let displayMethods = Object.create(null); |
33 displayMethods.critical = ["icon", "notification", "popup"]; | 34 displayMethods.critical = ["icon", "notification", "popup"]; |
34 displayMethods.question = ["notification"]; | 35 displayMethods.question = ["notification"]; |
35 displayMethods.normal = ["notification"]; | 36 displayMethods.normal = ["notification"]; |
36 displayMethods.relentless = ["notification"]; | 37 displayMethods.relentless = ["notification"]; |
37 displayMethods.information = ["icon", "popup"]; | 38 displayMethods.information = ["icon", "popup"]; |
(...skipping 11 matching lines...) Expand all Loading... | |
49 startIconAnimation(activeNotification.type); | 50 startIconAnimation(activeNotification.type); |
50 } | 51 } |
51 | 52 |
52 function getNotificationButtons(notificationType, message) | 53 function getNotificationButtons(notificationType, message) |
53 { | 54 { |
54 let buttons = []; | 55 let buttons = []; |
55 if (notificationType == "question") | 56 if (notificationType == "question") |
56 { | 57 { |
57 buttons.push({ | 58 buttons.push({ |
58 type: "question", | 59 type: "question", |
59 title: ext.i18n.getMessage("overlay_notification_button_yes") | 60 title: browser.i18n.getMessage("overlay_notification_button_yes") |
60 }); | 61 }); |
61 buttons.push({ | 62 buttons.push({ |
62 type: "question", | 63 type: "question", |
63 title: ext.i18n.getMessage("overlay_notification_button_no") | 64 title: browser.i18n.getMessage("overlay_notification_button_no") |
64 }); | 65 }); |
65 } | 66 } |
66 else | 67 else |
67 { | 68 { |
68 let regex = /<a>(.*?)<\/a>/g; | 69 let regex = /<a>(.*?)<\/a>/g; |
69 let match; | 70 let match; |
70 while (match = regex.exec(message)) | 71 while (match = regex.exec(message)) |
71 { | 72 { |
72 buttons.push({ | 73 buttons.push({ |
73 type: "link", | 74 type: "link", |
74 title: match[1] | 75 title: match[1] |
75 }); | 76 }); |
76 } | 77 } |
77 | 78 |
78 // Chrome only allows two notification buttons so we need to fall back | 79 // Chrome only allows two notification buttons so we need to fall back |
79 // to a single button to open all links if there are more than two. | 80 // to a single button to open all links if there are more than two. |
80 let maxButtons = (notificationType == "critical") ? 2 : 1; | 81 let maxButtons = (notificationType == "critical") ? 2 : 1; |
81 if (buttons.length > maxButtons) | 82 if (buttons.length > maxButtons) |
82 { | 83 { |
83 buttons = [ | 84 buttons = [ |
84 { | 85 { |
85 type: "open-all", | 86 type: "open-all", |
86 title: ext.i18n.getMessage("notification_open_all") | 87 title: browser.i18n.getMessage("notification_open_all") |
87 } | 88 } |
88 ]; | 89 ]; |
89 } | 90 } |
90 if (!["critical", "relentless"].includes(notificationType)) | 91 if (!["critical", "relentless"].includes(notificationType)) |
91 { | 92 { |
92 buttons.push({ | 93 buttons.push({ |
93 type: "configure", | 94 type: "configure", |
94 title: ext.i18n.getMessage("notification_configure") | 95 title: browser.i18n.getMessage("notification_configure") |
95 }); | 96 }); |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 return buttons; | 100 return buttons; |
100 } | 101 } |
101 | 102 |
102 function openNotificationLinks() | 103 function openNotificationLinks() |
103 { | 104 { |
104 if (activeNotification.links) | 105 if (activeNotification.links) |
105 { | 106 { |
106 for (let link of activeNotification.links) | 107 for (let link of activeNotification.links) |
107 ext.pages.open(Utils.getDocLink(link)); | 108 browser.tabs.create({url: Utils.getDocLink(link)}); |
108 } | 109 } |
109 } | 110 } |
110 | 111 |
111 function notificationButtonClick(buttonIndex) | 112 function notificationButtonClick(buttonIndex) |
112 { | 113 { |
113 if (!(activeButtons && buttonIndex in activeButtons)) | 114 if (!(activeButtons && buttonIndex in activeButtons)) |
114 return; | 115 return; |
115 | 116 |
116 switch (activeButtons[buttonIndex].type) | 117 switch (activeButtons[buttonIndex].type) |
117 { | 118 { |
118 case "link": | 119 case "link": |
119 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); | 120 browser.tabs.create({ |
121 url: Utils.getDocLink(activeNotification.links[buttonIndex]) | |
122 }); | |
120 break; | 123 break; |
121 case "open-all": | 124 case "open-all": |
122 openNotificationLinks(); | 125 openNotificationLinks(); |
123 break; | 126 break; |
124 case "configure": | 127 case "configure": |
125 Prefs.notifications_showui = true; | 128 Prefs.notifications_showui = true; |
126 ext.showOptions(page => | 129 showOptions(page => |
127 { | 130 { |
128 page.sendMessage({ | 131 page.sendMessage({ |
129 type: "app.respond", | 132 type: "app.respond", |
130 action: "focusSection", | 133 action: "focusSection", |
131 args: ["notifications"] | 134 args: ["notifications"] |
132 }); | 135 }); |
133 }); | 136 }); |
134 break; | 137 break; |
135 case "question": | 138 case "question": |
136 NotificationStorage.triggerQuestionListeners(activeNotification.id, | 139 NotificationStorage.triggerQuestionListeners(activeNotification.id, |
(...skipping 13 matching lines...) Expand all Loading... | |
150 { | 153 { |
151 // Chrome hides notifications in notification center when clicked so | 154 // Chrome hides notifications in notification center when clicked so |
152 // we need to clear them. | 155 // we need to clear them. |
153 function clearActiveNotification(notificationId) | 156 function clearActiveNotification(notificationId) |
154 { | 157 { |
155 if (activeNotification && | 158 if (activeNotification && |
156 activeNotification.type != "question" && | 159 activeNotification.type != "question" && |
157 !("links" in activeNotification)) | 160 !("links" in activeNotification)) |
158 return; | 161 return; |
159 | 162 |
160 chrome.notifications.clear(notificationId, wasCleared => | 163 browser.notifications.clear(notificationId, wasCleared => |
161 { | 164 { |
162 if (wasCleared) | 165 if (wasCleared) |
163 notificationClosed(); | 166 notificationClosed(); |
164 }); | 167 }); |
165 } | 168 } |
166 | 169 |
167 chrome.notifications.onButtonClicked.addListener( | 170 browser.notifications.onButtonClicked.addListener( |
168 (notificationId, buttonIndex) => | 171 (notificationId, buttonIndex) => |
169 { | 172 { |
170 notificationButtonClick(buttonIndex); | 173 notificationButtonClick(buttonIndex); |
171 clearActiveNotification(notificationId); | 174 clearActiveNotification(notificationId); |
172 } | 175 } |
173 ); | 176 ); |
174 chrome.notifications.onClicked.addListener(clearActiveNotification); | 177 browser.notifications.onClicked.addListener(clearActiveNotification); |
175 chrome.notifications.onClosed.addListener(notificationClosed); | 178 browser.notifications.onClosed.addListener(notificationClosed); |
176 } | 179 } |
177 | 180 |
178 function showNotification(notification) | 181 function showNotification(notification) |
179 { | 182 { |
180 if (activeNotification && activeNotification.id == notification.id) | 183 if (activeNotification && activeNotification.id == notification.id) |
181 return; | 184 return; |
182 | 185 |
183 activeNotification = notification; | 186 activeNotification = notification; |
184 if (shouldDisplay("notification", activeNotification.type)) | 187 if (shouldDisplay("notification", activeNotification.type)) |
185 { | 188 { |
186 let texts = NotificationStorage.getLocalizedTexts(notification); | 189 let texts = NotificationStorage.getLocalizedTexts(notification); |
187 let title = texts.title || ""; | 190 let title = texts.title || ""; |
188 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); | 191 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); |
189 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 192 let iconUrl = browser.extension.getURL("icons/detailed/abp-128.png"); |
190 let linkCount = (activeNotification.links || []).length; | 193 let linkCount = (activeNotification.links || []).length; |
191 | 194 |
192 if ("notifications" in chrome) | 195 if ("notifications" in browser) |
193 { | 196 { |
194 activeButtons = getNotificationButtons(activeNotification.type, | 197 activeButtons = getNotificationButtons(activeNotification.type, |
195 texts.message); | 198 texts.message); |
196 let options = { | 199 let notificationOptions = { |
197 type: "basic", | 200 type: "basic", |
198 title, | 201 title, |
199 iconUrl, | 202 iconUrl, |
200 message, | 203 message, |
201 buttons: activeButtons.map(button => ({title: button.title})), | 204 buttons: activeButtons.map(button => ({title: button.title})), |
202 // We use the highest priority to prevent the notification | 205 // We use the highest priority to prevent the notification |
203 // from closing automatically. | 206 // from closing automatically. |
204 priority: 2 | 207 priority: 2 |
205 }; | 208 }; |
206 chrome.notifications.create("", options, () => | 209 browser.notifications.create("", notificationOptions, () => |
207 { | 210 { |
208 if (chrome.runtime.lastError && chrome.runtime.lastError.message === ` | 211 // Opera does not support the addtition of buttons to notifications. |
Sebastian Noack
2017/11/01 18:45:03
Typo: addtition
| |
209 Adding buttons to notifications is not supported.`) | 212 // Question type notfications always include buttons. |
Sebastian Noack
2017/10/21 21:04:28
I would skip the check for the error message. If i
Jon Sonesen
2017/10/30 22:41:41
Done.
| |
213 if (browser.runtime.lastError && activeNotification.type != "question") | |
210 { | 214 { |
211 delete options.buttons; | 215 delete notificationOptions.buttons; |
Jon Sonesen
2017/10/19 21:59:17
perhaps it is better to move this down or put all
Sebastian Noack
2017/10/21 21:04:28
Yeah, I guess this operation makes more sense in t
Jon Sonesen
2017/10/30 22:41:41
Done.
| |
212 if (activeNotification.type != "question") | 216 browser.notifications.create("", notificationOptions); |
213 chrome.notifications.create("", options); | |
214 } | 217 } |
215 }); | 218 }); |
216 } | 219 } |
217 else if ("Notification" in window && activeNotification.type != "question") | 220 else if ("Notification" in window && activeNotification.type != "question") |
218 { | 221 { |
219 if (linkCount > 0) | 222 if (linkCount > 0) |
220 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 223 { |
224 message += " " + browser.i18n.getMessage( | |
225 "notification_without_buttons" | |
226 ); | |
227 } | |
221 | 228 |
222 let widget = new Notification( | 229 let widget = new Notification( |
223 title, | 230 title, |
224 { | 231 { |
225 lang: Utils.appLocale, | 232 lang: Utils.appLocale, |
226 dir: Utils.readingDirection, | 233 dir: Utils.readingDirection, |
227 body: message, | 234 body: message, |
228 icon: iconUrl | 235 icon: iconUrl |
229 } | 236 } |
230 ); | 237 ); |
231 | 238 |
232 widget.addEventListener("click", openNotificationLinks); | 239 widget.addEventListener("click", openNotificationLinks); |
233 widget.addEventListener("close", notificationClosed); | 240 widget.addEventListener("close", notificationClosed); |
234 } | 241 } |
235 else | 242 else |
236 { | 243 { |
237 message = title + "\n" + message; | 244 message = title + "\n" + message; |
238 if (linkCount > 0) | 245 if (linkCount > 0) |
239 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 246 { |
247 message += "\n\n" + browser.i18n.getMessage( | |
248 "notification_with_buttons" | |
249 ); | |
250 } | |
240 | 251 |
241 let approved = confirm(message); | 252 let approved = confirm(message); |
242 if (activeNotification.type == "question") | 253 if (activeNotification.type == "question") |
243 notificationButtonClick(approved ? 0 : 1); | 254 notificationButtonClick(approved ? 0 : 1); |
244 else if (approved) | 255 else if (approved) |
245 openNotificationLinks(); | 256 openNotificationLinks(); |
246 } | 257 } |
247 } | 258 } |
248 prepareNotificationIconAndPopup(); | 259 prepareNotificationIconAndPopup(); |
249 | 260 |
250 if (notification.type !== "question") | 261 if (notification.type !== "question") |
251 NotificationStorage.markAsShown(notification.id); | 262 NotificationStorage.markAsShown(notification.id); |
252 } | 263 } |
253 | 264 |
254 /** | 265 /** |
255 * Initializes the notification system. | 266 * Initializes the notification system. |
256 */ | 267 */ |
257 exports.initNotifications = () => | 268 exports.initNotifications = () => |
258 { | 269 { |
259 if ("notifications" in chrome) | 270 if ("notifications" in browser) |
260 initChromeNotifications(); | 271 initChromeNotifications(); |
261 initAntiAdblockNotification(); | 272 initAntiAdblockNotification(); |
262 }; | 273 }; |
263 | 274 |
264 /** | 275 /** |
265 * Gets the active notification to be shown if any. | 276 * Gets the active notification to be shown if any. |
266 * | 277 * |
267 * @return {?object} | 278 * @return {?object} |
268 */ | 279 */ |
269 exports.getActiveNotification = () => activeNotification; | 280 exports.getActiveNotification = () => activeNotification; |
(...skipping 12 matching lines...) Expand all Loading... | |
282 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 293 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
283 return methods.includes(method); | 294 return methods.includes(method); |
284 }; | 295 }; |
285 | 296 |
286 ext.pages.onLoading.addListener(page => | 297 ext.pages.onLoading.addListener(page => |
287 { | 298 { |
288 NotificationStorage.showNext(stringifyURL(page.url)); | 299 NotificationStorage.showNext(stringifyURL(page.url)); |
289 }); | 300 }); |
290 | 301 |
291 NotificationStorage.addShowListener(showNotification); | 302 NotificationStorage.addShowListener(showNotification); |
LEFT | RIGHT |