If the don't, clear the timer, as each subsequent run would operate on the same empty nodes.
		
			
				
	
	
		
			78 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| // ==UserScript==
 | |
| // @name          Google+ desktop notification
 | |
| // @namespace     https://www.schtobia.de/
 | |
| // @version       0.2.0
 | |
| // @description   Google+ desktop notification (based on HTML5)
 | |
| // @include       http*://plus.google.com/*
 | |
| // @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" : "https://ssl.gstatic.com/s2/oz/images/faviconr3.ico"
 | |
| 			}
 | |
| 		)
 | |
| 
 | |
| 		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();
 |