KockaAdmiralac / GitHub Notifications

// ==UserScript==
// @name         GitHub Notifications
// @namespace    https://github.com
// @version      1.0
// @description  Send web notifications on GitHub notifications page
// @author       KockaAdmiralac
// @match        https://github.com/notifications
// @license      MIT
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';
    var element = document.querySelector('.notification-indicator .mail-status'),
        hadClass = isUnread();
    function reload() {
        location.reload();
    }
    function isUnread() {
        return element.classList.contains('unread');
    }
    function observationCallback(d) {
        if (!d[0] || d[0].attributeName !== 'class') {
            return;
        }
        var hasClass = isUnread();
        if (hasClass && !hadClass) {
            new Notification('New GitHub notifications!', {
                icon: 'https://camo.githubusercontent.com/7710b43d0476b6f6d4b4b2865e35c108f69991f3/68747470733a2f2f7777772e69636f6e66696e6465722e636f6d2f646174612f69636f6e732f6f637469636f6e732f313032342f6d61726b2d6769746875622d3235362e706e67'
            });
            setTimeout(reload, 3000);
        }
        hadClass = hasClass;
    }
    function permissionCallback(response) {
        if (response === 'granted') {
            var observer = new MutationObserver(observationCallback);
            observer.observe(element, {
                attributes: true
            });
        } else {
            console.error('GitHub Notifications script will not work without a permission to send notifications!');
        }
    }
    Notification.requestPermission().then(permissionCallback);
})();