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

Side by Side Diff: lib/notificationHelper.js

Issue 29570614: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Update adblockplusui dependency Created Oct. 17, 2017, 1:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/io.js ('k') | lib/options.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 startIconAnimation(activeNotification.type); 50 startIconAnimation(activeNotification.type);
51 } 51 }
52 52
53 function getNotificationButtons(notificationType, message) 53 function getNotificationButtons(notificationType, message)
54 { 54 {
55 let buttons = []; 55 let buttons = [];
56 if (notificationType == "question") 56 if (notificationType == "question")
57 { 57 {
58 buttons.push({ 58 buttons.push({
59 type: "question", 59 type: "question",
60 title: chrome.i18n.getMessage("overlay_notification_button_yes") 60 title: browser.i18n.getMessage("overlay_notification_button_yes")
61 }); 61 });
62 buttons.push({ 62 buttons.push({
63 type: "question", 63 type: "question",
64 title: chrome.i18n.getMessage("overlay_notification_button_no") 64 title: browser.i18n.getMessage("overlay_notification_button_no")
65 }); 65 });
66 } 66 }
67 else 67 else
68 { 68 {
69 let regex = /<a>(.*?)<\/a>/g; 69 let regex = /<a>(.*?)<\/a>/g;
70 let match; 70 let match;
71 while (match = regex.exec(message)) 71 while (match = regex.exec(message))
72 { 72 {
73 buttons.push({ 73 buttons.push({
74 type: "link", 74 type: "link",
75 title: match[1] 75 title: match[1]
76 }); 76 });
77 } 77 }
78 78
79 // Chrome only allows two notification buttons so we need to fall back 79 // Chrome only allows two notification buttons so we need to fall back
80 // to a single button to open all links if there are more than two. 80 // to a single button to open all links if there are more than two.
81 let maxButtons = (notificationType == "critical") ? 2 : 1; 81 let maxButtons = (notificationType == "critical") ? 2 : 1;
82 if (buttons.length > maxButtons) 82 if (buttons.length > maxButtons)
83 { 83 {
84 buttons = [ 84 buttons = [
85 { 85 {
86 type: "open-all", 86 type: "open-all",
87 title: chrome.i18n.getMessage("notification_open_all") 87 title: browser.i18n.getMessage("notification_open_all")
88 } 88 }
89 ]; 89 ];
90 } 90 }
91 if (!["critical", "relentless"].includes(notificationType)) 91 if (!["critical", "relentless"].includes(notificationType))
92 { 92 {
93 buttons.push({ 93 buttons.push({
94 type: "configure", 94 type: "configure",
95 title: chrome.i18n.getMessage("notification_configure") 95 title: browser.i18n.getMessage("notification_configure")
96 }); 96 });
97 } 97 }
98 } 98 }
99 99
100 return buttons; 100 return buttons;
101 } 101 }
102 102
103 function openNotificationLinks() 103 function openNotificationLinks()
104 { 104 {
105 if (activeNotification.links) 105 if (activeNotification.links)
106 { 106 {
107 for (let link of activeNotification.links) 107 for (let link of activeNotification.links)
108 chrome.tabs.create({url: Utils.getDocLink(link)}); 108 browser.tabs.create({url: Utils.getDocLink(link)});
109 } 109 }
110 } 110 }
111 111
112 function notificationButtonClick(buttonIndex) 112 function notificationButtonClick(buttonIndex)
113 { 113 {
114 if (!(activeButtons && buttonIndex in activeButtons)) 114 if (!(activeButtons && buttonIndex in activeButtons))
115 return; 115 return;
116 116
117 switch (activeButtons[buttonIndex].type) 117 switch (activeButtons[buttonIndex].type)
118 { 118 {
119 case "link": 119 case "link":
120 chrome.tabs.create({ 120 browser.tabs.create({
121 url: Utils.getDocLink(activeNotification.links[buttonIndex]) 121 url: Utils.getDocLink(activeNotification.links[buttonIndex])
122 }); 122 });
123 break; 123 break;
124 case "open-all": 124 case "open-all":
125 openNotificationLinks(); 125 openNotificationLinks();
126 break; 126 break;
127 case "configure": 127 case "configure":
128 Prefs.notifications_showui = true; 128 Prefs.notifications_showui = true;
129 showOptions(page => 129 showOptions(page =>
130 { 130 {
(...skipping 22 matching lines...) Expand all
153 { 153 {
154 // Chrome hides notifications in notification center when clicked so 154 // Chrome hides notifications in notification center when clicked so
155 // we need to clear them. 155 // we need to clear them.
156 function clearActiveNotification(notificationId) 156 function clearActiveNotification(notificationId)
157 { 157 {
158 if (activeNotification && 158 if (activeNotification &&
159 activeNotification.type != "question" && 159 activeNotification.type != "question" &&
160 !("links" in activeNotification)) 160 !("links" in activeNotification))
161 return; 161 return;
162 162
163 chrome.notifications.clear(notificationId, wasCleared => 163 browser.notifications.clear(notificationId, wasCleared =>
164 { 164 {
165 if (wasCleared) 165 if (wasCleared)
166 notificationClosed(); 166 notificationClosed();
167 }); 167 });
168 } 168 }
169 169
170 chrome.notifications.onButtonClicked.addListener( 170 browser.notifications.onButtonClicked.addListener(
171 (notificationId, buttonIndex) => 171 (notificationId, buttonIndex) =>
172 { 172 {
173 notificationButtonClick(buttonIndex); 173 notificationButtonClick(buttonIndex);
174 clearActiveNotification(notificationId); 174 clearActiveNotification(notificationId);
175 } 175 }
176 ); 176 );
177 chrome.notifications.onClicked.addListener(clearActiveNotification); 177 browser.notifications.onClicked.addListener(clearActiveNotification);
178 chrome.notifications.onClosed.addListener(notificationClosed); 178 browser.notifications.onClosed.addListener(notificationClosed);
179 } 179 }
180 180
181 function showNotification(notification) 181 function showNotification(notification)
182 { 182 {
183 if (activeNotification && activeNotification.id == notification.id) 183 if (activeNotification && activeNotification.id == notification.id)
184 return; 184 return;
185 185
186 activeNotification = notification; 186 activeNotification = notification;
187 if (shouldDisplay("notification", activeNotification.type)) 187 if (shouldDisplay("notification", activeNotification.type))
188 { 188 {
189 let texts = NotificationStorage.getLocalizedTexts(notification); 189 let texts = NotificationStorage.getLocalizedTexts(notification);
190 let title = texts.title || ""; 190 let title = texts.title || "";
191 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); 191 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, "");
192 let iconUrl = chrome.extension.getURL("icons/detailed/abp-128.png"); 192 let iconUrl = browser.extension.getURL("icons/detailed/abp-128.png");
193 let linkCount = (activeNotification.links || []).length; 193 let linkCount = (activeNotification.links || []).length;
194 194
195 if ("notifications" in chrome) 195 if ("notifications" in browser)
196 { 196 {
197 activeButtons = getNotificationButtons(activeNotification.type, 197 activeButtons = getNotificationButtons(activeNotification.type,
198 texts.message); 198 texts.message);
199 chrome.notifications.create("", { 199 browser.notifications.create("", {
200 type: "basic", 200 type: "basic",
201 title, 201 title,
202 iconUrl, 202 iconUrl,
203 message, 203 message,
204 buttons: activeButtons.map(button => ({title: button.title})), 204 buttons: activeButtons.map(button => ({title: button.title})),
205 // We use the highest priority to prevent the notification 205 // We use the highest priority to prevent the notification
206 // from closing automatically. 206 // from closing automatically.
207 priority: 2 207 priority: 2
208 }); 208 });
209 } 209 }
210 else if ("Notification" in window && activeNotification.type != "question") 210 else if ("Notification" in window && activeNotification.type != "question")
211 { 211 {
212 if (linkCount > 0) 212 if (linkCount > 0)
213 message += " " + chrome.i18n.getMessage("notification_without_buttons"); 213 {
214 message += " " + browser.i18n.getMessage(
215 "notification_without_buttons"
216 );
217 }
214 218
215 let widget = new Notification( 219 let widget = new Notification(
216 title, 220 title,
217 { 221 {
218 lang: Utils.appLocale, 222 lang: Utils.appLocale,
219 dir: Utils.readingDirection, 223 dir: Utils.readingDirection,
220 body: message, 224 body: message,
221 icon: iconUrl 225 icon: iconUrl
222 } 226 }
223 ); 227 );
224 228
225 widget.addEventListener("click", openNotificationLinks); 229 widget.addEventListener("click", openNotificationLinks);
226 widget.addEventListener("close", notificationClosed); 230 widget.addEventListener("close", notificationClosed);
227 } 231 }
228 else 232 else
229 { 233 {
230 message = title + "\n" + message; 234 message = title + "\n" + message;
231 if (linkCount > 0) 235 if (linkCount > 0)
232 message += "\n\n" + chrome.i18n.getMessage("notification_with_buttons"); 236 {
237 message += "\n\n" + browser.i18n.getMessage(
238 "notification_with_buttons"
239 );
240 }
233 241
234 let approved = confirm(message); 242 let approved = confirm(message);
235 if (activeNotification.type == "question") 243 if (activeNotification.type == "question")
236 notificationButtonClick(approved ? 0 : 1); 244 notificationButtonClick(approved ? 0 : 1);
237 else if (approved) 245 else if (approved)
238 openNotificationLinks(); 246 openNotificationLinks();
239 } 247 }
240 } 248 }
241 prepareNotificationIconAndPopup(); 249 prepareNotificationIconAndPopup();
242 250
243 if (notification.type !== "question") 251 if (notification.type !== "question")
244 NotificationStorage.markAsShown(notification.id); 252 NotificationStorage.markAsShown(notification.id);
245 } 253 }
246 254
247 /** 255 /**
248 * Initializes the notification system. 256 * Initializes the notification system.
249 */ 257 */
250 exports.initNotifications = () => 258 exports.initNotifications = () =>
251 { 259 {
252 if ("notifications" in chrome) 260 if ("notifications" in browser)
253 initChromeNotifications(); 261 initChromeNotifications();
254 initAntiAdblockNotification(); 262 initAntiAdblockNotification();
255 }; 263 };
256 264
257 /** 265 /**
258 * Gets the active notification to be shown if any. 266 * Gets the active notification to be shown if any.
259 * 267 *
260 * @return {?object} 268 * @return {?object}
261 */ 269 */
262 exports.getActiveNotification = () => activeNotification; 270 exports.getActiveNotification = () => activeNotification;
(...skipping 12 matching lines...) Expand all
275 let methods = displayMethods[notificationType] || defaultDisplayMethods; 283 let methods = displayMethods[notificationType] || defaultDisplayMethods;
276 return methods.includes(method); 284 return methods.includes(method);
277 }; 285 };
278 286
279 ext.pages.onLoading.addListener(page => 287 ext.pages.onLoading.addListener(page =>
280 { 288 {
281 NotificationStorage.showNext(stringifyURL(page.url)); 289 NotificationStorage.showNext(stringifyURL(page.url));
282 }); 290 });
283 291
284 NotificationStorage.addShowListener(showNotification); 292 NotificationStorage.addShowListener(showNotification);
OLDNEW
« no previous file with comments | « lib/io.js ('k') | lib/options.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld