 Issue 29570614:
  Issue 5028 - Use browser namespace  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/
    
  
    Issue 29570614:
  Issue 5028 - Use browser namespace  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluschrome/| 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-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 Loading... | |
| 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 Loading... | |
| 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"); | |
| 
kzar
2017/10/16 10:35:51
Nit: IMO it looks weird having the indentation lik
 
Manish Jethani
2017/10/16 12:17:35
Done.
 | |
| 216 } | |
| 214 | 217 | 
| 215 let widget = new Notification( | 218 let widget = new Notification( | 
| 216 title, | 219 title, | 
| 217 { | 220 { | 
| 218 lang: Utils.appLocale, | 221 lang: Utils.appLocale, | 
| 219 dir: Utils.readingDirection, | 222 dir: Utils.readingDirection, | 
| 220 body: message, | 223 body: message, | 
| 221 icon: iconUrl | 224 icon: iconUrl | 
| 222 } | 225 } | 
| 223 ); | 226 ); | 
| 224 | 227 | 
| 225 widget.addEventListener("click", openNotificationLinks); | 228 widget.addEventListener("click", openNotificationLinks); | 
| 226 widget.addEventListener("close", notificationClosed); | 229 widget.addEventListener("close", notificationClosed); | 
| 227 } | 230 } | 
| 228 else | 231 else | 
| 229 { | 232 { | 
| 230 message = title + "\n" + message; | 233 message = title + "\n" + message; | 
| 231 if (linkCount > 0) | 234 if (linkCount > 0) | 
| 232 message += "\n\n" + chrome.i18n.getMessage("notification_with_buttons"); | 235 { | 
| 236 message += "\n\n" + browser.i18n.getMessage( | |
| 237 "notification_with_buttons"); | |
| 238 } | |
| 233 | 239 | 
| 234 let approved = confirm(message); | 240 let approved = confirm(message); | 
| 235 if (activeNotification.type == "question") | 241 if (activeNotification.type == "question") | 
| 236 notificationButtonClick(approved ? 0 : 1); | 242 notificationButtonClick(approved ? 0 : 1); | 
| 237 else if (approved) | 243 else if (approved) | 
| 238 openNotificationLinks(); | 244 openNotificationLinks(); | 
| 239 } | 245 } | 
| 240 } | 246 } | 
| 241 prepareNotificationIconAndPopup(); | 247 prepareNotificationIconAndPopup(); | 
| 242 | 248 | 
| 243 if (notification.type !== "question") | 249 if (notification.type !== "question") | 
| 244 NotificationStorage.markAsShown(notification.id); | 250 NotificationStorage.markAsShown(notification.id); | 
| 245 } | 251 } | 
| 246 | 252 | 
| 247 /** | 253 /** | 
| 248 * Initializes the notification system. | 254 * Initializes the notification system. | 
| 249 */ | 255 */ | 
| 250 exports.initNotifications = () => | 256 exports.initNotifications = () => | 
| 251 { | 257 { | 
| 252 if ("notifications" in chrome) | 258 if ("notifications" in browser) | 
| 253 initChromeNotifications(); | 259 initChromeNotifications(); | 
| 254 initAntiAdblockNotification(); | 260 initAntiAdblockNotification(); | 
| 255 }; | 261 }; | 
| 256 | 262 | 
| 257 /** | 263 /** | 
| 258 * Gets the active notification to be shown if any. | 264 * Gets the active notification to be shown if any. | 
| 259 * | 265 * | 
| 260 * @return {?object} | 266 * @return {?object} | 
| 261 */ | 267 */ | 
| 262 exports.getActiveNotification = () => activeNotification; | 268 exports.getActiveNotification = () => activeNotification; | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 275 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 281 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 
| 276 return methods.includes(method); | 282 return methods.includes(method); | 
| 277 }; | 283 }; | 
| 278 | 284 | 
| 279 ext.pages.onLoading.addListener(page => | 285 ext.pages.onLoading.addListener(page => | 
| 280 { | 286 { | 
| 281 NotificationStorage.showNext(stringifyURL(page.url)); | 287 NotificationStorage.showNext(stringifyURL(page.url)); | 
| 282 }); | 288 }); | 
| 283 | 289 | 
| 284 NotificationStorage.addShowListener(showNotification); | 290 NotificationStorage.addShowListener(showNotification); | 
| OLD | NEW |