Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/notificationHelper.js

Issue 29327244: Issue 3024 - Added opt-out to Chrome notifications (Closed)
Left Patch Set: Centralized button management Created Sept. 11, 2015, 12:36 p.m.
Right Patch Set: Made `getNotificationButtons` independent of external variables Created Sept. 11, 2015, 2:37 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « _locales/en_US/messages.json ('k') | options.html » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 { 51 {
52 let animateIcon = shouldDisplay("icon", activeNotification.type); 52 let animateIcon = shouldDisplay("icon", activeNotification.type);
53 activeNotification.onClicked = function() 53 activeNotification.onClicked = function()
54 { 54 {
55 if (animateIcon) 55 if (animateIcon)
56 stopIconAnimation(); 56 stopIconAnimation();
57 notificationClosed(); 57 notificationClosed();
58 }; 58 };
59 if (animateIcon) 59 if (animateIcon)
60 startIconAnimation(activeNotification.type); 60 startIconAnimation(activeNotification.type);
61 }
62
63 function getNotificationButtons(notificationType, message)
64 {
65 let buttons = [];
66 if (notificationType == "question")
67 {
68 buttons.push({
69 type: "question",
70 title: ext.i18n.getMessage("overlay_notification_button_yes")
71 });
72 buttons.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 buttons.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 = (notificationType == "critical") ? 2 : 1;
92 if (buttons.length > maxButtons)
93 {
94 buttons = [
95 {
96 type: "open-all",
97 title: ext.i18n.getMessage("notification_open_all")
98 }
99 ];
100 }
101 if (notificationType != "critical")
102 {
103 buttons.push({
104 type: "configure",
105 title: ext.i18n.getMessage("notification_configure")
106 });
107 }
108 }
109
110 return buttons;
61 } 111 }
62 112
63 function openNotificationLinks() 113 function openNotificationLinks()
64 { 114 {
65 if (activeNotification.links) 115 if (activeNotification.links)
66 { 116 {
67 for (let link of activeNotification.links) 117 for (let link of activeNotification.links)
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
68 ext.pages.open(Utils.getDocLink(link)); 118 ext.pages.open(Utils.getDocLink(link));
69 } 119 }
70 } 120 }
71 121
72 function notificationButtonClick(buttonIndex) 122 function notificationButtonClick(buttonIndex)
73 { 123 {
74 switch (activeButtons[buttonIndex].type) 124 switch (activeButtons[buttonIndex].type)
75 { 125 {
76 case "link": 126 case "link":
77 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 if (activeNotification && activeNotification.id == notification.id) 197 if (activeNotification && activeNotification.id == notification.id)
148 return; 198 return;
149 199
150 activeNotification = notification; 200 activeNotification = notification;
151 if (shouldDisplay("notification", activeNotification.type)) 201 if (shouldDisplay("notification", activeNotification.type))
152 { 202 {
153 let texts = NotificationStorage.getLocalizedTexts(notification); 203 let texts = NotificationStorage.getLocalizedTexts(notification);
154 let title = texts.title || ""; 204 let title = texts.title || "";
155 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 205 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
156 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 206 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
157 let hasLinks = activeNotification.links && activeNotification.links.length > 0; 207 let linkCount = (activeNotification.links || []).length;
Sebastian Noack 2015/09/11 13:09:55 I actually preferred the way you did it before usi
Thomas Greiner 2015/09/11 14:03:44 Done. Note that I merely reverted the changes from
158 208
159 if (canUseChromeNotifications) 209 if (canUseChromeNotifications)
160 { 210 {
161 activeButtons = []; 211 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
Sebastian Noack 2015/09/11 13:09:55 This function is becoming quite long. How about mo
Thomas Greiner 2015/09/11 14:03:44 Done. I agree, should make it easier to comprehend
162 let opts = { 212 let opts = {
163 type: "basic", 213 type: "basic",
164 title: title, 214 title: title,
165 message: message, 215 message: message,
166 buttons: [], 216 buttons: activeButtons.map(button => ({title: button.title})),
167 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 217 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
168 }; 218 };
169 if (activeNotification.type == "question")
170 {
171 activeButtons.push({
172 type: "question",
173 title: ext.i18n.getMessage("overlay_notification_button_yes")
174 });
175 activeButtons.push({
176 type: "question",
177 title: ext.i18n.getMessage("overlay_notification_button_no")
178 });
179 }
180 else
181 {
182 let regex = /<a>(.*?)<\/a>/g;
183 let match;
184 while (match = regex.exec(texts.message))
185 {
186 activeButtons.push({
187 type: "link",
188 title: match[1]
189 });
190 }
191
192 // Chrome only allows two notification buttons so we need to fall back
193 // to a single button to open all links if there are more than two.
194 let maxButtons = (activeNotification.type == "critical") ? 2 : 1;
195 if (activeButtons.length > maxButtons)
196 {
197 activeButtons = [
198 {
199 type: "open-all",
200 title: ext.i18n.getMessage("notification_open_all")
201 }
202 ];
203 }
204 if (activeNotification.type != "critical")
205 {
206 activeButtons.push({
207 type: "configure",
208 title: ext.i18n.getMessage("notification_configure")
209 });
210 }
211 }
212 219
213 imgToBase64(iconUrl, function(iconData) 220 imgToBase64(iconUrl, function(iconData)
214 { 221 {
215 opts.buttons = activeButtons.map(function(button)
Sebastian Noack 2015/09/11 13:09:55 Why not a for-of loop?
Thomas Greiner 2015/09/11 14:03:44 No particular reason. The way I use it now in the
Sebastian Noack 2015/09/11 14:15:39 I see. However, much better with an arrow function
216 {
217 return {title: button.title};
218 });
219 opts.iconUrl = iconData; 222 opts.iconUrl = iconData;
220 chrome.notifications.create("", opts, function() {}); 223 chrome.notifications.create("", opts, function() {});
221 }); 224 });
222 } 225 }
223 else if ("Notification" in window && activeNotification.type != "question") 226 else if ("Notification" in window && activeNotification.type != "question")
224 { 227 {
225 if (hasLinks) 228 if (linkCount > 0)
226 message += " " + ext.i18n.getMessage("notification_without_buttons"); 229 message += " " + ext.i18n.getMessage("notification_without_buttons");
227 230
228 imgToBase64(iconUrl, function(iconData) 231 imgToBase64(iconUrl, function(iconData)
229 { 232 {
230 let notification = new Notification( 233 let notification = new Notification(
231 title, 234 title,
232 { 235 {
233 lang: Utils.appLocale, 236 lang: Utils.appLocale,
234 dir: ext.i18n.getMessage("@@bidi_dir"), 237 dir: ext.i18n.getMessage("@@bidi_dir"),
235 body: message, 238 body: message,
236 icon: iconData 239 icon: iconData
237 } 240 }
238 ); 241 );
239 242
240 notification.addEventListener("click", openNotificationLinks); 243 notification.addEventListener("click", openNotificationLinks);
241 notification.addEventListener("close", notificationClosed); 244 notification.addEventListener("close", notificationClosed);
242 }); 245 });
243 } 246 }
244 else 247 else
245 { 248 {
246 let message = title + "\n" + message; 249 let message = title + "\n" + message;
247 if (hasLinks) 250 if (linkCount > 0)
248 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 251 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
249 252
250 let approved = confirm(message); 253 let approved = confirm(message);
251 if (activeNotification.type == "question") 254 if (activeNotification.type == "question")
252 notificationButtonClick(approved ? 0 : 1); 255 notificationButtonClick(approved ? 0 : 1);
253 else if (approved) 256 else if (approved)
254 openNotificationLinks(); 257 openNotificationLinks();
255 } 258 }
256 } 259 }
257 prepareNotificationIconAndPopup(); 260 prepareNotificationIconAndPopup();
(...skipping 22 matching lines...) Expand all
280 /** 283 /**
281 * Gets the active notification to be shown if any. 284 * Gets the active notification to be shown if any.
282 * 285 *
283 * @return {?object} 286 * @return {?object}
284 */ 287 */
285 exports.getActiveNotification = function() 288 exports.getActiveNotification = function()
286 { 289 {
287 return activeNotification; 290 return activeNotification;
288 }; 291 };
289 292
293 let shouldDisplay =
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
290 /** 294 /**
291 * Determines whether a given display method should be used for a 295 * Determines whether a given display method should be used for a
292 * specified notification type. 296 * specified notification type.
293 * 297 *
294 * @param {string} method Display method: icon, notification or popup 298 * @param {string} method Display method: icon, notification or popup
295 * @param {string} notificationType 299 * @param {string} notificationType
296 * @return {boolean} 300 * @return {boolean}
297 */ 301 */
298 exports.shouldDisplay = shouldDisplay; 302 exports.shouldDisplay = function(method, notificationType)
299 function shouldDisplay(method, notificationType)
300 { 303 {
301 let methods = displayMethods[notificationType] || defaultDisplayMethods; 304 let methods = displayMethods[notificationType] || defaultDisplayMethods;
302 return methods.indexOf(method) > -1; 305 return methods.indexOf(method) > -1;
303 } 306 }
304 307
305 NotificationStorage.addShowListener(showNotification); 308 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld