OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 let {startIconAnimation, stopIconAnimation} = require("icon"); |
| 19 let {Utils} = require("utils"); |
| 20 let {Notification: NotificationStorage} = require("notification"); |
| 21 |
| 22 let activeNotification = null; |
| 23 |
| 24 // Chrome on Linux does not fully support chrome.notifications until version 35 |
| 25 // https://code.google.com/p/chromium/issues/detail?id=291485 |
| 26 let canUseChromeNotifications = (function() |
| 27 { |
| 28 let info = require("info"); |
| 29 if (info.platform == "chromium" && "notifications" in chrome) |
| 30 { |
| 31 if (navigator.platform.indexOf("Linux") == -1) |
| 32 return true; |
| 33 if (Services.vc.compare(info.applicationVersion, "35") >= 0) |
| 34 return true; |
| 35 } |
| 36 return false; |
| 37 })(); |
| 38 |
| 39 function prepareNotificationIconAndPopup() |
| 40 { |
| 41 let animateIcon = (activeNotification.type !== "question"); |
| 42 activeNotification.onClicked = function() |
| 43 { |
| 44 if (animateIcon) |
| 45 stopIconAnimation(); |
| 46 notificationClosed(); |
| 47 }; |
| 48 if (animateIcon) |
| 49 startIconAnimation(activeNotification.type); |
| 50 } |
| 51 |
| 52 function openNotificationLinks() |
| 53 { |
| 54 if (activeNotification.links) |
| 55 { |
| 56 activeNotification.links.forEach(function(link) |
| 57 { |
| 58 ext.windows.getLastFocused(function(win) |
| 59 { |
| 60 win.openTab(Utils.getDocLink(link)); |
| 61 }); |
| 62 }); |
| 63 } |
| 64 } |
| 65 |
| 66 function notificationButtonClick(buttonIndex) |
| 67 { |
| 68 if (activeNotification.type === "question") |
| 69 { |
| 70 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn
dex === 0); |
| 71 NotificationStorage.markAsShown(activeNotification.id); |
| 72 activeNotification.onClicked(); |
| 73 } |
| 74 else if (activeNotification.links && activeNotification.links[buttonIndex]) |
| 75 { |
| 76 ext.windows.getLastFocused(function(win) |
| 77 { |
| 78 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); |
| 79 }); |
| 80 } |
| 81 } |
| 82 |
| 83 function notificationClosed() |
| 84 { |
| 85 activeNotification = null; |
| 86 } |
| 87 |
| 88 function imgToBase64(url, callback) |
| 89 { |
| 90 let canvas = document.createElement("canvas"), |
| 91 ctx = canvas.getContext("2d"), |
| 92 img = new Image; |
| 93 img.src = url; |
| 94 img.onload = function() |
| 95 { |
| 96 canvas.height = img.height; |
| 97 canvas.width = img.width; |
| 98 ctx.drawImage(img, 0, 0); |
| 99 callback(canvas.toDataURL("image/png")); |
| 100 canvas = null; |
| 101 }; |
| 102 } |
| 103 |
| 104 function initChromeNotifications() |
| 105 { |
| 106 // Chrome hides notifications in notification center when clicked so we need t
o clear them |
| 107 function clearActiveNotification(notificationId) |
| 108 { |
| 109 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) |
| 110 return; |
| 111 |
| 112 chrome.notifications.clear(notificationId, function(wasCleared) |
| 113 { |
| 114 if (wasCleared) |
| 115 notificationClosed(); |
| 116 }); |
| 117 } |
| 118 |
| 119 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt
onIndex) |
| 120 { |
| 121 notificationButtonClick(buttonIndex); |
| 122 clearActiveNotification(notificationId); |
| 123 }); |
| 124 chrome.notifications.onClicked.addListener(clearActiveNotification); |
| 125 chrome.notifications.onClosed.addListener(notificationClosed); |
| 126 } |
| 127 |
| 128 function showNotification(notification) |
| 129 { |
| 130 if (activeNotification && activeNotification.id === notification.id) |
| 131 return; |
| 132 |
| 133 activeNotification = notification; |
| 134 if (activeNotification.type === "critical" || activeNotification.type === "que
stion") |
| 135 { |
| 136 let texts = NotificationStorage.getLocalizedTexts(notification); |
| 137 let title = texts.title || ""; |
| 138 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "")
: ""; |
| 139 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
| 140 let hasLinks = activeNotification.links && activeNotification.links.length >
0; |
| 141 |
| 142 if (canUseChromeNotifications) |
| 143 { |
| 144 let opts = { |
| 145 type: "basic", |
| 146 title: title, |
| 147 message: message, |
| 148 buttons: [], |
| 149 priority: 2 // We use the highest priority to prevent the notification f
rom closing automatically |
| 150 }; |
| 151 if (activeNotification.type === "question") |
| 152 { |
| 153 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")}); |
| 155 } |
| 156 else |
| 157 { |
| 158 let regex = /<a>(.*?)<\/a>/g; |
| 159 let plainMessage = texts.message || ""; |
| 160 let match; |
| 161 while (match = regex.exec(plainMessage)) |
| 162 opts.buttons.push({title: match[1]}); |
| 163 } |
| 164 |
| 165 imgToBase64(iconUrl, function(iconData) |
| 166 { |
| 167 opts["iconUrl"] = iconData; |
| 168 chrome.notifications.create("", opts, function() {}); |
| 169 }); |
| 170 } |
| 171 else if ("Notification" in window && activeNotification.type !== "question") |
| 172 { |
| 173 if (hasLinks) |
| 174 message += " " + ext.i18n.getMessage("notification_without_buttons"); |
| 175 |
| 176 imgToBase64(iconUrl, function(iconData) |
| 177 { |
| 178 let notification = new Notification( |
| 179 title, |
| 180 { |
| 181 lang: Utils.appLocale, |
| 182 dir: ext.i18n.getMessage("@@bidi_dir"), |
| 183 body: message, |
| 184 icon: iconData |
| 185 } |
| 186 ); |
| 187 |
| 188 notification.addEventListener("click", openNotificationLinks); |
| 189 notification.addEventListener("close", notificationClosed); |
| 190 }); |
| 191 } |
| 192 else |
| 193 { |
| 194 let message = title + "\n" + message; |
| 195 if (hasLinks) |
| 196 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
| 197 |
| 198 let approved = confirm(message); |
| 199 if (activeNotification.type === "question") |
| 200 notificationButtonClick(approved ? 0 : 1); |
| 201 else if (approved) |
| 202 openNotificationLinks(); |
| 203 } |
| 204 } |
| 205 prepareNotificationIconAndPopup(); |
| 206 } |
| 207 |
| 208 setTimeout(function() |
| 209 { |
| 210 let notificationToShow = NotificationStorage.getNextToShow(); |
| 211 if (notificationToShow) |
| 212 showNotification(notificationToShow); |
| 213 }, 3 * 60 * 1000); |
OLD | NEW |