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-2017 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 * |
(...skipping 12 matching lines...) Expand all Loading... |
26 const {initAntiAdblockNotification} = require("antiadblockInit"); | 26 const {initAntiAdblockNotification} = require("antiadblockInit"); |
27 const {Prefs} = require("prefs"); | 27 const {Prefs} = require("prefs"); |
28 | 28 |
29 let activeNotification = null; | 29 let activeNotification = null; |
30 let activeButtons = null; | 30 let activeButtons = null; |
31 let defaultDisplayMethods = ["popup"]; | 31 let defaultDisplayMethods = ["popup"]; |
32 let displayMethods = Object.create(null); | 32 let displayMethods = Object.create(null); |
33 displayMethods.critical = ["icon", "notification", "popup"]; | 33 displayMethods.critical = ["icon", "notification", "popup"]; |
34 displayMethods.question = ["notification"]; | 34 displayMethods.question = ["notification"]; |
35 displayMethods.normal = ["notification"]; | 35 displayMethods.normal = ["notification"]; |
| 36 displayMethods.relentless = ["notification"]; |
36 displayMethods.information = ["icon", "popup"]; | 37 displayMethods.information = ["icon", "popup"]; |
37 | 38 |
38 function prepareNotificationIconAndPopup() | 39 function prepareNotificationIconAndPopup() |
39 { | 40 { |
40 let animateIcon = shouldDisplay("icon", activeNotification.type); | 41 let animateIcon = shouldDisplay("icon", activeNotification.type); |
41 activeNotification.onClicked = () => | 42 activeNotification.onClicked = () => |
42 { | 43 { |
43 if (animateIcon) | 44 if (animateIcon) |
44 stopIconAnimation(); | 45 stopIconAnimation(); |
45 notificationClosed(); | 46 notificationClosed(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 let maxButtons = (notificationType == "critical") ? 2 : 1; | 80 let maxButtons = (notificationType == "critical") ? 2 : 1; |
80 if (buttons.length > maxButtons) | 81 if (buttons.length > maxButtons) |
81 { | 82 { |
82 buttons = [ | 83 buttons = [ |
83 { | 84 { |
84 type: "open-all", | 85 type: "open-all", |
85 title: ext.i18n.getMessage("notification_open_all") | 86 title: ext.i18n.getMessage("notification_open_all") |
86 } | 87 } |
87 ]; | 88 ]; |
88 } | 89 } |
89 if (notificationType != "critical") | 90 if (["critical", "relentless"].indexOf(notificationType) == -1) |
90 { | 91 { |
91 buttons.push({ | 92 buttons.push({ |
92 type: "configure", | 93 type: "configure", |
93 title: ext.i18n.getMessage("notification_configure") | 94 title: ext.i18n.getMessage("notification_configure") |
94 }); | 95 }); |
95 } | 96 } |
96 } | 97 } |
97 | 98 |
98 return buttons; | 99 return buttons; |
99 } | 100 } |
(...skipping 25 matching lines...) Expand all Loading... |
125 ext.showOptions(page => | 126 ext.showOptions(page => |
126 { | 127 { |
127 page.sendMessage({ | 128 page.sendMessage({ |
128 type: "app.respond", | 129 type: "app.respond", |
129 action: "focusSection", | 130 action: "focusSection", |
130 args: ["notifications"] | 131 args: ["notifications"] |
131 }); | 132 }); |
132 }); | 133 }); |
133 break; | 134 break; |
134 case "question": | 135 case "question": |
135 NotificationStorage.triggerQuestionListeners(activeNotification.id, button
Index == 0); | 136 NotificationStorage.triggerQuestionListeners(activeNotification.id, |
| 137 buttonIndex == 0); |
136 NotificationStorage.markAsShown(activeNotification.id); | 138 NotificationStorage.markAsShown(activeNotification.id); |
137 activeNotification.onClicked(); | 139 activeNotification.onClicked(); |
138 break; | 140 break; |
139 } | 141 } |
140 } | 142 } |
141 | 143 |
142 function notificationClosed() | 144 function notificationClosed() |
143 { | 145 { |
144 activeNotification = null; | 146 activeNotification = null; |
145 } | 147 } |
146 | 148 |
147 function initChromeNotifications() | 149 function initChromeNotifications() |
148 { | 150 { |
149 // Chrome hides notifications in notification center when clicked so we need t
o clear them | 151 // Chrome hides notifications in notification center when clicked so |
| 152 // we need to clear them. |
150 function clearActiveNotification(notificationId) | 153 function clearActiveNotification(notificationId) |
151 { | 154 { |
152 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) | 155 if (activeNotification && |
| 156 activeNotification.type != "question" && |
| 157 !("links" in activeNotification)) |
153 return; | 158 return; |
154 | 159 |
155 chrome.notifications.clear(notificationId, wasCleared => | 160 chrome.notifications.clear(notificationId, wasCleared => |
156 { | 161 { |
157 if (wasCleared) | 162 if (wasCleared) |
158 notificationClosed(); | 163 notificationClosed(); |
159 }); | 164 }); |
160 } | 165 } |
161 | 166 |
162 chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex)
=> | 167 chrome.notifications.onButtonClicked.addListener( |
163 { | 168 (notificationId, buttonIndex) => |
164 notificationButtonClick(buttonIndex); | 169 { |
165 clearActiveNotification(notificationId); | 170 notificationButtonClick(buttonIndex); |
166 }); | 171 clearActiveNotification(notificationId); |
| 172 } |
| 173 ); |
167 chrome.notifications.onClicked.addListener(clearActiveNotification); | 174 chrome.notifications.onClicked.addListener(clearActiveNotification); |
168 chrome.notifications.onClosed.addListener(notificationClosed); | 175 chrome.notifications.onClosed.addListener(notificationClosed); |
169 } | 176 } |
170 | 177 |
171 function showNotification(notification) | 178 function showNotification(notification) |
172 { | 179 { |
173 if (activeNotification && activeNotification.id == notification.id) | 180 if (activeNotification && activeNotification.id == notification.id) |
174 return; | 181 return; |
175 | 182 |
176 activeNotification = notification; | 183 activeNotification = notification; |
177 if (shouldDisplay("notification", activeNotification.type)) | 184 if (shouldDisplay("notification", activeNotification.type)) |
178 { | 185 { |
179 let texts = NotificationStorage.getLocalizedTexts(notification); | 186 let texts = NotificationStorage.getLocalizedTexts(notification); |
180 let title = texts.title || ""; | 187 let title = texts.title || ""; |
181 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; | 188 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); |
182 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 189 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
183 let linkCount = (activeNotification.links || []).length; | 190 let linkCount = (activeNotification.links || []).length; |
184 | 191 |
185 if ("notifications" in chrome) | 192 if ("notifications" in chrome) |
186 { | 193 { |
187 activeButtons = getNotificationButtons(activeNotification.type, texts.mess
age); | 194 activeButtons = getNotificationButtons(activeNotification.type, |
| 195 texts.message); |
188 chrome.notifications.create("", { | 196 chrome.notifications.create("", { |
189 type: "basic", | 197 type: "basic", |
190 title: title, | 198 title, |
191 iconUrl: iconUrl, | 199 iconUrl, |
192 message: message, | 200 message, |
193 buttons: activeButtons.map(button => ({title: button.title})), | 201 buttons: activeButtons.map(button => ({title: button.title})), |
194 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically | 202 // We use the highest priority to prevent the notification |
| 203 // from closing automatically. |
| 204 priority: 2 |
195 }); | 205 }); |
196 } | 206 } |
197 else if ("Notification" in window && activeNotification.type != "question") | 207 else if ("Notification" in window && activeNotification.type != "question") |
198 { | 208 { |
199 if (linkCount > 0) | 209 if (linkCount > 0) |
200 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 210 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
201 | 211 |
202 let notification = new Notification( | 212 let widget = new Notification( |
203 title, | 213 title, |
204 { | 214 { |
205 lang: Utils.appLocale, | 215 lang: Utils.appLocale, |
206 dir: ext.i18n.getMessage("@@bidi_dir"), | 216 dir: ext.i18n.getMessage("@@bidi_dir"), |
207 body: message, | 217 body: message, |
208 icon: iconUrl | 218 icon: iconUrl |
209 } | 219 } |
210 ); | 220 ); |
211 | 221 |
212 notification.addEventListener("click", openNotificationLinks); | 222 widget.addEventListener("click", openNotificationLinks); |
213 notification.addEventListener("close", notificationClosed); | 223 widget.addEventListener("close", notificationClosed); |
214 } | 224 } |
215 else | 225 else |
216 { | 226 { |
217 let message = title + "\n" + message; | 227 message = title + "\n" + message; |
218 if (linkCount > 0) | 228 if (linkCount > 0) |
219 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 229 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
220 | 230 |
221 let approved = confirm(message); | 231 let approved = confirm(message); |
222 if (activeNotification.type == "question") | 232 if (activeNotification.type == "question") |
223 notificationButtonClick(approved ? 0 : 1); | 233 notificationButtonClick(approved ? 0 : 1); |
224 else if (approved) | 234 else if (approved) |
225 openNotificationLinks(); | 235 openNotificationLinks(); |
226 } | 236 } |
227 } | 237 } |
228 prepareNotificationIconAndPopup(); | 238 prepareNotificationIconAndPopup(); |
229 }; | 239 |
| 240 if (notification.type !== "question") |
| 241 NotificationStorage.markAsShown(notification.id); |
| 242 } |
230 | 243 |
231 /** | 244 /** |
232 * Initializes the notification system. | 245 * Initializes the notification system. |
233 */ | 246 */ |
234 exports.initNotifications = () => | 247 exports.initNotifications = () => |
235 { | 248 { |
236 if ("notifications" in chrome) | 249 if ("notifications" in chrome) |
237 initChromeNotifications(); | 250 initChromeNotifications(); |
238 initAntiAdblockNotification(); | 251 initAntiAdblockNotification(); |
239 }; | 252 }; |
(...skipping 19 matching lines...) Expand all Loading... |
259 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 272 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
260 return methods.indexOf(method) > -1; | 273 return methods.indexOf(method) > -1; |
261 }; | 274 }; |
262 | 275 |
263 ext.pages.onLoading.addListener(page => | 276 ext.pages.onLoading.addListener(page => |
264 { | 277 { |
265 NotificationStorage.showNext(stringifyURL(page.url)); | 278 NotificationStorage.showNext(stringifyURL(page.url)); |
266 }); | 279 }); |
267 | 280 |
268 NotificationStorage.addShowListener(showNotification); | 281 NotificationStorage.addShowListener(showNotification); |
OLD | NEW |