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