greasemonkey/googleplus-notification.user.js
2017-11-21 09:49:05 +01:00

86 lines
2.4 KiB
JavaScript

// ==UserScript==
// @name Google+ desktop notification
// @namespace https://www.schtobia.de/
// @version 0.2.7
// @description Google+ desktop notification (based on HTML5)
// @author Tobias Schmidl
// @include http://plus.google.com/
// @include https://plus.google.com/
// @include http://plus.google.com/u/0/*
// @include https://plus.google.com/u/0/*
// @exclude http://plus.google.com/u/0/_/*
// @exclude https://plus.google.com/u/0/_/*
// @grant GM_getValue
// @run-at document-end
// @downloadURL https://github.com/schtobia/greasemonkey/raw/master/googleplus-notification.user.js
// @updateURL https://github.com/schtobia/greasemonkey/raw/master/googleplus-notification.user.js
// ==/UserScript==
//
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
var notificationsElement = document.getElementsByClassName("gb_Fa")[0];
var newNotifications = 0;
var lastNotifications = 0;
var notificationInstance = null;
var timeout = GM_getValue("timeout", 10000);
var intervalID = null;
notify = function()
{
if (notificationsElement == null)
{
console.log(GM_info.script.name + ": The DOM node is empty! Clearing the interval timer " + intervalID + "!");
clearInterval(intervalID);
intervalID = null;
return;
}
newNotifications = notificationsElement.innerHTML;
if (newNotifications > lastNotifications)
{
var msg = (newNotifications - lastNotifications) + " new Notification(s)";
console.log(GM_info.script.name + ": Trying to set notification...", msg);
notificationInstance = new Notification("Google+ (" + newNotifications + ')',
{
"dir" : "auto",
"body" : msg,
"icon" : document.baseURI + "/favicon.ico",
"lang" : "en-US"
}
)
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();