WebMail Notify

WebNotification Api for Webmail

目前為 2015-05-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name        WebMail Notify
// @namespace   type-style
// @description WebNotification Api for Webmail
// @include     https://support.euserv.de/webmail/
// @version     1
// @grant       none
// ==/UserScript==

var init = function () {
    console.info('--- init ---');
	if (check4Element()) { // greasemonkey fires init multiple times :-(
		if (check4Storage()) {			
			Notification.requestPermission(function (status) {
				if (Notification.permission !== status) {
					Notification.permission = status;
				}
			});
		
    		getIntervalStarted();
		}
	} else {
		console.log("--- element not found initial ---");
	}
   
    
}


window.addEventListener('load', function () {
    window.setTimeout(function () { // give some time
		init();
	}, 2000);
});

// activate for testing 
// document.addEventListener('dblclick', init, false); 

var check4Storage = function () {
    var storage = true;
    try {
        sessionStorage.setItem('storage', true);
    } 
    catch (e) {
        storage = false;
    }
    window.storage = true;
    return storage;
}
var getUnreadNr = function () {
    var el = check4Element()
    if (el) {
        var unreadNr = el.textContent;
        unreadNr = parseFloat(unreadNr.replace('(', ''), 10);
        return unreadNr;
    }
}
var check4Element = function() {
    var el = document.querySelector('.unreadcount');
	console.log(document.querySelector('.unreadcount'));
    if (el) {
       return el;
    } else {
       console.log("element not found");
       return false;
    }
}

var getIntervalStarted = function() {
    if (storage && check4Element()) {
        sessionStorage.setItem('unread', getUnreadNr()); // initial call
        var t = window.setInterval(function () {
            var oldNr = sessionStorage.getItem('unread'); // get old Status
            if (rcmail) { rcmail.command('checkmail'); } // check for newMails
            window.setTimeout(function () { // give some time
                sessionStorage.setItem('unread', getUnreadNr()); // get new Status and write it into storage
                // compare
                if (oldNr < sessionStorage.getItem('unread')) {
                    console.info('yes'); // there is more unread than before
                    // notification
                    if (window.Notification && Notification.permission === 'granted') { 
                       var messageItems = document.getElementById("messagelist").querySelectorAll(".message");
                       var bodyText = "";
                       for (var i = 0; i <= messageItems.length -1; i++) { // scan messages find the first and get subject
                           if (messageItems[i].style.display != "none") {
                             var bodyText = messageItems[i].querySelector(".subject a").textContent;
                             break;
                           }                    
                       }
                       var n = new Notification("New Mail", { 
                           body: bodyText,
                           icon: "data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22114%22%20height%3D%22114%22%3E%0A%3Crect%20width%3D%22114%22%20height%3D%22114%22%20rx%3D%2211%22%2F%3E%0A%3Cpath%20stroke%3D%22%23FFF%22%20stroke-width%3D%226%22%20d%3D%22m16%2C25h82v60H16zl37%2C37q4%2C3%208%2C0l37-37M16%2C85l30-30m22%2C0%2030%2C30%22%2F%3E%0A%3C%2Fsvg%3E"
                       }); 
                    }
                } else {
                   console.info('no');
                }
            }, 500)
        }, 10000);
    }
}




QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址