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 */ |
| 19 |
18 let {startIconAnimation, stopIconAnimation} = require("icon"); | 20 let {startIconAnimation, stopIconAnimation} = require("icon"); |
19 let {Utils} = require("utils"); | 21 let {Utils} = require("utils"); |
20 let {Notification: NotificationStorage} = require("notification"); | 22 let {Notification: NotificationStorage} = require("notification"); |
| 23 let {stringifyURL} = require("url"); |
| 24 let {initAntiAdblockNotification} = require("antiadblockInit"); |
21 | 25 |
22 let activeNotification = null; | 26 let activeNotification = null; |
23 | 27 |
24 // Chrome on Linux does not fully support chrome.notifications until version 35 | 28 // Chrome on Linux does not fully support chrome.notifications until version 35 |
25 // https://code.google.com/p/chromium/issues/detail?id=291485 | 29 // https://code.google.com/p/chromium/issues/detail?id=291485 |
26 let canUseChromeNotifications = (function() | 30 let canUseChromeNotifications = (function() |
27 { | 31 { |
28 let info = require("info"); | 32 let info = require("info"); |
29 if (info.platform == "chromium" && "notifications" in chrome) | 33 if (info.platform == "chromium" && "notifications" in chrome) |
30 { | 34 { |
31 if (navigator.platform.indexOf("Linux") == -1) | 35 if (navigator.platform.indexOf("Linux") == -1) |
32 return true; | 36 return true; |
33 if (Services.vc.compare(info.applicationVersion, "35") >= 0) | 37 if (Services.vc.compare(info.applicationVersion, "35") >= 0) |
34 return true; | 38 return true; |
35 } | 39 } |
36 return false; | 40 return false; |
37 })(); | 41 })(); |
38 | 42 |
39 function prepareNotificationIconAndPopup() | 43 function prepareNotificationIconAndPopup() |
40 { | 44 { |
41 let animateIcon = (activeNotification.type !== "question"); | 45 let animateIcon = (activeNotification.type != "question"); |
42 activeNotification.onClicked = function() | 46 activeNotification.onClicked = function() |
43 { | 47 { |
44 if (animateIcon) | 48 if (animateIcon) |
45 stopIconAnimation(); | 49 stopIconAnimation(); |
46 notificationClosed(); | 50 notificationClosed(); |
47 }; | 51 }; |
48 if (animateIcon) | 52 if (animateIcon) |
49 startIconAnimation(activeNotification.type); | 53 startIconAnimation(activeNotification.type); |
50 } | 54 } |
51 | 55 |
52 function openNotificationLinks() | 56 function openNotificationLinks() |
53 { | 57 { |
54 if (activeNotification.links) | 58 if (activeNotification.links) |
55 { | 59 { |
56 activeNotification.links.forEach(function(link) | 60 activeNotification.links.forEach(function(link) |
57 { | 61 { |
58 ext.windows.getLastFocused(function(win) | 62 ext.windows.getLastFocused(function(win) |
59 { | 63 { |
60 win.openTab(Utils.getDocLink(link)); | 64 win.openTab(Utils.getDocLink(link)); |
61 }); | 65 }); |
62 }); | 66 }); |
63 } | 67 } |
64 } | 68 } |
65 | 69 |
66 function notificationButtonClick(buttonIndex) | 70 function notificationButtonClick(buttonIndex) |
67 { | 71 { |
68 if (activeNotification.type === "question") | 72 if (activeNotification.type == "question") |
69 { | 73 { |
70 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn
dex === 0); | 74 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn
dex == 0); |
71 NotificationStorage.markAsShown(activeNotification.id); | 75 NotificationStorage.markAsShown(activeNotification.id); |
72 activeNotification.onClicked(); | 76 activeNotification.onClicked(); |
73 } | 77 } |
74 else if (activeNotification.links && activeNotification.links[buttonIndex]) | 78 else if (activeNotification.links && activeNotification.links[buttonIndex]) |
75 { | 79 { |
76 ext.windows.getLastFocused(function(win) | 80 ext.windows.getLastFocused(function(win) |
77 { | 81 { |
78 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); | 82 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); |
79 }); | 83 }); |
80 } | 84 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 122 |
119 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt
onIndex) | 123 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt
onIndex) |
120 { | 124 { |
121 notificationButtonClick(buttonIndex); | 125 notificationButtonClick(buttonIndex); |
122 clearActiveNotification(notificationId); | 126 clearActiveNotification(notificationId); |
123 }); | 127 }); |
124 chrome.notifications.onClicked.addListener(clearActiveNotification); | 128 chrome.notifications.onClicked.addListener(clearActiveNotification); |
125 chrome.notifications.onClosed.addListener(notificationClosed); | 129 chrome.notifications.onClosed.addListener(notificationClosed); |
126 } | 130 } |
127 | 131 |
128 function showNotification(notification) | 132 /** |
129 { | 133 * Initializes the notification system. |
130 if (activeNotification && activeNotification.id === notification.id) | 134 */ |
| 135 exports.initNotifications = function() |
| 136 { |
| 137 if (canUseChromeNotifications) |
| 138 initChromeNotifications(); |
| 139 initAntiAdblockNotification(); |
| 140 }; |
| 141 |
| 142 let showNextNotification = |
| 143 /** |
| 144 * Shows the next notification from the queue if any. |
| 145 * |
| 146 * @param {URL} [url] URL to match notifications to |
| 147 */ |
| 148 exports.showNextNotification = function(url) |
| 149 { |
| 150 let notification = NotificationStorage.getNextToShow(url && stringifyURL(url))
; |
| 151 if (!notification || activeNotification && activeNotification.id == notificati
on.id) |
131 return; | 152 return; |
132 | 153 |
133 activeNotification = notification; | 154 activeNotification = notification; |
134 if (activeNotification.type === "critical" || activeNotification.type === "que
stion") | 155 if (activeNotification.type == "critical" || activeNotification.type == "quest
ion") |
135 { | 156 { |
136 let texts = NotificationStorage.getLocalizedTexts(notification); | 157 let texts = NotificationStorage.getLocalizedTexts(notification); |
137 let title = texts.title || ""; | 158 let title = texts.title || ""; |
138 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; | 159 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; |
139 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); | 160 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
140 let hasLinks = activeNotification.links && activeNotification.links.length >
0; | 161 let hasLinks = activeNotification.links && activeNotification.links.length >
0; |
141 | 162 |
142 if (canUseChromeNotifications) | 163 if (canUseChromeNotifications) |
143 { | 164 { |
144 let opts = { | 165 let opts = { |
145 type: "basic", | 166 type: "basic", |
146 title: title, | 167 title: title, |
147 message: message, | 168 message: message, |
148 buttons: [], | 169 buttons: [], |
149 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically | 170 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically |
150 }; | 171 }; |
151 if (activeNotification.type === "question") | 172 if (activeNotification.type == "question") |
152 { | 173 { |
153 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt
on_yes")}); | 174 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt
on_yes")}); |
154 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt
on_no")}); | 175 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt
on_no")}); |
155 } | 176 } |
156 else | 177 else |
157 { | 178 { |
158 let regex = /<a>(.*?)<\/a>/g; | 179 let regex = /<a>(.*?)<\/a>/g; |
159 let plainMessage = texts.message || ""; | 180 let plainMessage = texts.message || ""; |
160 let match; | 181 let match; |
161 while (match = regex.exec(plainMessage)) | 182 while (match = regex.exec(plainMessage)) |
162 opts.buttons.push({title: match[1]}); | 183 opts.buttons.push({title: match[1]}); |
163 } | 184 } |
164 | 185 |
165 imgToBase64(iconUrl, function(iconData) | 186 imgToBase64(iconUrl, function(iconData) |
166 { | 187 { |
167 opts["iconUrl"] = iconData; | 188 opts["iconUrl"] = iconData; |
168 chrome.notifications.create("", opts, function() {}); | 189 chrome.notifications.create("", opts, function() {}); |
169 }); | 190 }); |
170 } | 191 } |
171 else if ("Notification" in window && activeNotification.type !== "question") | 192 else if ("Notification" in window && activeNotification.type != "question") |
172 { | 193 { |
173 if (hasLinks) | 194 if (hasLinks) |
174 message += " " + ext.i18n.getMessage("notification_without_buttons"); | 195 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
175 | 196 |
176 imgToBase64(iconUrl, function(iconData) | 197 imgToBase64(iconUrl, function(iconData) |
177 { | 198 { |
178 let notification = new Notification( | 199 let notification = new Notification( |
179 title, | 200 title, |
180 { | 201 { |
181 lang: Utils.appLocale, | 202 lang: Utils.appLocale, |
182 dir: ext.i18n.getMessage("@@bidi_dir"), | 203 dir: ext.i18n.getMessage("@@bidi_dir"), |
183 body: message, | 204 body: message, |
184 icon: iconData | 205 icon: iconData |
185 } | 206 } |
186 ); | 207 ); |
187 | 208 |
188 notification.addEventListener("click", openNotificationLinks); | 209 notification.addEventListener("click", openNotificationLinks); |
189 notification.addEventListener("close", notificationClosed); | 210 notification.addEventListener("close", notificationClosed); |
190 }); | 211 }); |
191 } | 212 } |
192 else | 213 else |
193 { | 214 { |
194 let message = title + "\n" + message; | 215 let message = title + "\n" + message; |
195 if (hasLinks) | 216 if (hasLinks) |
196 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); | 217 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
197 | 218 |
198 let approved = confirm(message); | 219 let approved = confirm(message); |
199 if (activeNotification.type === "question") | 220 if (activeNotification.type == "question") |
200 notificationButtonClick(approved ? 0 : 1); | 221 notificationButtonClick(approved ? 0 : 1); |
201 else if (approved) | 222 else if (approved) |
202 openNotificationLinks(); | 223 openNotificationLinks(); |
203 } | 224 } |
204 } | 225 } |
205 prepareNotificationIconAndPopup(); | 226 prepareNotificationIconAndPopup(); |
206 } | 227 }; |
207 | 228 |
208 setTimeout(function() | 229 setTimeout(showNextNotification, 3 * 60 * 1000); |
209 { | |
210 let notificationToShow = NotificationStorage.getNextToShow(); | |
211 if (notificationToShow) | |
212 showNotification(notificationToShow); | |
213 }, 3 * 60 * 1000); | |
LEFT | RIGHT |