Desktop Notification plugin for chat messages - Better Battlelog Forums #127797

Sitemap
Post edited 1 x times, last by
Topicstarter
Hi! I'd like people to try out this plugin I made which uses the HTML5 Notifications API to display received chat messages as notifications.

One bug which I haven't been able to fix yet is that the first message in the history will be displayed when the chat window is opened or the page is opened with an open chat window.

Code
var notifiedWithinLast30 = false;

BBLog.handle("add.plugin", {

id: "notify-chat",

name: "Chat Desktop Notifications",

init: function(instance) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}

else if(!Notification.permission) {
Notification.requestPermission(function(perm){ notification.permission = perm; });
}

// Let's check if the user is okay to get some notification
if (Notification.permission === "granted" || Notification.permission !== "denied") {

// If it's okay let's create a notification
comcenter.chatmessage._render = comcenter.chatmessage.render;
comcenter.chatmessage.render = function(o,b,kwargs){

var html = comcenter.chatmessage._render(o,b,kwargs);
console.log(html);

var $html = $(html);
var obj = {
author: $html.attr("author"),
ts: $html.attr("ts"),
content: $html.find(".comcenter-chat-message-content").text(),
name: $(".name").text().replace(/[\n\r\s]/g, ""),
timestamp: $html.find(".comcenter-chat-timestamp").text().replace(/[\n\r\s]/g, "")
}

if(obj.content && instance.storage("lastNotified") < obj.ts && !notifiedWithinLast30 && obj.author !== obj.name){

var notif = new Notification(obj.author, {
body: obj.content,
icon: "http://getbblog.com/img/banners/450x450.jpg"
});

instance.storage("lastNotifed", obj.ts);

notifiedWithinLast30 = true;

setTimeout(function(){ notifiedWithinLast30 = false; }, 30000);

}

return html;
}

console.log("notify set up");

}

// At last, if the user already denied any notification, and you
// want to be respectful there is no need to bother them any more.
}

});
what should we do with this code? can we eat it?
have i to print it and stick it to my desktop screen?
Topicstarter
You can copy it into the plugin editor and run it yourself for testing purposes. It still has bugs. That's why I didn't publish it.
Post edited 2 x times, last by
ORFK wrote:
what should we do with this code? can we eat it?
have i to print it and stick it to my desktop screen?

It's posted in the custom plugins section, also he call it a "plugin".
So, it's obviously a BBLog plugin and you don't need to be rude.

Btw to the topic - Nice idea, never thought about that way.
BrainFooLong wrote:
So, it's obviously a BBLog plugin and you don't need to be rude.

nobody is rude. it was a question which can be asked by everybody.
I uploaded the source to my dropbox for easy install and use
https://dl.dropboxusercontent.com/u/33225956/BBLog/plugins/notify_chat/notify_chat.js