2014-02-24 14:11:14 +01:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Facebook desktop notification
|
|
|
|
// @namespace https://www.schtobia.de/
|
2017-12-19 19:17:02 +01:00
|
|
|
// @version 0.2.8
|
2014-03-08 07:08:05 +01:00
|
|
|
// @description Facebook desktop notification (based on HTML5)
|
2017-11-21 09:48:39 +01:00
|
|
|
// @author Tobias Schmidl
|
2014-02-24 14:11:14 +01:00
|
|
|
// @include http*://www.facebook.com/*
|
2017-12-19 19:17:02 +01:00
|
|
|
// @include http*://m.facebook.com/*
|
2014-02-24 14:11:14 +01:00
|
|
|
// @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;
|
|
|
|
|
2014-02-24 17:58:20 +01:00
|
|
|
var timeout = GM_getValue("timeout", 10000);
|
2014-02-24 14:11:14 +01:00
|
|
|
|
2014-02-24 15:49:24 +01:00
|
|
|
var intervalID = null;
|
|
|
|
|
2014-02-24 14:11:14 +01:00
|
|
|
notify = function()
|
|
|
|
{
|
2014-03-08 07:34:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-24 14:11:14 +01:00
|
|
|
newFriendRequests = friendRequestsElement.innerHTML;
|
|
|
|
newMessages = messagesElement.innerHTML;
|
|
|
|
newNotifications = notificationsElement.innerHTML;
|
|
|
|
|
2014-03-08 07:08:05 +01:00
|
|
|
if (newFriendRequests > lastFriendRequests || newMessages > lastMessages || newNotifications > lastNotifications)
|
2014-02-24 14:11:14 +01:00
|
|
|
{
|
|
|
|
var msg= " ";
|
|
|
|
|
|
|
|
if (newFriendRequests > lastFriendRequests)
|
2014-03-08 07:08:05 +01:00
|
|
|
msg = msg + "| " + (newFriendRequests - lastFriendRequests) + " new Friend Request(s) |";
|
2014-02-24 14:11:14 +01:00
|
|
|
|
2014-03-08 07:08:05 +01:00
|
|
|
if (newMessages > lastMessages)
|
|
|
|
msg = msg + "| " + (newMessages - lastMessages) + " new Message(s) |";
|
2014-02-24 14:11:14 +01:00
|
|
|
|
2014-03-08 07:08:05 +01:00
|
|
|
if (newNotifications > lastNotifications)
|
|
|
|
msg = msg + "| " + (newNotifications - lastNotifications) + " new Notification(s) |";
|
2014-02-24 14:11:14 +01:00
|
|
|
|
2014-02-24 15:49:24 +01:00
|
|
|
console.log(GM_info.script.name + ": Trying to set notification...", msg);
|
|
|
|
|
2014-02-24 14:11:14 +01:00
|
|
|
notificationInstance = new Notification("Facebook (" + newFriendRequests + '|' + newMessages + '|' + newNotifications + ')',
|
|
|
|
{
|
|
|
|
"dir" : "auto",
|
|
|
|
"body" : msg,
|
2015-02-18 15:48:28 +01:00
|
|
|
"icon" : document.baseURI + "/favicon.ico",
|
2014-03-08 20:05:54 +01:00
|
|
|
"lang" : "en-US"
|
2014-02-24 14:11:14 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
lastFriendRequests = newFriendRequests;
|
|
|
|
lastMessages = newMessages;
|
|
|
|
lastNotifications = newNotifications;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerNotification = function()
|
|
|
|
{
|
|
|
|
Notification.requestPermission(function(grantedPermission){
|
|
|
|
if (grantedPermission === "granted")
|
|
|
|
{
|
2014-02-24 15:49:24 +01:00
|
|
|
console.log(GM_info.script.name + ": permission granted");
|
|
|
|
intervalID = setInterval(notify, timeout);
|
|
|
|
console.log(GM_info.script.name + ": intervalID: " + intervalID);
|
2014-02-24 14:11:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 15:49:24 +01:00
|
|
|
console.log(GM_info.script.name + ": permission NOT granted");
|
2014-02-24 14:11:14 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normal flow of operations below! */
|
|
|
|
if (!Notification)
|
|
|
|
{
|
|
|
|
alert("Notifications are not supported for this browser version yet.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
registerNotification();
|