Browse Source

fix highlight-only notifications, use serviceworker registration for notifications

Andrew Dolgov 2 years ago
parent
commit
ea7a98908e
3 changed files with 34 additions and 19 deletions
  1. 14 11
      js/functions.js
  2. 20 8
      js/tt-irc.js
  3. 0 0
      worker.js

+ 14 - 11
js/functions.js

@@ -396,21 +396,24 @@ function notify(msg) {
 		if (alerts_enabled && typeof Notification != "undefined" &&
 				Notification.permission == "granted") {
 
-			var notification = new Notification(strip_tags(msg));
-			notification.onshow = function() {
-				setTimeout(notification.close.bind(notification), 5000);
-			};
-
-			notifications.push(notification);
-
-			if (notifications.length > 3) {
-				var notification = notifications.shift();
-				notification.close();
+			if ('serviceWorker' in navigator) {
+
+				navigator.serviceWorker.ready.then(function(registration) {
+					registration.showNotification(__('Tiny Tiny IRC'), {
+						body: msg,
+						icon: 'images/icon-hires.png' });
+
+					registration.getNotifications().then(function(notifications) {
+						while (notifications.length > 1) {
+							notifications.shift().close();
+						}
+					});
+				});
 			}
 
 		}
 	} catch (e) {
-		exception_error("notify", e);
+		console.warn("notify", e);
 	}
 }
 

+ 20 - 8
js/tt-irc.js

@@ -557,6 +557,14 @@ try {
 function init_second_stage(transport) {
 	try {
 
+		if ('serviceWorker' in navigator) {
+ 			 navigator.serviceWorker
+           .register('worker.js')
+           .then(function() {
+					console.log("service worker registered");
+			  });
+		}
+
 		var params = JSON.parse(transport.responseText);
 
 		if (!handle_error(params, transport)) return false;
@@ -1797,17 +1805,21 @@ function push_message(connection_id, channel, message, message_type, no_tab_hl)
 
 			if (!no_tab_hl && tab && (get_selected_tab() != tab || !window_active)) {
 
-				if (notify_events[1]) {
-					var msg = __("(%c) %n: %s");
+				if (notify_events[1] || notify_events[4]) {
+					var is_hl = notify_events[4] || is_highlight(connection_id, message);
 
-					msg = msg.replace("%c", message.channel);
-					msg = msg.replace("%n", message.sender);
-					msg = msg.replace("%s", message.message);
+					if (is_hl) {
+						var msg = __("(%c) %n: %s");
 
-					if (message.sender && message.channel &&
-							message.sender != model.getConnection(connection_id).active_nick()) {
+						msg = msg.replace("%c", message.channel);
+						msg = msg.replace("%n", message.sender);
+						msg = msg.replace("%s", message.message);
 
-						notify(msg);
+						if (message.sender && message.channel && message.sender != "---" &
+								message.sender != model.getConnection(connection_id).active_nick()) {
+
+							notify(msg, channel);
+						}
 					}
 				}
 

+ 0 - 0
worker.js