Index: lib/notificationHelper.js |
=================================================================== |
--- a/lib/notificationHelper.js |
+++ b/lib/notificationHelper.js |
@@ -24,6 +24,7 @@ |
let {initAntiAdblockNotification} = require("antiadblockInit"); |
let activeNotification = null; |
+let activeButtons = null; |
let defaultDisplayMethods = ["popup"]; |
let displayMethods = Object.create(null); |
displayMethods.critical = ["icon", "notification", "popup"]; |
@@ -59,34 +60,90 @@ |
startIconAnimation(activeNotification.type); |
} |
+function getNotificationButtons(message) |
+{ |
+ activeButtons = []; |
+ if (activeNotification.type == "question") |
+ { |
+ activeButtons.push({ |
+ type: "question", |
+ title: ext.i18n.getMessage("overlay_notification_button_yes") |
+ }); |
+ activeButtons.push({ |
+ type: "question", |
+ title: ext.i18n.getMessage("overlay_notification_button_no") |
+ }); |
+ } |
+ else |
+ { |
+ let regex = /<a>(.*?)<\/a>/g; |
+ let match; |
+ while (match = regex.exec(message)) |
+ { |
+ activeButtons.push({ |
+ type: "link", |
+ title: match[1] |
+ }); |
+ } |
+ |
+ // Chrome only allows two notification buttons so we need to fall back |
+ // to a single button to open all links if there are more than two. |
+ let maxButtons = (activeNotification.type == "critical") ? 2 : 1; |
+ if (activeButtons.length > maxButtons) |
+ { |
+ activeButtons = [ |
+ { |
+ type: "open-all", |
+ title: ext.i18n.getMessage("notification_open_all") |
+ } |
+ ]; |
+ } |
+ if (activeNotification.type != "critical") |
+ { |
+ activeButtons.push({ |
+ type: "configure", |
+ title: ext.i18n.getMessage("notification_configure") |
+ }); |
+ } |
+ } |
+ |
+ return activeButtons.map(button => ({title: button.title})); |
+} |
+ |
function openNotificationLinks() |
{ |
if (activeNotification.links) |
{ |
- activeNotification.links.forEach(function(link) |
- { |
- ext.windows.getLastFocused(function(win) |
- { |
- win.openTab(Utils.getDocLink(link)); |
- }); |
- }); |
+ for (let link of activeNotification.links) |
+ ext.pages.open(Utils.getDocLink(link)); |
} |
} |
function notificationButtonClick(buttonIndex) |
{ |
- if (activeNotification.type == "question") |
+ switch (activeButtons[buttonIndex].type) |
{ |
- NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIndex == 0); |
- NotificationStorage.markAsShown(activeNotification.id); |
- activeNotification.onClicked(); |
- } |
- else if (activeNotification.links && activeNotification.links[buttonIndex]) |
- { |
- ext.windows.getLastFocused(function(win) |
- { |
- win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); |
- }); |
+ case "link": |
+ ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); |
+ break; |
+ case "open-all": |
+ openNotificationLinks(); |
+ break; |
+ case "configure": |
+ Prefs.notifications_showui = true; |
+ ext.showOptions(function(page) |
+ { |
+ page.sendMessage({ |
+ type: "focus-section", |
+ section: "notifications" |
+ }); |
+ }); |
+ break; |
+ case "question": |
+ NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIndex == 0); |
+ NotificationStorage.markAsShown(activeNotification.id); |
+ activeNotification.onClicked(); |
+ break; |
} |
} |
@@ -147,7 +204,7 @@ |
let title = texts.title || ""; |
let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; |
let iconUrl = ext.getURL("icons/detailed/abp-128.png"); |
- let hasLinks = activeNotification.links && activeNotification.links.length > 0; |
+ let linkCount = (activeNotification.links || []).length; |
if (canUseChromeNotifications) |
{ |
@@ -155,32 +212,19 @@ |
type: "basic", |
title: title, |
message: message, |
- buttons: [], |
+ buttons: getNotificationButtons(texts.message), |
priority: 2 // We use the highest priority to prevent the notification from closing automatically |
}; |
- if (activeNotification.type == "question") |
- { |
- opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_button_yes")}); |
- opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_button_no")}); |
- } |
- else |
- { |
- let regex = /<a>(.*?)<\/a>/g; |
- let plainMessage = texts.message || ""; |
- let match; |
- while (match = regex.exec(plainMessage)) |
- opts.buttons.push({title: match[1]}); |
- } |
imgToBase64(iconUrl, function(iconData) |
{ |
- opts["iconUrl"] = iconData; |
+ opts.iconUrl = iconData; |
chrome.notifications.create("", opts, function() {}); |
}); |
} |
else if ("Notification" in window && activeNotification.type != "question") |
{ |
- if (hasLinks) |
+ if (linkCount > 0) |
message += " " + ext.i18n.getMessage("notification_without_buttons"); |
imgToBase64(iconUrl, function(iconData) |
@@ -202,7 +246,7 @@ |
else |
{ |
let message = title + "\n" + message; |
- if (hasLinks) |
+ if (linkCount > 0) |
message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); |
let approved = confirm(message); |