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"]; |
@@ -63,30 +64,36 @@ |
{ |
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; |
} |
} |
@@ -151,6 +158,7 @@ |
if (canUseChromeNotifications) |
{ |
+ activeButtons = []; |
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
|
let opts = { |
type: "basic", |
title: title, |
@@ -160,21 +168,55 @@ |
}; |
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")}); |
+ 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 plainMessage = texts.message || ""; |
let match; |
- while (match = regex.exec(plainMessage)) |
- opts.buttons.push({title: match[1]}); |
+ while (match = regex.exec(texts.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") |
+ }); |
+ } |
} |
imgToBase64(iconUrl, function(iconData) |
{ |
- opts["iconUrl"] = iconData; |
+ 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
|
+ { |
+ return {title: button.title}; |
+ }); |
+ opts.iconUrl = iconData; |
chrome.notifications.create("", opts, function() {}); |
}); |
} |