| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 {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 defaultDisplayMethods = ["popup"]; | |
| 28 let displayMethods = Object.create(null); | |
| 29 displayMethods.critical = ["icon", "notification", "popup"]; | |
| 30 displayMethods.question = ["notification"]; | |
| 31 displayMethods.normal = ["notification"]; | |
| 32 displayMethods.information = ["icon", "popup"]; | |
| 27 | 33 |
| 28 // Chrome on Linux does not fully support chrome.notifications until version 35 | 34 // Chrome on Linux does not fully support chrome.notifications until version 35 |
| 29 // https://code.google.com/p/chromium/issues/detail?id=291485 | 35 // https://code.google.com/p/chromium/issues/detail?id=291485 |
| 30 let canUseChromeNotifications = (function() | 36 let canUseChromeNotifications = (function() |
| 31 { | 37 { |
| 32 let info = require("info"); | 38 let info = require("info"); |
| 33 if (info.platform == "chromium" && "notifications" in chrome) | 39 if (info.platform == "chromium" && "notifications" in chrome) |
| 34 { | 40 { |
| 35 if (navigator.platform.indexOf("Linux") == -1) | 41 if (navigator.platform.indexOf("Linux") == -1) |
| 36 return true; | 42 return true; |
| 37 if (Services.vc.compare(info.applicationVersion, "35") >= 0) | 43 if (Services.vc.compare(info.applicationVersion, "35") >= 0) |
| 38 return true; | 44 return true; |
| 39 } | 45 } |
| 40 return false; | 46 return false; |
| 41 })(); | 47 })(); |
| 42 | 48 |
| 43 function prepareNotificationIconAndPopup() | 49 function prepareNotificationIconAndPopup() |
| 44 { | 50 { |
| 45 let animateIcon = (activeNotification.type == "critical" | 51 let animateIcon = shouldDisplay("icon", activeNotification.type); |
|
Sebastian Noack
2015/09/09 13:35:36
I wonder whether we should use global objects used
Thomas Greiner
2015/09/09 15:32:36
Done. However, I decided to implement a function t
| |
| 46 || activeNotification.type == "information"); | |
| 47 activeNotification.onClicked = function() | 52 activeNotification.onClicked = function() |
| 48 { | 53 { |
| 49 if (animateIcon) | 54 if (animateIcon) |
| 50 stopIconAnimation(); | 55 stopIconAnimation(); |
| 51 notificationClosed(); | 56 notificationClosed(); |
| 52 }; | 57 }; |
| 53 if (animateIcon) | 58 if (animateIcon) |
| 54 startIconAnimation(activeNotification.type); | 59 startIconAnimation(activeNotification.type); |
| 55 } | 60 } |
| 56 | 61 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 chrome.notifications.onClicked.addListener(clearActiveNotification); | 134 chrome.notifications.onClicked.addListener(clearActiveNotification); |
| 130 chrome.notifications.onClosed.addListener(notificationClosed); | 135 chrome.notifications.onClosed.addListener(notificationClosed); |
| 131 } | 136 } |
| 132 | 137 |
| 133 function showNotification(notification) | 138 function showNotification(notification) |
| 134 { | 139 { |
| 135 if (activeNotification && activeNotification.id == notification.id) | 140 if (activeNotification && activeNotification.id == notification.id) |
| 136 return; | 141 return; |
| 137 | 142 |
| 138 activeNotification = notification; | 143 activeNotification = notification; |
| 139 if (activeNotification.type == "critical" | 144 if (shouldDisplay("notification", activeNotification.type)) |
| 140 || activeNotification.type == "question" | |
| 141 || activeNotification.type == "normal") | |
| 142 { | 145 { |
| 143 let texts = NotificationStorage.getLocalizedTexts(notification); | 146 let texts = NotificationStorage.getLocalizedTexts(notification); |
| 144 let title = texts.title || ""; | 147 let title = texts.title || ""; |
| 145 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; | 148 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; |
| 146 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 149 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
| 147 let hasLinks = activeNotification.links && activeNotification.links.length > 0; | 150 let hasLinks = activeNotification.links && activeNotification.links.length > 0; |
| 148 | 151 |
| 149 if (canUseChromeNotifications) | 152 if (canUseChromeNotifications) |
| 150 { | 153 { |
| 151 let opts = { | 154 let opts = { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 /** | 238 /** |
| 236 * Gets the active notification to be shown if any. | 239 * Gets the active notification to be shown if any. |
| 237 * | 240 * |
| 238 * @return {?object} | 241 * @return {?object} |
| 239 */ | 242 */ |
| 240 exports.getActiveNotification = function() | 243 exports.getActiveNotification = function() |
| 241 { | 244 { |
| 242 return activeNotification; | 245 return activeNotification; |
| 243 }; | 246 }; |
| 244 | 247 |
| 248 /** | |
| 249 * Determines whether a given display method should be used for a | |
| 250 * specified notification type. | |
| 251 * | |
| 252 * @param {string} method Display method: icon, notification or popup | |
| 253 * @param {string} notificationType | |
| 254 * @return {boolean} | |
| 255 */ | |
| 256 exports.shouldDisplay = shouldDisplay; | |
|
Sebastian Noack
2015/09/11 13:15:46
I just realized that this way shouldDisplay is doc
| |
| 257 function shouldDisplay(method, notificationType) | |
| 258 { | |
| 259 let methods = displayMethods[notificationType] || defaultDisplayMethods; | |
| 260 return methods.indexOf(method) > -1; | |
| 261 } | |
| 262 | |
| 245 NotificationStorage.addShowListener(showNotification); | 263 NotificationStorage.addShowListener(showNotification); |
| LEFT | RIGHT |