Commit e079e431 authored by Dave Barker's avatar Dave Barker 🐈

Refs #60, #62 - Rework the notification button handling logic

parent b4fead3b
Pipeline #77383680 passed with stages
in 7 minutes and 26 seconds
......@@ -57,6 +57,22 @@ let buttonsByNotificationId = new Map();
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/20146233/
const browserNotificationsSupported = require("info").platform != "edgehtml";
// Opera < 57 and Firefox (tested up to 68.0.1) do not support notifications
// being created with buttons. Opera added button support with >= 57, however
// the button click handlers have broken with >= 60 (tested up to 62). Until
// Opera fixes that bug (reference DNAWIZ-70332) we unfortunately can't use
// feature detection when deciding if notification buttons should be displayed.
const browserNotificationButtonsSupported = browserNotificationsSupported &&
info.platform == "chromium" &&
info.application != "opera";
// As of August 2019, only Chrome supports this flag and, since Firefox
// throws on unsupported options (tested with version 69), we need to
// explicitly set it only for supported browsers.
const browserNotificationRequireInteractionSupported = (
info.platform == "chromium" && parseInt(info.platformVersion, 10) >= 50
);
function playNotificationIconAnimation(notification)
{
let animateIcon = !(notification.urlFilters instanceof Array) &&
......@@ -203,14 +219,21 @@ function showNotification(notification)
if (activeNotification && activeNotification.id == notification.id)
return;
// Without buttons, showing notifications of the type "question" is pointless.
if (notification.type == "question" && !browserNotificationButtonsSupported)
return;
activeNotification = notification;
if (shouldDisplay("notification", notification.type))
{
let texts = NotificationStorage.getLocalizedTexts(notification);
let title = texts.title || "";
let notificationTitle = texts.title || "";
let message = (texts.message || "").replace(/<\/?(a|strong)>/g, "");
let iconUrl = browser.extension.getURL("icons/detailed/abp-128.png");
// We take a note of the notification's buttons even if notification buttons
// are not supported by this browser. That way, if the user clicks the
// (buttonless) notification we can still open all the links.
let buttons = getNotificationButtons(notification, texts.message);
buttonsByNotificationId.set(notification.id, buttons);
......@@ -218,45 +241,24 @@ function showNotification(notification)
{
let notificationOptions = {
type: "basic",
title,
title: notificationTitle,
iconUrl,
message,
buttons: buttons.map(button => ({title: button.title})),
// We use the highest priority to prevent the notification
// from closing automatically, for browsers that don't support the
// requireInteraction flag.
priority: 2
};
// As of August 2019, only Chrome supports this flag and, since Firefox
// throws on unsupported options (tested with version 69), we need to
// explicitly set it only for supported browsers.
if (info.platform == "chromium" &&
parseInt(info.platformVersion, 10) >= 50)
{
if (browserNotificationButtonsSupported)
notificationOptions.buttons = buttons.map(({title}) => ({title}));
if (browserNotificationRequireInteractionSupported)
notificationOptions.requireInteraction = true;
}
// Firefox and Opera don't support buttons. Firefox throws synchronously,
// while Opera gives an asynchronous error. Wrapping the promise like
// this, turns the synchronous error on Firefox into a promise rejection.
new Promise(resolve =>
{
resolve(browser.notifications.create(notification.id,
notificationOptions));
}).catch(() =>
{
// Without buttons, showing notifications of the type "question" is
// pointless. For other notifications, retry with the buttons removed.
if (notification.type != "question")
{
delete notificationOptions.buttons;
buttonsByNotificationId.delete(notification.id);
browser.notifications.create(notification.id, notificationOptions);
}
});
browser.notifications.create(notification.id, notificationOptions);
}
else if (notification.type != "question")
else
{
let linkCount = (notification.links || []).length;
......@@ -268,7 +270,7 @@ function showNotification(notification)
}
let basicNotification = new Notification(
title,
notificationTitle,
{
lang: Utils.appLocale,
dir: Utils.readingDirection,
......@@ -311,6 +313,8 @@ exports.initNotifications = () =>
{
if (typeof buttonIndex != "undefined")
notificationButtonClick(notificationId, buttonIndex);
else if (!browserNotificationButtonsSupported)
openNotificationLinks(notificationId);
// Chrome hides notifications in the notification center when clicked,
// so we need to clear them.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment