greasemonkey/facebook-notification.user.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-02-24 14:11:14 +01:00
// ==UserScript==
// @name Facebook desktop notification
// @namespace https://www.schtobia.de/
// @version 0.0.5
// @description Facebook desktop notification (based on HTML5)
// @include http*://www.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", 30000);
notify = function()
{
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) |";
notificationInstance = new Notification("Facebook (" + newFriendRequests + '|' + newMessages + '|' + newNotifications + ')',
{
"dir" : "auto",
"body" : msg,
"icon" : 'https://s-static.ak.facebook.com/rsrc.php/yi/r/q9U99v3_saj.ico',
}
)
lastFriendRequests = newFriendRequests;
lastMessages = newMessages;
lastNotifications = newNotifications;
}
}
registerNotification = function()
{
var registrationSuccessful = false;
Notification.requestPermission(function(grantedPermission){
if (grantedPermission === "granted")
{
registrationSuccessful = true;
console.log("permission granted");
}
else
{
registrationSuccessful = false;
console.log("permission NOT granted");
}
});
if (registrationSuccessful)
setTimeout(notify, timeout);
}
/* Normal flow of operations below! */
if (!Notification)
{
alert("Notifications are not supported for this browser version yet.");
}
else
registerNotification();