greasemonkey/facebook-notification.user.js

102 lines
3.1 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.2.8
// @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/*
// @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;
var timeout = GM_getValue("timeout", 10000);
2014-02-24 14:11:14 +01:00
var intervalID = null;
2014-02-24 14:11:14 +01:00
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;
}
2014-02-24 14:11:14 +01:00
newFriendRequests = friendRequestsElement.innerHTML;
newMessages = messagesElement.innerHTML;
newNotifications = notificationsElement.innerHTML;
if (newFriendRequests > lastFriendRequests || newMessages > lastMessages || newNotifications > lastNotifications)
2014-02-24 14:11:14 +01:00
{
var msg= " ";
if (newFriendRequests > lastFriendRequests)
msg = msg + "| " + (newFriendRequests - lastFriendRequests) + " new Friend Request(s) |";
2014-02-24 14:11:14 +01:00
if (newMessages > lastMessages)
msg = msg + "| " + (newMessages - lastMessages) + " new Message(s) |";
2014-02-24 14:11:14 +01:00
if (newNotifications > lastNotifications)
msg = msg + "| " + (newNotifications - lastNotifications) + " new Notification(s) |";
2014-02-24 14:11:14 +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,
"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")
{
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
{
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();