| OLD | NEW |
| 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 {startIconAnimation, stopIconAnimation} = require("icon"); | 20 let {loadImageAsCanvas, |
| 21 startIconAnimation, stopIconAnimation} = require("icon"); |
| 21 let {Utils} = require("utils"); | 22 let {Utils} = require("utils"); |
| 22 let {Notification: NotificationStorage} = require("notification"); | 23 let {Notification: NotificationStorage} = require("notification"); |
| 23 let {stringifyURL} = require("url"); | 24 let {stringifyURL} = require("url"); |
| 24 let {initAntiAdblockNotification} = require("antiadblockInit"); | 25 let {initAntiAdblockNotification} = require("antiadblockInit"); |
| 25 | 26 |
| 26 let activeNotification = null; | 27 let activeNotification = null; |
| 27 let activeButtons = null; | 28 let activeButtons = null; |
| 28 let defaultDisplayMethods = ["popup"]; | 29 let defaultDisplayMethods = ["popup"]; |
| 29 let displayMethods = Object.create(null); | 30 let displayMethods = Object.create(null); |
| 30 displayMethods.critical = ["icon", "notification", "popup"]; | 31 displayMethods.critical = ["icon", "notification", "popup"]; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 activeNotification.onClicked(); | 146 activeNotification.onClicked(); |
| 146 break; | 147 break; |
| 147 } | 148 } |
| 148 } | 149 } |
| 149 | 150 |
| 150 function notificationClosed() | 151 function notificationClosed() |
| 151 { | 152 { |
| 152 activeNotification = null; | 153 activeNotification = null; |
| 153 } | 154 } |
| 154 | 155 |
| 155 function imgToBase64(url, callback) | |
| 156 { | |
| 157 let canvas = document.createElement("canvas"), | |
| 158 ctx = canvas.getContext("2d"), | |
| 159 img = new Image; | |
| 160 img.src = url; | |
| 161 img.onload = function() | |
| 162 { | |
| 163 canvas.height = img.height; | |
| 164 canvas.width = img.width; | |
| 165 ctx.drawImage(img, 0, 0); | |
| 166 callback(canvas.toDataURL("image/png")); | |
| 167 canvas = null; | |
| 168 }; | |
| 169 } | |
| 170 | |
| 171 function initChromeNotifications() | 156 function initChromeNotifications() |
| 172 { | 157 { |
| 173 // Chrome hides notifications in notification center when clicked so we need t
o clear them | 158 // Chrome hides notifications in notification center when clicked so we need t
o clear them |
| 174 function clearActiveNotification(notificationId) | 159 function clearActiveNotification(notificationId) |
| 175 { | 160 { |
| 176 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) | 161 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) |
| 177 return; | 162 return; |
| 178 | 163 |
| 179 chrome.notifications.clear(notificationId, function(wasCleared) | 164 chrome.notifications.clear(notificationId, function(wasCleared) |
| 180 { | 165 { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 210 { | 195 { |
| 211 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); | 196 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); |
| 212 let opts = { | 197 let opts = { |
| 213 type: "basic", | 198 type: "basic", |
| 214 title: title, | 199 title: title, |
| 215 message: message, | 200 message: message, |
| 216 buttons: activeButtons.map(button => ({title: button.title})), | 201 buttons: activeButtons.map(button => ({title: button.title})), |
| 217 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 |
| 218 }; | 203 }; |
| 219 | 204 |
| 220 imgToBase64(iconUrl, function(iconData) | 205 loadImageAsCanvas(iconUrl).then(canvas => |
| 221 { | 206 { |
| 222 opts.iconUrl = iconData; | 207 opts.iconUrl = canvas.toDataURL("image/png"); |
| 223 chrome.notifications.create("", opts, function() {}); | 208 chrome.notifications.create("", opts, function() {}); |
| 224 }); | 209 }); |
| 225 } | 210 } |
| 226 else if ("Notification" in window && activeNotification.type != "question") | 211 else if ("Notification" in window && activeNotification.type != "question") |
| 227 { | 212 { |
| 228 if (linkCount > 0) | 213 if (linkCount > 0) |
| 229 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 214 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
| 230 | 215 |
| 231 imgToBase64(iconUrl, function(iconData) | 216 loadImageAsCanvas(iconUrl).then(canvas => |
| 232 { | 217 { |
| 233 let notification = new Notification( | 218 let notification = new Notification( |
| 234 title, | 219 title, |
| 235 { | 220 { |
| 236 lang: Utils.appLocale, | 221 lang: Utils.appLocale, |
| 237 dir: ext.i18n.getMessage("@@bidi_dir"), | 222 dir: ext.i18n.getMessage("@@bidi_dir"), |
| 238 body: message, | 223 body: message, |
| 239 icon: iconData | 224 icon: canvas.toDataURL("image/png") |
| 240 } | 225 } |
| 241 ); | 226 ); |
| 242 | 227 |
| 243 notification.addEventListener("click", openNotificationLinks); | 228 notification.addEventListener("click", openNotificationLinks); |
| 244 notification.addEventListener("close", notificationClosed); | 229 notification.addEventListener("close", notificationClosed); |
| 245 }); | 230 }); |
| 246 } | 231 } |
| 247 else | 232 else |
| 248 { | 233 { |
| 249 let message = title + "\n" + message; | 234 let message = title + "\n" + message; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 * @param {string} notificationType | 284 * @param {string} notificationType |
| 300 * @return {boolean} | 285 * @return {boolean} |
| 301 */ | 286 */ |
| 302 exports.shouldDisplay = function(method, notificationType) | 287 exports.shouldDisplay = function(method, notificationType) |
| 303 { | 288 { |
| 304 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 289 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
| 305 return methods.indexOf(method) > -1; | 290 return methods.indexOf(method) > -1; |
| 306 }; | 291 }; |
| 307 | 292 |
| 308 NotificationStorage.addShowListener(showNotification); | 293 NotificationStorage.addShowListener(showNotification); |
| OLD | NEW |