| 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 activeButtons = null; | |
| 27 let defaultDisplayMethods = ["popup"]; | 28 let defaultDisplayMethods = ["popup"]; |
| 28 let displayMethods = Object.create(null); | 29 let displayMethods = Object.create(null); |
| 29 displayMethods.critical = ["icon", "notification", "popup"]; | 30 displayMethods.critical = ["icon", "notification", "popup"]; |
| 30 displayMethods.question = ["notification"]; | 31 displayMethods.question = ["notification"]; |
| 31 displayMethods.normal = ["notification"]; | 32 displayMethods.normal = ["notification"]; |
| 32 displayMethods.information = ["icon", "popup"]; | 33 displayMethods.information = ["icon", "popup"]; |
| 33 | 34 |
| 34 // 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 |
| 35 // https://code.google.com/p/chromium/issues/detail?id=291485 | 36 // https://code.google.com/p/chromium/issues/detail?id=291485 |
| 36 let canUseChromeNotifications = (function() | 37 let canUseChromeNotifications = (function() |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 52 activeNotification.onClicked = function() | 53 activeNotification.onClicked = function() |
| 53 { | 54 { |
| 54 if (animateIcon) | 55 if (animateIcon) |
| 55 stopIconAnimation(); | 56 stopIconAnimation(); |
| 56 notificationClosed(); | 57 notificationClosed(); |
| 57 }; | 58 }; |
| 58 if (animateIcon) | 59 if (animateIcon) |
| 59 startIconAnimation(activeNotification.type); | 60 startIconAnimation(activeNotification.type); |
| 60 } | 61 } |
| 61 | 62 |
| 63 function getNotificationButtons(message) | |
| 64 { | |
| 65 activeButtons = []; | |
|
Sebastian Noack
2015/09/11 14:15:39
I find it kinda unexpected that a function that is
Thomas Greiner
2015/09/11 14:43:58
Done. The idea was to ensure that `activeButtons`
| |
| 66 if (activeNotification.type == "question") | |
| 67 { | |
| 68 activeButtons.push({ | |
| 69 type: "question", | |
| 70 title: ext.i18n.getMessage("overlay_notification_button_yes") | |
| 71 }); | |
| 72 activeButtons.push({ | |
| 73 type: "question", | |
| 74 title: ext.i18n.getMessage("overlay_notification_button_no") | |
| 75 }); | |
| 76 } | |
| 77 else | |
| 78 { | |
| 79 let regex = /<a>(.*?)<\/a>/g; | |
| 80 let match; | |
| 81 while (match = regex.exec(message)) | |
| 82 { | |
| 83 activeButtons.push({ | |
| 84 type: "link", | |
| 85 title: match[1] | |
| 86 }); | |
| 87 } | |
| 88 | |
| 89 // Chrome only allows two notification buttons so we need to fall back | |
| 90 // to a single button to open all links if there are more than two. | |
| 91 let maxButtons = (activeNotification.type == "critical") ? 2 : 1; | |
| 92 if (activeButtons.length > maxButtons) | |
| 93 { | |
| 94 activeButtons = [ | |
| 95 { | |
| 96 type: "open-all", | |
| 97 title: ext.i18n.getMessage("notification_open_all") | |
| 98 } | |
| 99 ]; | |
| 100 } | |
| 101 if (activeNotification.type != "critical") | |
| 102 { | |
| 103 activeButtons.push({ | |
| 104 type: "configure", | |
| 105 title: ext.i18n.getMessage("notification_configure") | |
| 106 }); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 return activeButtons.map(button => ({title: button.title})); | |
| 111 } | |
| 112 | |
| 62 function openNotificationLinks() | 113 function openNotificationLinks() |
| 63 { | 114 { |
| 64 if (activeNotification.links) | 115 if (activeNotification.links) |
| 65 { | 116 { |
| 66 activeNotification.links.forEach(function(link) | 117 for (let link of activeNotification.links) |
| 67 { | 118 ext.pages.open(Utils.getDocLink(link)); |
| 68 ext.windows.getLastFocused(function(win) | |
| 69 { | |
| 70 win.openTab(Utils.getDocLink(link)); | |
| 71 }); | |
| 72 }); | |
| 73 } | 119 } |
| 74 } | 120 } |
| 75 | 121 |
| 76 function notificationButtonClick(buttonIndex) | 122 function notificationButtonClick(buttonIndex) |
| 77 { | 123 { |
| 78 if (activeNotification.type == "question") | 124 switch (activeButtons[buttonIndex].type) |
| 79 { | 125 { |
| 80 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex == 0); | 126 case "link": |
| 81 NotificationStorage.markAsShown(activeNotification.id); | 127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); |
| 82 activeNotification.onClicked(); | 128 break; |
| 83 } | 129 case "open-all": |
| 84 else if (activeNotification.links && activeNotification.links[buttonIndex]) | 130 openNotificationLinks(); |
| 85 { | 131 break; |
| 86 ext.windows.getLastFocused(function(win) | 132 case "configure": |
| 87 { | 133 Prefs.notifications_showui = true; |
| 88 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); | 134 ext.showOptions(function(page) |
| 89 }); | 135 { |
| 136 page.sendMessage({ | |
| 137 type: "focus-section", | |
| 138 section: "notifications" | |
| 139 }); | |
| 140 }); | |
| 141 break; | |
| 142 case "question": | |
| 143 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0); | |
| 144 NotificationStorage.markAsShown(activeNotification.id); | |
| 145 activeNotification.onClicked(); | |
| 146 break; | |
| 90 } | 147 } |
| 91 } | 148 } |
| 92 | 149 |
| 93 function notificationClosed() | 150 function notificationClosed() |
| 94 { | 151 { |
| 95 activeNotification = null; | 152 activeNotification = null; |
| 96 } | 153 } |
| 97 | 154 |
| 98 function imgToBase64(url, callback) | 155 function imgToBase64(url, callback) |
| 99 { | 156 { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 if (activeNotification && activeNotification.id == notification.id) | 197 if (activeNotification && activeNotification.id == notification.id) |
| 141 return; | 198 return; |
| 142 | 199 |
| 143 activeNotification = notification; | 200 activeNotification = notification; |
| 144 if (shouldDisplay("notification", activeNotification.type)) | 201 if (shouldDisplay("notification", activeNotification.type)) |
| 145 { | 202 { |
| 146 let texts = NotificationStorage.getLocalizedTexts(notification); | 203 let texts = NotificationStorage.getLocalizedTexts(notification); |
| 147 let title = texts.title || ""; | 204 let title = texts.title || ""; |
| 148 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; | 205 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; |
| 149 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 206 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
| 150 let hasLinks = activeNotification.links && activeNotification.links.length > 0; | 207 let linkCount = (activeNotification.links || []).length; |
| 151 | 208 |
| 152 if (canUseChromeNotifications) | 209 if (canUseChromeNotifications) |
| 153 { | 210 { |
| 154 let opts = { | 211 let opts = { |
| 155 type: "basic", | 212 type: "basic", |
| 156 title: title, | 213 title: title, |
| 157 message: message, | 214 message: message, |
| 158 buttons: [], | 215 buttons: getNotificationButtons(texts.message), |
| 159 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically | 216 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically |
| 160 }; | 217 }; |
| 161 if (activeNotification.type == "question") | |
| 162 { | |
| 163 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")}); | |
| 164 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")}); | |
| 165 } | |
| 166 else | |
| 167 { | |
| 168 let regex = /<a>(.*?)<\/a>/g; | |
| 169 let plainMessage = texts.message || ""; | |
| 170 let match; | |
| 171 while (match = regex.exec(plainMessage)) | |
| 172 opts.buttons.push({title: match[1]}); | |
| 173 } | |
| 174 | 218 |
| 175 imgToBase64(iconUrl, function(iconData) | 219 imgToBase64(iconUrl, function(iconData) |
| 176 { | 220 { |
| 177 opts["iconUrl"] = iconData; | 221 opts.iconUrl = iconData; |
| 178 chrome.notifications.create("", opts, function() {}); | 222 chrome.notifications.create("", opts, function() {}); |
| 179 }); | 223 }); |
| 180 } | 224 } |
| 181 else if ("Notification" in window && activeNotification.type != "question") | 225 else if ("Notification" in window && activeNotification.type != "question") |
| 182 { | 226 { |
| 183 if (hasLinks) | 227 if (linkCount > 0) |
| 184 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 228 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
| 185 | 229 |
| 186 imgToBase64(iconUrl, function(iconData) | 230 imgToBase64(iconUrl, function(iconData) |
| 187 { | 231 { |
| 188 let notification = new Notification( | 232 let notification = new Notification( |
| 189 title, | 233 title, |
| 190 { | 234 { |
| 191 lang: Utils.appLocale, | 235 lang: Utils.appLocale, |
| 192 dir: ext.i18n.getMessage("@@bidi_dir"), | 236 dir: ext.i18n.getMessage("@@bidi_dir"), |
| 193 body: message, | 237 body: message, |
| 194 icon: iconData | 238 icon: iconData |
| 195 } | 239 } |
| 196 ); | 240 ); |
| 197 | 241 |
| 198 notification.addEventListener("click", openNotificationLinks); | 242 notification.addEventListener("click", openNotificationLinks); |
| 199 notification.addEventListener("close", notificationClosed); | 243 notification.addEventListener("close", notificationClosed); |
| 200 }); | 244 }); |
| 201 } | 245 } |
| 202 else | 246 else |
| 203 { | 247 { |
| 204 let message = title + "\n" + message; | 248 let message = title + "\n" + message; |
| 205 if (hasLinks) | 249 if (linkCount > 0) |
| 206 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 250 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
| 207 | 251 |
| 208 let approved = confirm(message); | 252 let approved = confirm(message); |
| 209 if (activeNotification.type == "question") | 253 if (activeNotification.type == "question") |
| 210 notificationButtonClick(approved ? 0 : 1); | 254 notificationButtonClick(approved ? 0 : 1); |
| 211 else if (approved) | 255 else if (approved) |
| 212 openNotificationLinks(); | 256 openNotificationLinks(); |
| 213 } | 257 } |
| 214 } | 258 } |
| 215 prepareNotificationIconAndPopup(); | 259 prepareNotificationIconAndPopup(); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 238 /** | 282 /** |
| 239 * Gets the active notification to be shown if any. | 283 * Gets the active notification to be shown if any. |
| 240 * | 284 * |
| 241 * @return {?object} | 285 * @return {?object} |
| 242 */ | 286 */ |
| 243 exports.getActiveNotification = function() | 287 exports.getActiveNotification = function() |
| 244 { | 288 { |
| 245 return activeNotification; | 289 return activeNotification; |
| 246 }; | 290 }; |
| 247 | 291 |
| 292 let shouldDisplay = | |
| 248 /** | 293 /** |
| 249 * Determines whether a given display method should be used for a | 294 * Determines whether a given display method should be used for a |
| 250 * specified notification type. | 295 * specified notification type. |
| 251 * | 296 * |
| 252 * @param {string} method Display method: icon, notification or popup | 297 * @param {string} method Display method: icon, notification or popup |
| 253 * @param {string} notificationType | 298 * @param {string} notificationType |
| 254 * @return {boolean} | 299 * @return {boolean} |
| 255 */ | 300 */ |
| 256 exports.shouldDisplay = shouldDisplay; | 301 exports.shouldDisplay = function(method, notificationType) |
| 257 function shouldDisplay(method, notificationType) | |
| 258 { | 302 { |
| 259 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 303 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
| 260 return methods.indexOf(method) > -1; | 304 return methods.indexOf(method) > -1; |
| 261 } | 305 } |
| 262 | 306 |
| 263 NotificationStorage.addShowListener(showNotification); | 307 NotificationStorage.addShowListener(showNotification); |
| OLD | NEW |