15

I can enable desktop notifications for chats on the lower right side:

enter image description here

But I only get these notifications when I am pinged in a message like:

@Lino bla bla bla - User

There is already a feature to play a notification sound for different kind of messages:

enter image description here

Could we also have the same options for desktop notifications? So that I could get desktop notifications for every new message in every chat I am currently part of?


Note: this is not related to the SO Close Vote Reviewers-Chat, I just chose it as an example

1 Answer 1

13

I'd like to see this feature as well, mainly to keep track on some specific chat rooms.

While there's currently no SE-sided support, I created a userscript as a workaround. It adds a link (as can be seen here), which toggles it for each room. It hooks into the SE chat API using CHAT.addEventHandlerHook to grab chat messages, and from there uses Notification to send you a notification, assuming the message isn't from you.

You can check if your browser supports the feature here. If you aren't familiar with userscripts, you need a browser plugin for it. One example is TamperMonkey, but there's also other variants out there.

Direct install

Code (also available on GitHub):

// ==UserScript==
// @name         Desktop Notifications Everywhere
// @match        *://chat.stackexchange.com/rooms/*
// @match        *://chat.meta.stackexchange.com/rooms/*
// @match        *://chat.stackoverflow.com/rooms/*
// @downloadURL  https://github.com/LunarWatcher/userscripts/raw/master/DesktopNotificationsEverywhere/DesktopNotificationsEverywhere.user.js
// @updateURL    https://github.com/LunarWatcher/userscripts/raw/master/DesktopNotificationsEverywhere/DesktopNotificationsEverywhere.meta.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

var url = null;
var uid = 0;
var room = "";
var toggled = false;

function load() {
    var value = GM_getValue("room-" + room);
    if (value == null) return false;
    else return value == "true" || value == "1";
}

function save() {
    GM_setValue("room-" + room, toggled);

}

$(document).ready(function () {
    // Parse various necessary data
    url = window.location.href;
    let i = $("#active-user").attr("class");
    uid = parseInt(i.replace("user-container user-", ""), 10);
    if (uid == 0) throw "Failed to find UID";

    room = $("#roomname").text();

    // Link the event handler
    CHAT.addEventHandlerHook(chatMessageRecieved);

    toggled = load();

    // Link toggler

     $("#sidebar-menu").append("| <a id='toggleAllNotifications' href='#' onclick='return false'>" + (toggled ? "Disable" : "Enable") + " all notifications</a>");


    $("#toggleAllNotifications").click(function () {
        toggleNotifications();
    });
});

function toggleNotifications() {
    toggled = !toggled;
    $("#toggleAllNotifications").text((toggled ? "Disable" : "Enable") + " all notifications");
    save();
}

function chatMessageRecieved({event_type, user_id, content, user_name}) {

    if(!toggled) return;
    if (event_type !== 1) {
        return false;
    }

    if(user_id == uid) return; // Ignore your own messages
    var text = $('<div>').html(content).text();
    if (text == "" || text == null) text = "<Image>"
    notify(text, user_name);

}


function notify(content, username) {

    if (Notification.permission !== 'granted')
        Notification.requestPermission();
    else {
        var notification = new Notification(room + " | " + username, {
            icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
            body: content
        });

        notification.onclick = function () {
            window.open(url);
        };

    }

}
3
  • is it required to have SO open in a tag for this to work? Or will it work even when only my browser is open? Oct 11, 2020 at 5:08
  • 1
    It only works while a chat tab is open. It's a limitation of the API that can't be fixed. Oct 11, 2020 at 5:37
  • I have a suggestion.. clicking on the notification opens the chat in a new tab... Instead of this if it would be great if it can bring the previously opened tab into focus... That way I won't have multiple tabs opened of the same chat... Great script btw I am loving it! :) Oct 11, 2020 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .