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-2016 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 {loadImage, startIconAnimation, stopIconAnimation} = require("icon"); | 20 let {startIconAnimation, stopIconAnimation} = require("icon"); |
21 let {Utils} = require("utils"); | 21 let {Utils} = require("utils"); |
22 let {Notification: NotificationStorage} = require("notification"); | 22 let {Notification: NotificationStorage} = require("notification"); |
23 let {stringifyURL} = require("url"); | 23 let {stringifyURL} = require("url"); |
24 let {initAntiAdblockNotification} = require("antiadblockInit"); | 24 let {initAntiAdblockNotification} = require("antiadblockInit"); |
25 | 25 |
26 let activeNotification = null; | 26 let activeNotification = null; |
27 let activeButtons = null; | 27 let activeButtons = null; |
28 let defaultDisplayMethods = ["popup"]; | 28 let defaultDisplayMethods = ["popup"]; |
29 let displayMethods = Object.create(null); | 29 let displayMethods = Object.create(null); |
30 displayMethods.critical = ["icon", "notification", "popup"]; | 30 displayMethods.critical = ["icon", "notification", "popup"]; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 activeNotification.onClicked(); | 145 activeNotification.onClicked(); |
146 break; | 146 break; |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 function notificationClosed() | 150 function notificationClosed() |
151 { | 151 { |
152 activeNotification = null; | 152 activeNotification = null; |
153 } | 153 } |
154 | 154 |
155 function imageToBase64(url) | |
156 { | |
157 return loadImage(url).then(image => | |
158 { | |
159 let canvas = document.createElement("canvas"); | |
160 let context = canvas.getContext("2d"); | |
161 canvas.height = image.height; | |
162 canvas.width = image.width; | |
163 context.drawImage(image, 0, 0); | |
164 return canvas.toDataURL("image/png"); | |
165 }); | |
166 } | |
167 | |
168 function initChromeNotifications() | 155 function initChromeNotifications() |
169 { | 156 { |
170 // Chrome hides notifications in notification center when clicked so we need t
o clear them | 157 // Chrome hides notifications in notification center when clicked so we need t
o clear them |
171 function clearActiveNotification(notificationId) | 158 function clearActiveNotification(notificationId) |
172 { | 159 { |
173 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) | 160 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) |
174 return; | 161 return; |
175 | 162 |
176 chrome.notifications.clear(notificationId, function(wasCleared) | 163 chrome.notifications.clear(notificationId, function(wasCleared) |
177 { | 164 { |
(...skipping 21 matching lines...) Expand all Loading... |
199 { | 186 { |
200 let texts = NotificationStorage.getLocalizedTexts(notification); | 187 let texts = NotificationStorage.getLocalizedTexts(notification); |
201 let title = texts.title || ""; | 188 let title = texts.title || ""; |
202 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; | 189 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; |
203 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 190 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
204 let linkCount = (activeNotification.links || []).length; | 191 let linkCount = (activeNotification.links || []).length; |
205 | 192 |
206 if (canUseChromeNotifications) | 193 if (canUseChromeNotifications) |
207 { | 194 { |
208 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); | 195 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); |
209 let opts = { | 196 chrome.notifications.create("", { |
210 type: "basic", | 197 type: "basic", |
211 title: title, | 198 title: title, |
| 199 iconUrl: iconUrl, |
212 message: message, | 200 message: message, |
213 buttons: activeButtons.map(button => ({title: button.title})), | 201 buttons: activeButtons.map(button => ({title: button.title})), |
214 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically | 202 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically |
215 }; | |
216 | |
217 imageToBase64(iconUrl).then(iconData => | |
218 { | |
219 opts.iconUrl = iconData; | |
220 chrome.notifications.create("", opts, function() {}); | |
221 }); | 203 }); |
222 } | 204 } |
223 else if ("Notification" in window && activeNotification.type != "question") | 205 else if ("Notification" in window && activeNotification.type != "question") |
224 { | 206 { |
225 if (linkCount > 0) | 207 if (linkCount > 0) |
226 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 208 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
227 | 209 |
228 imageToBase64(iconUrl).then(iconData => | 210 let notification = new Notification( |
229 { | 211 title, |
230 let notification = new Notification( | 212 { |
231 title, | 213 lang: Utils.appLocale, |
232 { | 214 dir: ext.i18n.getMessage("@@bidi_dir"), |
233 lang: Utils.appLocale, | 215 body: message, |
234 dir: ext.i18n.getMessage("@@bidi_dir"), | 216 icon: iconUrl |
235 body: message, | 217 } |
236 icon: iconData | 218 ); |
237 } | 219 |
238 ); | 220 notification.addEventListener("click", openNotificationLinks); |
239 | 221 notification.addEventListener("close", notificationClosed); |
240 notification.addEventListener("click", openNotificationLinks); | |
241 notification.addEventListener("close", notificationClosed); | |
242 }); | |
243 } | 222 } |
244 else | 223 else |
245 { | 224 { |
246 let message = title + "\n" + message; | 225 let message = title + "\n" + message; |
247 if (linkCount > 0) | 226 if (linkCount > 0) |
248 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 227 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
249 | 228 |
250 let approved = confirm(message); | 229 let approved = confirm(message); |
251 if (activeNotification.type == "question") | 230 if (activeNotification.type == "question") |
252 notificationButtonClick(approved ? 0 : 1); | 231 notificationButtonClick(approved ? 0 : 1); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 * @param {string} notificationType | 275 * @param {string} notificationType |
297 * @return {boolean} | 276 * @return {boolean} |
298 */ | 277 */ |
299 exports.shouldDisplay = function(method, notificationType) | 278 exports.shouldDisplay = function(method, notificationType) |
300 { | 279 { |
301 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 280 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
302 return methods.indexOf(method) > -1; | 281 return methods.indexOf(method) > -1; |
303 }; | 282 }; |
304 | 283 |
305 NotificationStorage.addShowListener(showNotification); | 284 NotificationStorage.addShowListener(showNotification); |
LEFT | RIGHT |