greasemonkey/facebook-notification.user.js
2017-12-19 19:17:02 +01:00

102 lines
3.1 KiB
JavaScript

// ==UserScript==
// @name Facebook desktop notification
// @namespace https://www.schtobia.de/
// @version 0.2.8
// @description Facebook desktop notification (based on HTML5)
// @author Tobias Schmidl
// @include http*://www.facebook.com/*
// @include http*://m.facebook.com/*
// @include http*://facebook.com/*
// @grant GM_getValue
// @run-at document-end
// @downloadURL https://github.com/schtobia/greasemonkey/raw/master/facebook-notification.user.js
// @updateURL https://github.com/schtobia/greasemonkey/raw/master/facebook-notification.user.js
// ==/UserScript==
//
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
var friendRequestsElement = document.getElementById("requestsCountValue");
var messagesElement = document.getElementById("mercurymessagesCountValue");
var notificationsElement = document.getElementById("notificationsCountValue");
var newFriendRequests = 0;
var newMessages = 0;
var newNotifications = 0;
var lastFriendRequests = 0;
var lastMessages = 0;
var lastNotifications = 0;
var notificationInstance = null;
var timeout = GM_getValue("timeout", 10000);
var intervalID = null;
notify = function()
{
if (friendRequestsElement == null || messagesElement == null || notificationsElement == null)
{
console.log(GM_info.script.name + ": One of the DOM nodes is empty! Clearing the interval timer " + intervalID + "!");
clearInterval(intervalID);
intervalID = null;
return;
}
newFriendRequests = friendRequestsElement.innerHTML;
newMessages = messagesElement.innerHTML;
newNotifications = notificationsElement.innerHTML;
if (newFriendRequests > lastFriendRequests || newMessages > lastMessages || newNotifications > lastNotifications)
{
var msg= " ";
if (newFriendRequests > lastFriendRequests)
msg = msg + "| " + (newFriendRequests - lastFriendRequests) + " new Friend Request(s) |";
if (newMessages > lastMessages)
msg = msg + "| " + (newMessages - lastMessages) + " new Message(s) |";
if (newNotifications > lastNotifications)
msg = msg + "| " + (newNotifications - lastNotifications) + " new Notification(s) |";
console.log(GM_info.script.name + ": Trying to set notification...", msg);
notificationInstance = new Notification("Facebook (" + newFriendRequests + '|' + newMessages + '|' + newNotifications + ')',
{
"dir" : "auto",
"body" : msg,
"icon" : document.baseURI + "/favicon.ico",
"lang" : "en-US"
}
)
lastFriendRequests = newFriendRequests;
lastMessages = newMessages;
lastNotifications = newNotifications;
}
}
registerNotification = function()
{
Notification.requestPermission(function(grantedPermission){
if (grantedPermission === "granted")
{
console.log(GM_info.script.name + ": permission granted");
intervalID = setInterval(notify, timeout);
console.log(GM_info.script.name + ": intervalID: " + intervalID);
}
else
{
console.log(GM_info.script.name + ": permission NOT granted");
}
});
}
/* Normal flow of operations below! */
if (!Notification)
{
alert("Notifications are not supported for this browser version yet.");
}
else
registerNotification();