Index: lib/notification.js |
=================================================================== |
--- a/lib/notification.js |
+++ b/lib/notification.js |
@@ -38,6 +38,7 @@ |
relentless: 2, |
critical: 3 |
}; |
+const MATCHER = Symbol("Notification matcher"); |
let showListeners = []; |
let questionListeners = {}; |
@@ -103,10 +104,76 @@ |
} |
/** |
+ * Initializes notification's matcher based on notification's URL filters |
+ * @param {Object} notification |
+ */ |
+function initNotificationMatcher(notification) |
+{ |
+ if (MATCHER in notification || !(notification.urlFilters instanceof Array)) |
+ return; |
+ |
+ let matcher = new Matcher(); |
+ for (let urlFilter of notification.urlFilters) |
+ { |
+ matcher.add(Filter.fromText(urlFilter)); |
+ } |
+ notification[MATCHER] = matcher; |
+} |
+ |
+/** |
+ * Matches URL against notification's URL filters |
+ * @param {Object} notification |
+ * @param {string} [url] |
+ * @return {boolean} whether notification and URL match |
+ */ |
+function matchesUrl(notification, url) |
+{ |
+ // No matching necessary if there's nothing to match |
+ if (typeof url !== "string" && !(MATCHER in notification)) |
+ return true; |
+ |
+ // Notification shouldn't match if extension is disabled |
+ if (!Prefs.enabled) |
+ return false; |
+ |
+ // Notification shouldn't match if matching cannot be done |
+ if (typeof url !== "string" || !(MATCHER in notification)) |
+ return false; |
+ |
+ let host; |
+ try |
+ { |
+ host = new URL(url).hostname; |
+ } |
+ catch (e) |
+ { |
+ host = ""; |
+ } |
+ |
+ // Notification shouldn't match if extension is disabled on provided domain |
+ let exception = defaultMatcher.matchesAny( |
+ url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
+ ); |
+ if (exception instanceof WhitelistFilter) |
+ return false; |
+ |
+ // Notification should match if one of its filters matches |
+ let filter = notification[MATCHER].matchesAny( |
+ url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
+ ); |
+ return !!filter; |
+} |
+ |
+/** |
* The object providing actual downloading functionality. |
* @type {Downloader} |
*/ |
let downloader = null; |
+ |
+/** |
+ * List of notifications provided by the extension |
+ * @type {Object[]} |
+ */ |
let localData = []; |
/** |
@@ -120,6 +187,15 @@ |
*/ |
init() |
{ |
+ let {data} = Prefs.notificationdata; |
+ if (data) |
+ { |
+ for (let notification of data.notifications) |
+ { |
+ initNotificationMatcher(notification); |
+ } |
+ } |
+ |
downloader = new Downloader(this._getDownloadables.bind(this), |
INITIAL_DELAY, CHECK_INTERVAL); |
downloader.onExpirationChange = this._onExpirationChange.bind(this); |
@@ -174,6 +250,7 @@ |
notification.type = notification.severity; |
delete notification.severity; |
} |
+ initNotificationMatcher(notification); |
} |
Prefs.notificationdata.data = data; |
} |
@@ -233,13 +310,7 @@ |
*/ |
_getNextToShow(url) |
{ |
- let remoteData = []; |
- if (typeof Prefs.notificationdata.data == "object" && |
- Prefs.notificationdata.data.notifications instanceof Array) |
- { |
- remoteData = Prefs.notificationdata.data.notifications; |
- } |
- |
+ let remoteData = Prefs.notificationdata.data.notifications; |
let notifications = localData.concat(remoteData); |
if (notifications.length === 0) |
return null; |
@@ -298,39 +369,8 @@ |
} |
} |
- if (typeof url === "string" || notification.urlFilters instanceof Array) |
- { |
- if (Prefs.enabled && typeof url === "string" && |
- notification.urlFilters instanceof Array) |
- { |
- let host; |
- try |
- { |
- host = new URL(url).hostname; |
- } |
- catch (e) |
- { |
- host = ""; |
- } |
- |
- let exception = defaultMatcher.matchesAny( |
- url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
- ); |
- if (exception instanceof WhitelistFilter) |
- continue; |
- |
- let matcher = new Matcher(); |
- for (let urlFilter of notification.urlFilters) |
- matcher.add(Filter.fromText(urlFilter)); |
- if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, |
- false, null)) |
- { |
- continue; |
- } |
- } |
- else |
- continue; |
- } |
+ if (!matchesUrl(notification, url)) |
+ continue; |
if (notification.targets instanceof Array) |
{ |
@@ -433,7 +473,10 @@ |
addNotification(notification) |
{ |
if (localData.indexOf(notification) == -1) |
+ { |
+ initNotificationMatcher(notification); |
localData.push(notification); |
+ } |
}, |
/** |