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

Unified Diff: lib/notificationHelper.js

Issue 29327244: Issue 3024 - Added opt-out to Chrome notifications (Closed)
Patch Set: Made `getNotificationButtons` independent of external variables Created Sept. 11, 2015, 2:37 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « _locales/en_US/messages.json ('k') | options.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(notificationType, message)
+{
+ let buttons = [];
+ if (notificationType == "question")
+ {
+ buttons.push({
+ type: "question",
+ title: ext.i18n.getMessage("overlay_notification_button_yes")
+ });
+ buttons.push({
+ type: "question",
+ title: ext.i18n.getMessage("overlay_notification_button_no")
+ });
+ }
+ else
+ {
+ let regex = /<a>(.*?)<\/a>/g;
+ let match;
+ while (match = regex.exec(message))
+ {
+ buttons.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 = (notificationType == "critical") ? 2 : 1;
+ if (buttons.length > maxButtons)
+ {
+ buttons = [
+ {
+ type: "open-all",
+ title: ext.i18n.getMessage("notification_open_all")
+ }
+ ];
+ }
+ if (notificationType != "critical")
+ {
+ buttons.push({
+ type: "configure",
+ title: ext.i18n.getMessage("notification_configure")
+ });
+ }
+ }
+
+ return buttons;
+}
+
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)
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
+ 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,40 +204,28 @@
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)
{
+ activeButtons = getNotificationButtons(activeNotification.type, texts.message);
let opts = {
type: "basic",
title: title,
message: message,
- buttons: [],
+ buttons: activeButtons.map(button => ({title: button.title})),
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 +247,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);
@@ -245,6 +290,7 @@
return activeNotification;
};
+let shouldDisplay =
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
/**
* Determines whether a given display method should be used for a
* specified notification type.
@@ -253,8 +299,7 @@
* @param {string} notificationType
* @return {boolean}
*/
-exports.shouldDisplay = shouldDisplay;
-function shouldDisplay(method, notificationType)
+exports.shouldDisplay = function(method, notificationType)
{
let methods = displayMethods[notificationType] || defaultDisplayMethods;
return methods.indexOf(method) > -1;
« no previous file with comments | « _locales/en_US/messages.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld